Estimated time: 15 minutes

Numpy

NumPy (Numerical Python) is an open source Python library that’s used in almost every field of science and engineering. It’s the universal standard for working with numerical data in Python, and it’s at the core of the scientific Python and PyData ecosystems. NumPy users include everyone from beginning coders to experienced researchers doing state-of-the-art scientific and industrial research and development. The NumPy API is used extensively in Pandas, SciPy, Matplotlib, scikit-learn, scikit-image and most other data science and scientific Python packages.


Basic of Numpy

NumPy’s main object is the homogeneous multidimensional array. It is a table of elements (usually numbers), all of the same type, indexed by a tuple of non-negative integers. In NumPy dimensions are called axes.

NumPy’s array class is called ndarray. It is also known by the alias array.

ndarray attributes Function
ndim Number of axes (dimensions) of the array.
shape The dimensions of the array. For a matrix with n rows and m columns, shape will be (n,m).
size Total number of elements of the array.
dtype Describing the type of the elements in the array.

Let's look an example. Let's create a evenly spaced values from 0 to 15 with arange and reshape to (3,5).

import numpy as np
a = np.arange(15).reshape(3, 5)
a
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])
a.shape
(3, 5)
a.ndim
2
a.shape
(3, 5)
a.size
15
a.dtype
dtype('int64')
type(a)
numpy.ndarray

Let's create an array with random number of ours.

b = np.array([(1.5,2,3), (4,5,6)])
b
array([[1.5, 2. , 3. ],
       [4. , 5. , 6. ]])

The type of the array can also be explicitly specified at creation time.

c = np.array( [ [1,2], [3,4] ], dtype=complex )
c
array([[1.+0.j, 2.+0.j],
       [3.+0.j, 4.+0.j]])

Arrays of 0's and 1's

The function zeros creates an array full of zeros, the function onescreates an array full of ones, and the function empty creates an array whose initial content is random and depends on the state of the memory. By default, the dtype of the created array is float64. We could create random arrays with random.random().

one = np.ones((3,4))
one
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]])
one_int = np.ones((3,4),dtype=int)
one_int
array([[1, 1, 1, 1],
       [1, 1, 1, 1],
       [1, 1, 1, 1]])
zero = np.zeros((3,4))
zero
array([[0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.]])
empty = np.empty((3,4))
empty #result may differ
array([[0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.]])
rand = np.random.random((3,4))
rand
array([[0.31776229, 0.40938551, 0.31779678, 0.53183529],
       [0.64184832, 0.79602921, 0.26223035, 0.18015341],
       [0.50175027, 0.46000645, 0.57778508, 0.14912112]])

Basic operation

Arithmetic operators on arrays apply elementwise. A new array is created and filled with the result.

x = np.array([10,20,30,40])
y = np.array([1,2,4,5])
z = x-y
z
array([ 9, 18, 26, 35])
y**2
array([ 1,  4, 16, 25])
y*2
array([ 2,  4,  8, 10])
x<30
array([ True,  True, False, False])

The product operator * operates elementwise in NumPy arrays. The matrix product can be performed using the @ operator (in python >=3.5) or the dot function or method

f = np.array([[1,2],[3,4]])
g = np.array([[3,4],[5,6]])
f*g
array([[ 3,  8],
       [15, 24]])
f@g
array([[13, 16],
       [29, 36]])
f.dot(g)
array([[13, 16],
       [29, 36]])

Indexing, Slicing and Iterating

One-dimensional arrays can be indexed, sliced and iterated over, much like lists and other Python sequences.

a = np.array([1,2,4,6])
a[0]
1
a[1:3]
array([2, 4])

Multidimensional arrays can have one index per axis. These indices are given in a tuple separated by commas:

b = np.array([[1,2,3,4],[3,4,5,6],[7,8,9,10],[8,9,10,11]])
b[0][0]
1
b[0,0]
1
b[0:4,0]
array([1, 3, 7, 8])
b[-1]
array([ 8,  9, 10, 11])
for row in b:
  print(row)
[1 2 3 4]
[3 4 5 6]
[ 7  8  9 10]
[ 8  9 10 11]

However, if one wants to perform an operation on each element in the array, one can use the flat attribute which is an iterator over all the elements of the array:

for elem in b.flat:
  print(elem)
1
2
3
4
3
4
5
6
7
8
9
10
8
9
10
11

Aggregation

b = np.array([[3,4,5,6],[89,2,4,39]])
b.max()
89
b.min()
2
b.mean()
19.0
b.sum()
152
b.std()
28.853076092507017

Transposing and Reshaping

A common need when dealing with matrices is the need to rotate them. This is often the case when we need to take the dot product of two matrices and need to align the dimension they share. NumPy arrays have a convenient property called T to get the transpose of a matrix:

c = np.array([[1,2,3,4],[5,6,7,5]])
c
array([[1, 2, 3, 4],
       [5, 6, 7, 5]])
c.T
array([[1, 5],
       [2, 6],
       [3, 7],
       [4, 5]])
c.reshape(1,8)
array([[1, 2, 3, 4, 5, 6, 7, 5]])

Stacking together different arrays

Several arrays can be stacked together along different axes:

b = np.zeros((2,3))
c = np.array([[4,5,6],[3,2,1]])
np.hstack((b,c))
array([[0., 0., 0., 4., 5., 6.],
       [0., 0., 0., 3., 2., 1.]])
np.vstack((b,c))
array([[0., 0., 0.],
       [0., 0., 0.],
       [4., 5., 6.],
       [3., 2., 1.]])

Multidimension

m = np.array([[[1,2,3],[5,6,7]],[[3,4,5],[7,8,9]]])
m
array([[[1, 2, 3],
        [5, 6, 7]],

       [[3, 4, 5],
        [7, 8, 9]]])
m*2
array([[[ 2,  4,  6],
        [10, 12, 14]],

       [[ 6,  8, 10],
        [14, 16, 18]]])
m+2
array([[[ 3,  4,  5],
        [ 7,  8,  9]],

       [[ 5,  6,  7],
        [ 9, 10, 11]]])
np.zeros((2,3,2))
array([[[0., 0.],
        [0., 0.],
        [0., 0.]],

       [[0., 0.],
        [0., 0.],
        [0., 0.]]])