numpy in Python


In [1]:
import numpy as np 
import matplotlib.pyplot as plt

Consider the list a , the list contains three nested lists each of equal size.


In [2]:
a=[[11,12,13],[21,22,23],[31,32,33]]
a


Out[2]:
[[11, 12, 13], [21, 22, 23], [31, 32, 33]]

We can cast the list to a numpy array as follow


In [3]:
#every elment if of the same type 
A=np.array(a)
A


Out[3]:
array([[11, 12, 13],
       [21, 22, 23],
       [31, 32, 33]])

We can use the attribute ndim to obtain the number of axes or dimensions referred to as the rank.


In [4]:
A.ndim


Out[4]:
2

Attribute shape returns a tuple corresponding to the size or number of each dimension.


In [5]:
A.shape


Out[5]:
(3, 3)

The total number of elements in the array is given by the attribute size.


In [6]:
A.size


Out[6]:
9

Accessing different elements of an Numpy Array

We can use rectangular brackets to access the different elements of the array,The correspondence between the rectangular brackets and the list and the rectangular representation is shown in the following figure for a 3x3 array:

We can access the 2nd-row 3rd column as shown in the following figure:

We simply use the square brackets and the indices corresponding to the element we would like:


In [7]:
A[1,2]


Out[7]:
23

We can also use the following notation to obtain the elements:


In [44]:
A[1][2]


Out[44]:
23

Consider the elements shown in the following figure

we can access the element as follows


In [8]:
A[0][0]


Out[8]:
11

We can also use slicing in numpy arrays,consider the following figure; we would like to obtain the first two columns in the first row

This can be done with the following syntax


In [9]:
A[0][0:2]


Out[9]:
array([11, 12])

Similarly, we can obtain the first two rows of the 3rd column as follows:


In [10]:
A[0:2,2]


Out[10]:
array([13, 23])

Corresponding to the following figure:

Basic Operations

We can also add arrays; the process is identical to matrix addition. Matrix addition of X and Y is shown in the following figure:

The numpy array is given by X and Y


In [11]:
X=np.array([[1,0],[0,1]]) 
X


Out[11]:
array([[1, 0],
       [0, 1]])

In [12]:
Y=np.array([[2,1],[1,2]]) 
Y


Out[12]:
array([[2, 1],
       [1, 2]])

We can add the numpy arrays as follows.


In [13]:
Z=X+Y
Z


Out[13]:
array([[3, 1],
       [1, 3]])

Multiplying a numpy array by a scaler is identical to multiplying a matrix by a scaler. If we multiply the matrix Y by the scaler 2, we simply multiply every element in the matrix by 2 as shown in the figure.

We can perform the same operation in numpy as follows


In [14]:
Y=np.array([[2,1],[1,2]]) 
Y


Out[14]:
array([[2, 1],
       [1, 2]])

In [15]:
Z=2*Y
Z


Out[15]:
array([[4, 2],
       [2, 4]])

Multiplication of two arrays corresponds to an element-wise product or Hadamard product. Consider matrix X and Y. The Hadamard product corresponds to multiplying each of the elements in the same position, i.e. multiplying elements contained in the same colour boxes together. The result is a new matrix that is the same size as matrix Y or X, as shown in the following figure.

We can perform element-wise product of the array X and Y as follows:


In [16]:
Y=np.array([[2,1],[1,2]]) 
Y


Out[16]:
array([[2, 1],
       [1, 2]])

In [17]:
X=np.array([[1,0],[0,1]]) 
X


Out[17]:
array([[1, 0],
       [0, 1]])

In [18]:
Z=X*Y
Z


Out[18]:
array([[2, 0],
       [0, 2]])

We can also perform matrix multiplication with the numpy arrays A and B as follows:

First, we define matrix A and B:


In [19]:
A=np.array([[0,1,1],[1,0,1]])
A


Out[19]:
array([[0, 1, 1],
       [1, 0, 1]])

In [20]:
B=np.array([[1,1],[1,1],[-1,1]])
B


Out[20]:
array([[ 1,  1],
       [ 1,  1],
       [-1,  1]])

We use the numpy function dot to multiply the arrays together.


In [21]:
Z=np.dot(A,B)
Z


Out[21]:
array([[0, 2],
       [0, 2]])

In [22]:
np.sin(Z)


Out[22]:
array([[0.        , 0.90929743],
       [0.        , 0.90929743]])

In [23]:
C=np.array([[1,1],[2,2],[3,3]])
C


Out[23]:
array([[1, 1],
       [2, 2],
       [3, 3]])

In [24]:
C.T


Out[24]:
array([[1, 2, 3],
       [1, 2, 3]])

About the Authors:

Joseph Santarcangelo has a PhD in Electrical Engineering, his research focused on using machine learning, signal processing, and computer vision to determine how videos impact human cognition. Joseph has been working for IBM since he completed his PhD.

Copyright © 2017 cognitiveclass.ai. This notebook and its source code are released under the terms of the MIT License.