NumPy Array all attributes Explained

In this article, we are going to learn about the attributes or properties of NumPy array.NumPy array object has some attribute properties which help us to get the information about the NumPy array and the data it holds. So let us understand the different properties that a NumPy array object holds.

To understand the properties we will take 2 sample arrays of 1-D array and 2-D array and then we will go through the properties one by one.

import numpy as np
samplearray = np.array([("Apple","Banana", "Cheery","Pears"),
              ("Orange","Grapes", "Kiwi","Pineapple" )])
samplearray1D = np.array([1,2,3,4,5,6,7,8,9,10])

print("2-D Array elements",samplearray)
print("1-D Array elements",samplearray1D)

Output:

2-D Array elements [['Apple' 'Banana' 'Cheery' 'Pears']
 ['Orange' 'Grapes' 'Kiwi' 'Pineapple']]
1-D Array elements [ 1  2  3  4  5  6  7  8  9 10]

As you can see above we have created a NumPy array with some of the values. We may be interested in finding the size of the array, shape of the array, type of elements it holds. To find all this information NumPy array object provides us some of the attribute functions.

1.ndarray.dtype

If we want to find the type of the array elements we can use samplearray.dtype which will return us the type of the array elements.

print("Data Type of of 2-D Array elements",samplearray.dtype)
print("Data Type of of 1-D Array elements",samplearray1D.dtype)

Output:

Data Type of of 2-D Array elements <U9
Data Type of of 1-D Array elements int32

2. ndarray.size

To find the size of the array we can use samplearray.size which will return us the size of the array by providing the number of array elements:

print("Size of 2-D Array",samplearray.size)
print("Size of 1-D Array",samplearray1D.size)

Output:

Size of 2-D Array 8
Size of 1-D Array 10

When trying to determine the size of the array we can specify the axis if we are interested in a particular axis for a 2-D or N-D array. For 1-D array, the axis is always 0 as it is a linear array.

To use the ndarray.size attribute of NumPy we pass 0 if we want the number of rows and we pass 1 if we are interested in the number of columns. If we do not pass any argument for axis then size() functions return the total number of elements in the array.Let us understand this with the example below:

print("Size of 2-D Array",np.size(samplearray))
print("Number of Rows (X-Axis)",np.size(samplearray,0))
print("Number of Column (Y-Axis)",np.size(samplearray,1))

print("Size of 1-D Array",np.size(samplearray1D))
print("Size of 1-D Array(X-Axis)",np.size(samplearray1D,0))

Output:

Size of 2-D Array 8
Number of Rows (X-Axis) 2
Number of Column (Y-Axis) 4
Size of 1-D Array 10
Size of 1-D Array(X-Axis) 10

3. ndarray.shape

Similarly, To find the shape of the array we can use samplearray.shape which will return us the shape of the array in terms of the number of rows and number of columns this array holds.

print("Shape of 2-D Array",samplearray.shape)
print("Shape of 1-D Array",samplearray1D.shape)

Output:

Shape of 2-D Array (2, 4)
Shape of 1-D Array (10,)

4. ndarray.flags

We can use the flags property to find out the memory layout of the NumPy array.C_Contigous tells us if the rows are in contiguous memory locations.F_Contigous tells us if the columns are in contiguous memory locations.

print("2-D Array Memory Layout",samplearray.flags)
print("1-D Array Memory Layout",samplearray1D.flags)

Output:

2-D Array Memory Layout   C_CONTIGUOUS : True
  F_CONTIGUOUS : False
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

1-D Array Memory Layout   C_CONTIGUOUS : True
  F_CONTIGUOUS : True
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

The following attributes contain information about the memory layout of the array:

5.ndarray.ndim

We can find the dimensions of the array by using ndim attribute. This will tell us if the array is 1-D,2-D..or N-Dimensional.
To use this use below syntax:

print("samplearray Array is of Dimensions: ",samplearray.ndim)
print("samplearray1D Array is of Dimensions: ",samplearray1D.ndim)

Output:

samplearray Array is of Dimensions:  2
samplearray1D Array is of Dimensions:  1

6.ndarray.data

We can find the starting memory address of the array by using the data attributes.This will tell us the memory address of the first element.
To use this use below syntax:

print("samplearray Array Starting address: ",samplearray.data)
print("samplearray1D Array Starting address: ",samplearray1D.data)

Output:

samplearray Array Starting address:  <memory at 0x01B31338>
samplearray1D Array Starting address:  <memory at 0x0D5B2088>

7. ndarray.itemsize

We can find the size in bytes of each array element by using itemsize attributes.This will tell us memory bytes required to store one element of the array.
To use this use below syntax:

print("samplearray Array Each element size: ",samplearray.itemsize)
print("samplearray1D Array Each element size: ",samplearray1D.itemsize)

Output:

samplearray Array Each element size:  36
samplearray1D Array Each element size:  4

8.ndarray.nbytes

We can find the size in bytes of the full array by using nbytes attribute.
This will tell us memory bytes required to store all of the array elements.
To use this use below syntax:

print("samplearray Array Total Byte size: ",samplearray.nbytes)
print("samplearray Array Total Byte size: ",samplearray1D.nbytes)

Output:

samplearray Array Total Byte size:  288
samplearray Array Total Byte size:  40

9. ndarray.base

We have a ndarray.base attribute, which helps us determine if the array object is sharing the memory with another object. This comes handy when we want to check View is for which array. This can tell us if an array is a View if another array or not.
To use this use below syntax:

print("samplearray Array have a Base: ",samplearray.base)
print("samplearray Array have a Base: ",samplearray1D.base)
temp = samplearray.view()
print("temp Array have a Base: ",temp.base)

Here is one example of a base attribute by using the view() function. We can see, since the temp is a view of samplearray, the base is providing us the samplearray itself.

Output:

samplearray Array have a Base:  None
samplearray Array have a Base:  None
temp Array have a Base:  [['Apple' 'Banana' 'Cheery' 'Pears']
 ['Orange' 'Grapes' 'Kiwi' 'Pineapple']]

10. ndarray.strides

When we traverse an array, in either direction. Then we need to step a certain number of bytes. To find out the step size we have stride attribute which tells us a Tuple of bytes to step in each dimension when traversing an array.
To use this use below syntax:

print("samplearray Array Tuple of bytes to step in each dimension: ",samplearray.strides)

print("samplearray Array Tuple of bytes to step in each dimension: ",samplearray1D.strides)

Output:

samplearray Array Tuple of bytes to step in each dimension:  (144, 36)
samplearray Array Tuple of bytes to step in each dimension:  (4,)