Share @ LinkedIn Facebook  numpy, basics
Learning Numpy - Simple Tutorial For Beginners - NumPy - Array Attributes Part 4

Learning Numpy - Simple Tutorial For Beginners - NumPy - Array Attributes Part 4

Here we would be learning about the attributes of the array.

Table Of Contents

  1. ndarray.shape
  2. ndarray.ndim
  3. numpy.itemsize
  4. numpy.flags

ndarray.shape

This array attribute returns a tuple consisting of array dimensions. It can also be used to resize the array.

Example 1

In [2]:
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
print (a.shape)
(2, 3)

Example 2

In [3]:
# this resizes the ndarray 
import numpy as np

a = np.array([[1,2,3],[4,5,6]])
a.shape = (3,2)
print (a)
[[1 2]
 [3 4]
 [5 6]]

Example 3

NumPy also provides a reshape function to resize an array.

In [5]:
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
b = a.reshape(3,2)
print (b)
[[1 2]
 [3 4]
 [5 6]]

ndarray.ndim

This array attribute returns the number of array dimensions.

Example 1

In [6]:
# an array of evenly spaced numbers 
import numpy as np
a = np.arange(24)
print (a)
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]

Example 2

In [7]:
# this is one dimensional array 
import numpy as np
a = np.arange(24)
a.ndim

# now reshape it 
b = a.reshape(2,4,3)
print (b)
# b is having three dimensions
[[[ 0  1  2]
  [ 3  4  5]
  [ 6  7  8]
  [ 9 10 11]]

 [[12 13 14]
  [15 16 17]
  [18 19 20]
  [21 22 23]]]

numpy.itemsize

This array attribute returns the length of each element of array in bytes.

Example 1

In [9]:
# dtype of array is int8 (1 byte) 
import numpy as np
x = np.array([1,2,3,4,5], dtype = np.int8)
print (x.itemsize)
1

Example 2

In [11]:
# dtype of array is now float32 (4 bytes) 
import numpy as np
x = np.array([1,2,3,4,5], dtype = np.float32)
print (x.itemsize)
4

numpy.flags

The ndarray object has the following attributes. Its current values are returned by this function.

Example 1

In [1]:
import numpy as np
x = np.array([1,2,3,4,5])
print (x.flags)
  C_CONTIGUOUS : True
  F_CONTIGUOUS : True
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False
Attribute Description
C_CONTIGUOUS (C) The data is in a single, C-style contiguous segment
F_CONTIGUOUS (F) The data is in a single, Fortran-style contiguous segment
OWNDATA (O) The array owns the memory it uses or borrows it from another object
WRITEABLE (W) The data area can be written to. Setting this to False locks the data, making it read-only
ALIGNED (A) The data and all elements are aligned appropriately for the hardware
UPDATEIFCOPY (U) This array is a copy of some other array. When this array is deallocated, the base array will be updated with the contents of this array


Dolly Solanki  Dolly Solanki