Array Attributes in Numpy

Array Attributes in Numpy

In this chapter, we will discuss the various array attributes of NumPy.

ndarray.shape

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

Example 1

import numpy as np
a = np.array([[4,6,9],[4,5,6],[6,7,9],[0,9,5]])
print(a.shape)

The output is as follows −

(4, 3)

Example 2

# this resizes the ndarray
import numpy as np
a = np.array([[11,19,18,6],[40,50,60,80],[30,39,80,76]])
a.shape = (3,4)
print(a)

The output is as follows −

[[11, 19, 18, 6]
[40, 50, 60, 80]
[30, 39, 80, 76]]

Example 3

NumPy also provides a reshape function to resize an array.

import numpy as np
a = np.array([[6,7,8],[18,19,20]])
b = a.reshape(3,2)
print(b)

The output is as follows −

[[6, 7]
[8, 18]
[19, 20]]

ndarray.ndim

This array attribute returns the number of array dimensions.

Example 1

# an array of evenly spaced numbers
import numpy as np
a = np.arange(15)
print(a)

The output is as follows −

[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]

Example 2

a = np.arange(24)
a.ndim
# now reshape it
b = a.reshape(3,2,4)
print(b)
# b is having three dimensions

The output is as follows −

[[[ 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

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

The output is as follows −

8

Example 2

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

The output is as follows −

2

numpy.flags

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

Sr.No. Attribute & Description
1 C_CONTIGUOUS (C)
The data is in a single, C-style contiguous segment
2 F_CONTIGUOUS (F)
The data is in a single, Fortran-style contiguous segment
3 OWNDATA (O)
The array owns the memory it uses or borrows it from another object
4 WRITEABLE (W)
The data area can be written to. Setting this to False locks the data, making it read-only
5 ALIGNED (A)
The data and all elements are aligned appropriately for the hardware
6 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

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

Example

The following example shows the current values of flags.

import numpy as np
x = np.array([1, 4, 16, 25])
print (x.flags)

The output is as follows −

C_CONTIGUOUS : True
F_CONTIGUOUS : True
OWNDATA : True
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False
Data Types in Numpy (Prev Lesson)
(Next Lesson) Array Creation Routines using Numpy
', { 'anonymize_ip': true });