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]:
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]:
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]:
Attribute shape returns a tuple corresponding to the size or number of each dimension.
In [5]:
A.shape
Out[5]:
The total number of elements in the array is given by the attribute size.
In [6]:
A.size
Out[6]:
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]:
We can also use the following notation to obtain the elements:
In [44]:
A[1][2]
Out[44]:
Consider the elements shown in the following figure
we can access the element as follows
In [8]:
A[0][0]
Out[8]:
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]:
Similarly, we can obtain the first two rows of the 3rd column as follows:
In [10]:
A[0:2,2]
Out[10]:
Corresponding to the following figure:
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]:
In [12]:
Y=np.array([[2,1],[1,2]])
Y
Out[12]:
We can add the numpy arrays as follows.
In [13]:
Z=X+Y
Z
Out[13]:
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]:
In [15]:
Z=2*Y
Z
Out[15]:
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]:
In [17]:
X=np.array([[1,0],[0,1]])
X
Out[17]:
In [18]:
Z=X*Y
Z
Out[18]:
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]:
In [20]:
B=np.array([[1,1],[1,1],[-1,1]])
B
Out[20]:
We use the numpy function dot to multiply the arrays together.
In [21]:
Z=np.dot(A,B)
Z
Out[21]:
In [22]:
np.sin(Z)
Out[22]:
In [23]:
C=np.array([[1,1],[2,2],[3,3]])
C
Out[23]:
In [24]:
C.T
Out[24]:
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.