Python_for_AIML2
Python_for_AIML2
1
np.linspace(0, 10, 8, endpoint=True) generates evenly spaced 8 data points from 0 to 10.
a = np.random.randint(100)
print(a)
x = np.random.rand(2, 2)
y = np.random.rand(2, 3)
print(x)
print(y)
31
[[ 5 40 38 78 11]
[22 30 72 42 65]
[87 11 75 86 25]]
[[0.27238965 0.39646817]
[0.69083451 0.23382228]]
[[0.08980293 0.47838114 0.67746532]
[0.84147445 0.24579799 0.4059905 ]]
[0. 1. 2.]
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
[ 0. 1.42857143 2.85714286 4.28571429 5.71428571 7.14285714
8.57142857 10. ]
2
• plt.plot(x, x**2, label=‘quadratic’)
https://matplotlib.org/tutorials/introductory/pyplot.html
[0. 0.2 0.4 0.6 0.8 1. 1.2 1.4 1.6 1.8 2. 2.2 2.4 2.6 2.8 3. 3.2 3.4
3.6 3.8 4. 4.2 4.4 4.6 4.8]
3
3.1 Controlling the x or y axis limits
y = np.zeros(8)
x1 = np.linspace(0, 10, 8, endpoint=True)
x2 = np.linspace(0, 10, 8, endpoint=False)
plt.plot(x1, y, 'o', x2, y + 0.5, 'sr')
plt.ylim([-0.5, 1])
#(-0.5, 1)
plt.show()
4
plt.xlabel('entry a')
plt.ylabel('entry b')
plt.show()
5
4 Exercise 1
Load the data from iris_with_header.csv using pandas library and display the dimension of the
data, statistics summary and top 3 rows of the data. Plot the sepal_length and sepal_width data
as scatter chart as shown in expected output.
Expected Output: 1. Load and display the data from iris_w_header.csv
shape of data : (150, 5)
6
sepal_length sepal_width petal_length petal_width class
0 5.1 3.5 1.4 0.2 Iris-setosa
1 4.9 3.0 1.4 0.2 Iris-setosa
2 4.7 3.2 1.3 0.2 Iris-setosa
# 1) Load data and display dimension, statistics summary and top 3 rows
#_______________________________________________________________________________________________
#_______________________________________________________________________________________________
# display dimension
#_______________________________________________________________________________________________
7
# display statistics summary
#_______________________________________________________________________________________________
#_______________________________________________________________________________________________
#_______________________________________________________________________________________________
#_______________________________________________________________________________________________
#_______________________________________________________________________________________________
plt.show()
8
[8]: "\ndf.plot(kind='scatter', x='sepal_length', y='sepal_width',
c='blue')\nplt.show()\n"
9
Matrix A is an m·n-tuple of elements aij , i = 1, . . . , m, j = 1, . . . , n, which is ordered according
to a rectangular scheme consisting of m rows and n columns. The matrix objects are a subclass of
the numpy arrays (ndarray). The matrix objects inherit all the attributes and methods of ndarry.
Multidimensional array (matrix) can be created using np.array function as follow:
• creating 2x3 matrix A
• A = np.array([[1,2,3], [4,5,6]])
Alternatively you can reshape the a list generated by np.arrange function into n-dimensions as
follow:
• creating 2x3 matrix A
• A = np.arange(6).reshape(2,3)
mA = np.array([[1,2,3], [4,5,6]])
# mA = np.arange(6).reshape(2,3)
print(vR)
print(vC)
print(vC1)
print(mA)
[1 2 3 4]
[1 2 3 4]
[[1]
[2]
[3]
[4]]
[[1 2 3]
[4 5 6]]
10
• Outer Product of Vector
• Inner Product of Vector
n
h x, yi = ∑ xi yi = X T .Y
i =1
11
[10]: v = np.array([1,2,3])
sV = 3 * v
print(sV)
[3 6 9]
[11]: vA = np.array([1,2,3,4,5])
vB = np.array([6,7,8,9,10])
[ 6 14 24 36 50]
[[ 6 7 8 9 10]
[12 14 16 18 20]
[18 21 24 27 30]
[24 28 32 36 40]
[30 35 40 45 50]]
130
(5,)
(5, 1)
[130]
4.3 Exercise 1:
Create row vector rA with number 1,3,5,7 and column vector cB with 2,4,6,8. Perform outer prod-
uct and dot product of the vector.
Expected output:
1. Outer product
12
2 4 6 8
6 12 18 24
10 20 30 40
14 28 42 56
2. Dot product
• [100]
[12]: import numpy as np
# Create row vector
#_______________________________________________________________________________________________
#_______________________________________________________________________________________________
#_______________________________________________________________________________________________
print(vOuter)
#_______________________________________________________________________________________________
print(vDot)
[[ 2 4 6 8]
[ 6 12 18 24]
[10 20 30 40]
[14 28 42 56]]
[100]
13
#A = np.array([[1,2,3], [4,5,6]])
A = np.arange(6).reshape(2,3)
print(A1.shape)
print(B1.shape)
C1 = A1 * B1
print(C1)
[[ 7 9 11]
[13 15 17]]
(2, 3)
(3,)
[[ 7 16 27]
[28 40 54]]
[14]: a1 = np.arange(4).reshape((2,2))
b1 = np.arange(6).reshape(3,2)
print(a1)
print(b1)
14
print(result)
print(result.shape)
[[0 1]
[2 3]]
[[0 1]
[2 3]
[4 5]]
[[ 1 3 5]
[ 3 13 23]]
(2, 3)
[[14 32]]
(1, 2)
1 ∗ 7 + 2 ∗ 10 = 27
1 ∗ 8 + 2 ∗ 11 = 30
1 ∗ 9 + 2 ∗ 12 = 33
3 ∗ 7 + 4 ∗ 10 = 61
3 ∗ 8 + 4 ∗ 11 = 68
3 ∗ 9 + 4 ∗ 12 = 75
5 ∗ 7 + 6 ∗ 10 = 95
15
5 ∗ 8 + 6 ∗ 11 = 106
5 ∗ 9 + 6 ∗ 12 = 117
[15]: a1 = np.arange(6).reshape((2,3))
b1 = np.arange(12).reshape(3,4)
print(a1)
print(b1)
[[0 1 2]
[3 4 5]]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[20 23 26 29]
[56 68 80 92]]
(2, 4)
1 0 0
0 1 0
0 0 1
Dot product of any square matrix by identity matrix will get same matrix.
A.I = A
Inverse Matrix
The inverse of a matrix A is defined as the matrix A−1 which multiplies A to give the identity
matrix. Not all the matrix are invertable. Matrix which has inversed matrix is called non-singular
otherwise singular matrix. The dot product of matrix and inverse of matrix returns identity matrix.
To get inverse of matrix, you can use inv() function from numpy.linalg package.
A.A−1 = I
Transpose of Matrix
The transpose of a matrix is obtained by moving the rows data to the column and columns data to
the rows. If we have an array of shape (X, Y) then the transpose of the array will have the shape
16
(Y, X). Transpose operation can be performed by transpose function of numpy package or just use
.T operation.
For example:
X is matrix 2 x 3 matrix and transpose of X (t - 3 x 2) can be generated following ways.
• t = np.transponse(X)
• or
• t = X.T
[16]: A = np.array([[1, 1, 1],[1, 2, 3],[1, 3, 4]])
# identity matrix
IM = np.array([[1, 0, 0],[0, 1, 0],[0, 0, 1]])
C = A.dot(IM)
C1 = IM.dot(A)
print(IM)
print(C)
print(C1)
print(t)
[[1 0 0]
[0 1 0]
[0 0 1]]
[[1 1 1]
[1 2 3]
[1 3 4]]
[[1 1 1]
[1 2 3]
[1 3 4]]
[[1 2 3]
[4 5 6]]
17
# multiply A and B
I = A.dot(B)
print(np.round_(I, decimals = 5))
[[1. 2.]
[3. 4.]]
[[-2. 1. ]
[ 1.5 -0.5]]
[[1. 0.]
[0. 1.]]
4.5 Exercise 2:
Create matrix A of 2x2 and B of 2x3 with values shown in the figure. Find the dot product A.B of
two matrices.
Matrix A:
2 4
6 8
Matrix B:
2 4 6
1 2 3
Expected output:
1. Dot product
8 16 24
20 40 60
#_______________________________________________________________________________________________
#_______________________________________________________________________________________________
#_______________________________________________________________________________________________
print(C)
[[ 8 16 24]
[20 40 60]]
18
4.6 Exercise 3:
Create matrix X of 3x3 and column vector vB of 3x1 with values shown in the figure. Find the dot
product X.vB.
Matrix X:
1 5 3
2 1 6
1 2 3
Vector B:
2
4
6
Expected output:
1. Dot product
40 44 28
#_______________________________________________________________________________________________
#_______________________________________________________________________________________________
#_______________________________________________________________________________________________
#_______________________________________________________________________________________________
[40 44 28]
n
h x, wi = ∑ xi wi = X T .w
i =1
19
Matrix X:
1 5 3
1 2 3
Weight Matrix W:
0.1 0.2
0.5 0.2
Expected output:
1. Weighted sum / Dot product
0.6 0.4
1.5 1.4
1.8 1.2
#_______________________________________________________________________________________________
#_______________________________________________________________________________________________
print(X.T)
print(W.shape)
#_______________________________________________________________________________________________
#_______________________________________________________________________________________________
[[1 1]
[5 2]
[3 3]]
(2, 2)
[[0.6 0.4]
[1.5 1.4]
[1.8 1.2]]
20
import timeit
import tensorflow as tf
import pathlib
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt
for h in range(height):
for w in range(width):
for c in range(channels):
distance += (image1[h][w][c] - image2[h][w][c])**2
print(distance)
l2_loop(image1, image2)
#%timeit -n 100 -r 3 l2_loop(image1, image2)
#%timeit -n 100 -r 3 l2_vectorise(image1, image2)
(280, 457, 3)
(280, 457, 3)
0.0
21