Numpy shape()

Numpy

numpy.shape(shape, dtype=None, order='C')
Return tuple with details of the array.
aarray input
Shape() returns a tuple giving shape of the array. Number of rows , columns and dimension of the array.

Let us check how different dimensional arrays returns the value as tuple when we use shape().
One Dimensional array ( 1-D )  : (n, ) :tuple elements = 1 
Two Dimensional array ( 2-D )  : (n,m )   :tuple elements  = 2
Three Dimensional array ( 3-D) : (n,m,j)  : tuple elements  = 3 
Four Dimensional array ( 4-D ) : (n,m,j,k): tuple  elements = 4 
Ones array of shape(3,4)Ones array of shape(4,3)
Shape: (3, 4)
Dimension 2
Shape: (4, 3)
Dimension 2
You can read more on how to create arrays by using ones(). We will input different shapes to create the array by using ones().

Along with shape() we will display the dimainsion of the array by using ndim

One dimensional array

import numpy as np
ar=np.ones((3,))
print(ar)
print("Shape: ", ar.shape)
print("Dimension ", ar.ndim)
Output
[1. 1. 1.]
Shape:  (3,)
Dimension  1

Two dimensional array

import numpy as np
ar=np.ones((3,2))
print(ar)
print("Shape: ", ar.shape)
print("Dimension ", ar.ndim)
Output
[[1. 1.]
 [1. 1.]
 [1. 1.]]
Shape:  (3, 2)
Dimension  2

Three dimensional array

import numpy as np
ar=np.ones((3,2,3))
print(ar)
print("Shape: ", ar.shape)
print("Dimension ", ar.ndim)
Output
[[[1. 1. 1.]
  [1. 1. 1.]]

 [[1. 1. 1.]
  [1. 1. 1.]]

 [[1. 1. 1.]
  [1. 1. 1.]]]
Shape:  (3, 2, 3)
Dimension  3

Four dimensional array

import numpy as np
ar=np.ones((3,2,3,4))
print(ar)
print("Shape: ", ar.shape)
print("Dimension ", ar.ndim)
Output
[[[[1. 1. 1. 1.]
   [1. 1. 1. 1.]
   [1. 1. 1. 1.]]

  [[1. 1. 1. 1.]
   [1. 1. 1. 1.]
   [1. 1. 1. 1.]]]


 [[[1. 1. 1. 1.]
   [1. 1. 1. 1.]
   [1. 1. 1. 1.]]

  [[1. 1. 1. 1.]
   [1. 1. 1. 1.]
   [1. 1. 1. 1.]]]


 [[[1. 1. 1. 1.]
   [1. 1. 1. 1.]
   [1. 1. 1. 1.]]

  [[1. 1. 1. 1.]
   [1. 1. 1. 1.]
   [1. 1. 1. 1.]]]]
Shape:  (3, 2, 3, 4)
Dimension  4
reshape() : Change shape or dimension of the input array

Numpy eye() bincount() arange() linspace()


plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer