0% found this document useful (0 votes)
0 views

Python_for_AIML2

The document outlines a lab exercise on Python for AI and Machine Learning, covering topics such as random number generation, data visualization with Matplotlib, and basic algebra concepts including scalars, vectors, and matrices. It provides instructions for completing lab exercises, examples of using NumPy for generating random numbers and sequences, and guidance on plotting data. Additionally, it includes an exercise involving loading and analyzing a dataset using pandas and visualizing the results.

Uploaded by

nagulxlugan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Python_for_AIML2

The document outlines a lab exercise on Python for AI and Machine Learning, covering topics such as random number generation, data visualization with Matplotlib, and basic algebra concepts including scalars, vectors, and matrices. It provides instructions for completing lab exercises, examples of using NumPy for generating random numbers and sequences, and guidance on plotting data. Additionally, it includes an exercise involving loading and analyzing a dataset using pandas and visualizing the results.

Uploaded by

nagulxlugan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

1 Python for AIML 2

In this lab following topic will be covered:


1. Number generator in Python
2. Plotting with Matplotlib Library
3. Scalar, Vector and Matrix
4. Vectorization
Instruction to compplete lab exercises:
1. Open python notebook file under Lab folder
2. Read the problem statement in the exercise and expected output
3. Uncomment and remove the lines and fill in wiht your answer
4. Run your code to produce expected output.
Noted: Data files are stored in dataset folder

2 Random Number Generators


Machine learning algorithm sometime requires to generate random number such as weight of the
features. Sample data as random numbers sometime needs to generate when studying a machine
learning model or behavior of a program. Similar to generating the random number creating
numerical sequences is also useful tools in python. Following functions are frequently used in
generation random or sequence of numbers.
1. random
2. arange()
3. linspace()
Numpy has a random module has many functions that generates random number.
numpy.random.rand() function creates an array of the given shape and populate it with random
samples from a uniform distribution over.
Example:
numpy.random.rand(2, 3)
• [[0.02161571 0.84027497 0.56929447]
• [0.12055862 0.77247655 0.79532843]]
numpy.arange():
Function, arange() in numpy generate sequence of numbers as in ndarray type. For integer ar-
guments the function is equivalent to the Python built-in range function, but returns an ndarray
rather than a list.
Example: np.arange(3.0) returns ndarray [ 0., 1., 2.]
numpy.linspace():
Function linspace returns evenly spaced numbers over a specified interval.
Example:

1
np.linspace(0, 10, 8, endpoint=True) generates evenly spaced 8 data points from 0 to 10.

[1]: import numpy as np

a = np.random.randint(100)
print(a)

b = np.random.randint(100, size=(3, 5))


print(b)

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 ]]

[2]: import numpy as np


r1 = np.arange(3.0)
r2 = np.arange(1, 13).reshape(3,4)
xlin = np.linspace(0, 10, 8, endpoint=True)
print(r1)
print(r2)
print(xlin)

[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. ]

3 Plotting with Matplotlib


Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations
in Python. We will use the pyplot module in Matplotlib for visualization. For the plot function,
you always needs to provide x, y pair of arguments with optional arguments.
Example:
• x = np.arange(0., 5., 0.2)

2
• plt.plot(x, x**2, label=‘quadratic’)
https://matplotlib.org/tutorials/introductory/pyplot.html

[3]: import numpy as np


import matplotlib.pyplot as plt

# evenly sampled time at 200ms intervals


x = np.arange(0., 5., 0.2)
print(x)
#x = np.linspace(0, 2, 100) #100 points, with each point between 0 & 2
plt.plot(x, x, label='linear') # Plot some data on the (implicit) axes.
plt.plot(x, x**2, label='quadratic') # etc.
plt.plot(x, x**3, label='cubic')
plt.xlabel('x label')
plt.ylabel('y label')
plt.title("Simple Plot")
plt.legend()
plt.savefig('.\images\simple_plot.png')
plt.show()

[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

[4]: import numpy as np


import matplotlib.pyplot as plt

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()

3.2 Plotting with keyword strings


[5]: data = {'a': np.arange(50),
'c': np.random.randint(0, 50, 50),
'd': np.random.randn(50)}
data['b'] = data['a'] + 10 * np.random.randn(50)
data['d'] = np.abs(data['d']) * 100

plt.scatter('a', 'b', c='c', s='d', data=data)


#print(data['d'])
#names = ['group_a', 'group_b']
#plt.hist(data['d'] )

4
plt.xlabel('entry a')
plt.ylabel('entry b')
plt.show()

3.3 Plotting a figure with multiple subplots

[6]: import matplotlib.pyplot as plt


import numpy as np
np.random.seed(0)
data = np.random.randn(2, 100) # 2 x 100 array of normal-distributed random
,→data

fig, axs = plt.subplots(2, 2, figsize=(5, 5))


axs[0, 0].hist(data[0])
axs[1, 0].scatter(data[0], data[1])
axs[0, 1].plot(data[0], data[1])
axs[1, 1].hist2d(data[0], data[1])
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)

sepal_length sepal_width petal_length petal_width


count 150.000000 150.000000 150.000000 150.000000
mean 5.843333 3.054000 3.758667 1.198667
std 0.828066 0.433594 1.764420 0.763161
min 4.300000 2.000000 1.000000 0.100000
25% 5.100000 2.800000 1.600000 0.300000
50% 5.800000 3.000000 4.350000 1.300000
75% 6.400000 3.300000 5.100000 1.800000
max 7.900000 4.400000 6.900000 2.500000

sample of 3 rows of data using head():

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

2. Scatter plot using petal_length vs sepal_length

[7]: import pandas as pd


import matplotlib.pyplot as plt

# 1) Load data and display dimension, statistics summary and top 3 rows

# Load the data

#_______________________________________________________________________________________________

#_______________________________________________________________________________________________

# display dimension

#_______________________________________________________________________________________________

7
# display statistics summary

#_______________________________________________________________________________________________

# display top 3 rows

#_______________________________________________________________________________________________

shape of data : (150, 5)

sepal_length sepal_width petal_length petal_width


count 150.000000 150.000000 150.000000 150.000000
mean 5.843333 3.054000 3.758667 1.198667
std 0.828066 0.433594 1.764420 0.763161
min 4.300000 2.000000 1.000000 0.100000
25% 5.100000 2.800000 1.600000 0.300000
50% 5.800000 3.000000 4.350000 1.300000
75% 6.400000 3.300000 5.100000 1.800000
max 7.900000 4.400000 6.900000 2.500000

sample of 3 rows of data using head(): 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

[8]: # 2) Plot the data using matplot library


# You need to provide name of the column for x and y

#_______________________________________________________________________________________________

#_______________________________________________________________________________________________

#_______________________________________________________________________________________________

plt.show()

8
[8]: "\ndf.plot(kind='scatter', x='sepal_length', y='sepal_width',
c='blue')\nplt.show()\n"

4.1 Scalar, Vector and Matrix


It is important to understand basic algebra concept such as Matrix, Vector operations and their
usage before we start to learn machine learning. In this topic, we explain the fundamental concepts
of the different algebra operations in python.
Scalar
A scalar is a real number or a symbol representing a real number eg. 2, 3.4
Vector
Vector can be defines as a list of number in python. In linear algebra, a column vector is a column
of entries and a row vector is a row of entries. Row can be created using numpy array. For
the column vector, it can be created either by transposing the row vector or reshaping it into
two dimension with single column. Transpose of vector means replacing the row-wise data into
column-wise and vis-à-vis. Any list or n-d array can be transposed by .T function.
Example:
Row vector - vR = np.array([1,2,3,4])
Column vectors: - vC = vR.T - vC1 = vR.reshape(4,1)
Matrix

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)

[9]: import numpy as np

#Creating row vector


vR = np.array([1,2,3,4])

#Creating column vector by transposing row vector


vC = vR.T

#Creating column vector by reshaping row vector


vC1 = vR.reshape(4,1)

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]]

4.2 Vector Operations


• Addtion, Multiplication (Element-wise operations)
Any arithmetic operations between equal-size arrays applies the operation elementwise.

10
• Outer Product of Vector
• Inner Product of Vector

4.2.1 Outer Product of Vector


Two vectors a and b with dimension n x 1 and 1 x m then the outer product of the vector results in
a rectangular matrix of n x m. If two vectors have same dimension then the resultant matrix will
be a square matrix as shown in the figure.

4.2.2 Inner/dot Product of Vector


It is an algebraic operation that takes two coordinate vectors of equal size and returns a single
number. The dot product of two vectors is calculated by multiplying corresponding entries in each
vector and adding up all of those products. For single domension array, inner and dot product
behaves the same.

Dot product of can be represented by following equation:

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])

# Element-wise opetation of (1,5) (1,5) => (1,5)


Me = vA * vB
print(Me)

# Outer product of vector (1,5) and (1,5) => (5,5)


Mouter = np.outer(vA, vB)
print(Mouter)

# Inner product of vector (1,5) and (1,5) => (5,5)


Minner = np.inner(vA, vB)
print(Minner)

# Convert to column vector and find dot product of two vectors


vC = vB.reshape(5,1)
Mdot = np.dot(vA, vC)
print(vA.shape)
print(vC.shape)
print(Mdot)

[ 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

#_______________________________________________________________________________________________

# Create column vector

#_______________________________________________________________________________________________

# Calculate and display vector outer product

#_______________________________________________________________________________________________

print(vOuter)

# Calculate and display vector dot product

#_______________________________________________________________________________________________

print(vDot)

[[ 2 4 6 8]
[ 6 12 18 24]
[10 20 30 40]
[14 28 42 56]]
[100]

4.4 Matrix Operations


4.4.1 Element-wise operation (Addition, Multiplication)
This operation can be defined only two dimensions are compatible which is they are equal, or one
of them is 1.
If X and Y are ndarrays, - X + Y define an element by element addition - X * Y define an element
by element multiplication (Hadamard product).

[13]: import numpy as np

# creating 2x3 matrix A

13
#A = np.array([[1,2,3], [4,5,6]])
A = np.arange(6).reshape(2,3)

# Creating 2x3 matrix B


B = np.array([[7,8,9],[10,11,12]])

# Matrix element-wise operation


C = A + B
print(C)

A1 = np.array([[ 1, 2, 3], [4, 5, 6]])


B1 = np.array([7, 8, 9])

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]]

4.4.2 Inner Product


It is a sum product over the last axes. Hence the size of the last axes must be the same. eg. Inner
product of A: 2 x 2 and B: 3 x 2 produce a sum product of 2 x 3 matrix.

[14]: a1 = np.arange(4).reshape((2,2))
b1 = np.arange(6).reshape(3,2)
print(a1)
print(b1)

#Inner (2,2) (3,2) => (2, 3)


mi = np.inner(a1, b1)
print(mi)
print(mi.shape)

#Inner (1,3) (2, 3) => (1,2)


ma = np.array([[1,2,3]])
mb = A = np.array([[1,2,3], [4,5,6]])
result = np.inner(ma, mb)

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)

4.4.3 Dot Product


The product of two matrices A in the left and B in the right is defined A.B. It is a sum product over
the last axis of A and the second-last axis of B. In two dimension array (axis 0 - row and axis 1 -
column), it is a sum product over last axis of A, column, and second-last axis of b, row. eg. (3x2) .
(2x3) results 3x3 matrix. However dot product are not communicative which means A.B not equal
to B.A.
Example:
A.B=  
1 2
3 4
5 6
.  
7 8 9
10 11 12
=  
27 30 33
61 68 75 
95 106 117

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)

#Inner (2,3) (3,4) => (2, 4)


md = np.dot(a1, b1)
print(md)
print(md.shape)

[[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)

4.4.4 Identity Matrix, Inverse Matrix and Transpose of Matrix


Identity matrix are the n x n square matrix which contains ones on the main diagonal and zeros
elsewhere.
Identity matrix:

 
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)

X = np.array([[1, 4], [2, 5],[3, 6]])


t = np.transpose(X)
#t = X.T

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]: import numpy as np


from numpy.linalg import inv
# define matrix
A = np.array([[1.0, 2.0], [3.0, 4.0]])
print(A)
# invert matrix
B = inv(A)
print(B)

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

[24]: import numpy as np

#_______________________________________________________________________________________________

#_______________________________________________________________________________________________

#_______________________________________________________________________________________________

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

[1]: import numpy as np

#1) Create matrix X and Vector B

#_______________________________________________________________________________________________

#_______________________________________________________________________________________________

#2) Dot product of matrix X and Vector B

#_______________________________________________________________________________________________

#_______________________________________________________________________________________________

[40 44 28]

4.7 Additional Exercise:


Weighted sum value all features represented by following equation is used to compute in neural
network. Feature of column X1 and X2 are in matrix X of 2x3 and two set of weight data in matrix
W of 2x2 with values shown in the figure. Find the weighted sum / dot product X T .W. Matrix X
need to transpose in order to obtain feature column data as row-wise.

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

[6]: import numpy as np

#1) Create matrix X and Vector W

#_______________________________________________________________________________________________

#_______________________________________________________________________________________________

print(X.T)
print(W.shape)

#1) Computte Weighted sum / Dot product

#_______________________________________________________________________________________________

#_______________________________________________________________________________________________

[[1 1]
[5 2]
[3 3]]
(2, 2)
[[0.6 0.4]
[1.5 1.4]
[1.8 1.2]]

[5]: # Used to load images


#import cv2

20
import timeit
import tensorflow as tf
import pathlib
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt

# load the images


#image1 = cv2.imread(".\images\image1.jpeg").astype(np.int32)
#image2 = cv2.imread(".\images\image2.jpeg").astype(np.int32)
image1 = tf.keras.preprocessing.image.load_img(".\images\image1.jpg")
image1 = tf.keras.preprocessing.image.img_to_array(image1)
image2 = tf.keras.preprocessing.image.load_img(".\images\image1.jpg")
image2 = tf.keras.preprocessing.image.img_to_array(image2)
print(image1.shape)
print(image2.shape)

# Define the function that implements the loop version


def l2_loop(image1, image2):
height, width, channels = image1.shape
distance = 0

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)

# Define the vectorised version


def l2_vectorise(image1, image2):
distsum = ((image1 - image2)**2).sum()
print(distsum)

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

You might also like