The NumPy Array Object

  • The core of the NumPy Library is one main object: ndarray (which stands for N-dimensional array)
  • This object is a multi-dimensional homogeneous array with a predetermined number of items
  • In addition to the data stored in the array, this data structure also contains important metadata about the array, such as its shape, size, data type, and other attributes.

Basic Attributes of the ndarray Class

Attribute Description
shape A tuple that contains the number of elements (i.e., the length) for each dimension (axis) of the array.
size The total number elements in the array.
ndim Number of dimensions (axes).
nbytes Number of bytes used to store the data.
dtype The data type of the elements in the array.
itemsize Defines teh size in bytes of each item in the array.
data A buffer containing the actual elements of the array.

In order to use the NumPy library, we need to import it in our program. By convention, the numPy module imported under the alias np, like so:

import numpy as np

After this, we can access functions and classes in the numpy module using the np namespace. Throughout this notebook, we assume that the NumPy module is imported in this way.

data = np.array([[10, 2], [5, 8], [1, 1]])
data
array([[10,  2],
       [ 5,  8],
       [ 1,  1]])

Here the ndarray instance data is created from a nested Python list using the function np.array. More ways to create ndarray instances from data and from rules of various kinds are introduced later in this tutorial.

type(data)
numpy.ndarray
data.ndim
2
data.size
6
data.dtype
dtype('int64')
data.nbytes
48
data.itemsize
8
data.data
<memory at 0x10c240990>

Data types

  • dtype attribute of the ndarray describes the data type of each element in the array.
  • Since NumPy arrays are homogeneous, all elements have the same data type.

Basic Numerical Data Types Available in NumPy

dtype Variants Description
int int8, int16, int32, int64 Integers
uint uint8, uint16, uint32, uint64 Unsigned (non-negative) integers
bool Bool Boolean (True or False)
float float16, float32, float64, float128 Floating-point numbers
complex complex64, complex128, complex256 Complex-valued floating-point numbers

Once a NumPy array is created, its dtype cannot be changed, other than by creating a new copy with type-casted array values

data = np.array([5, 9, 87], dtype=np.float32)
data
array([ 5.,  9., 87.], dtype=float32)
data = np.array(data, dtype=np.int32) # use np.array function for type-casting
data
array([ 5,  9, 87], dtype=int32)
data = np.array([5, 9, 87], dtype=np.float32)
data
array([ 5.,  9., 87.], dtype=float32)
data = data.astype(np.int32) # Use astype method of the ndarray class for type-casting
data
array([ 5,  9, 87], dtype=int32)

Data Type Promotion

When working with NumPy arrays, the data type might get promoted from one type to another, if required by the operation. For instance, adding float-value and integer-valued arrays, the resulting array is a float-valued array:

arr1 = np.array([0, 2, 3], dtype=float)
arr1
array([0., 2., 3.])
arr2 = np.array([10, 20, 30], dtype=int)
arr2
array([10, 20, 30])
res = arr1 + arr2
res
array([10., 22., 33.])
res.dtype
dtype('float64')

NOTE:

In some cases, depending on the application and its requirements, it is essential to create arrays with data type appropriately set to right data type. The default data type is float:


np.sqrt(np.array([0, -1, 2]))
array([0.        ,        nan, 1.41421356])
np.sqrt(np.array([0, -1, 2], dtype=complex))
array([0.        +0.j, 0.        +1.j, 1.41421356+0.j])

Here, using the np.sqrt function to compute the square root of each element in an array gives different results depending on the data type of the array. Only when the data type of the array is complex is the square root of –1 resulting in the imaginary unit (denoted as 1j in Python).

%load_ext watermark
%watermark --iversion -g -m -v -u -d
numpy 1.16.3
last updated: 2019-05-16 

CPython 3.6.7
IPython 7.5.0

compiler   : GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)
system     : Darwin
release    : 18.2.0
machine    : x86_64
processor  : i386
CPU cores  : 8
interpreter: 64bit
Git hash   : 0f58e677706b1694ce7747ed41ec62e4fa43d03f