Professional Documents
Culture Documents
Lecture 4
”Linear” “Straight line” (impression)
Linear Non-linear
Coverage
• Minimum knowledge in Linear Algebra for Machine Learning
• Topics
• Vectors and Matrices
• Matrix/Vector operations
• Addition
• multiplication
• Inverse and Transpose
• …
• Linear Regression: equation and visualization
• Supporting matrices in data analytics
• Introduction to NumPy
Background Check
• Given an equation 3x - y + 1 = 0
• How many solution there? Now, guess the number of solution again for each of
A. 0 the following two systems of equations:
3x – y + 1 = 0
B. 1 4x – y – 4 = 0
C. More than 1 but limited
3x - y + 1= 0
D. infinite 3x - y + 10 = 0
Visualization of math equations
from sympy import *
x=symbols(“x”) y = 3x + 1
i.e. 3x - y + 1 = 0
plot(3x+1 )
y = 3x + 1
plot(3*x+1, 4*x -4) y = 4x - 4
3x – y + 1 = 0
4x – y – 4 = 0
3x - y + 1= 0
3x - y + 10 = 0
Key for a sound foundation in math for data science
Example:
k= 2
M = [[1,2,3], [4,5,6], [7,8,9]]
Matrix and Vector Multiplication
M • V or MV
If M is n x m, V must be m x 1, and
the result is a n x 1 vector
if M is 3 x 2, V is 3 x 1 No MV
if M is 3 x 4, V is 4 x 1 MV okay
Several ways of calculating MV
based on dot product
based on scalar matrix
multiplication
How to perform matrix multiplication?
https://www.youtube.com/watch?v=OMA2Mwo0aZg
Practice
A is B is
Given Matrix A as follows
1 0 1 3 2 1
1 2 5 0 1 1 1 0 1
3 4 9 1 0 0 0 2 3
10 20 30
7 6 1
What is A * B?
Assume B is the Transpose Matrix of A
And the first element is marked as B[0][0] A * B equal to B * A?
What is B[1][2]?
How to solve?
4x - 3y + z = - 10
2x + y + 3z = 0
- x + 2y - 5z = 17
https://www.khanacademy.org/math/algebra-home/alg-system-of-equations/alg-
systems-with-three-variables/v/systems-of-three-variables
Solving System of Equations
Examples Solving system of equations by matrix inversion
3x - y = 7
3 −1 𝑥𝑥 7
2x + y = 8 A= X = 𝑦𝑦 B=
2 1 8
Solution: x = 3, y=2
AX = B
X = A-1 B
How to solve?
How to find A-1?
4x - 3y + z = - 10
2x + y + 3z = 0
- x + 2y - 5z = 17
Using numpy for matrix operations
https://realpython.com/linear-regression-in-python/
https://realpython.com/linear-regression-in-python/#simple-linear-regression
https://scikit-learn.org/stable/auto_examples/linear_model/plot_ols.html#sphx-glr-auto-
examples-linear-model-plot-ols-py
Introduction to Numpy
Link: http://numpy.org
NumPy
• Stands for Numerical Python
arr = np.arange(10)
print(arr) # [0 1 2 3 4 5 6 7 8 9]
print(arr[5]) #5
print(arr[5:8]) #[5 6 7]
arr[5:8] = 12 #assignment
print(arr) #[ 0 1 2 3 4 12 12 12 8 9]
Arithmetic with NumPy Arrays
• Any arithmetic operations between equal-size arrays applies the
operation element-wise:
M1 = np.array([[1, 2, 3], [4, 5, 6]])
print(M1)
[[1 2 3]
[4 5 6]]
M2 = np.array([[1,1,1], [2,2,2]])
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
>>>NM = np.array([[1,2,3],[4,5,6]]
>>> print(NM**2)
>>> M = [[1,2,3], [4,5,6]] [[ 1 4 9]
[16 25 36]]
#2x3 matrix >>> print(NM**0.5) #same as print(np.sqrt(NM))
>>>print(M**2) [[1. 1.41421356 1.73205081]
[2. 2.23606798 2.44948974]]
#Error!
>>>
Numpy Matrix Operations
#randomly generate a 3x3 matrix #matrix transposition #scalar multiply matrix
import numpy.matlib M1 =np.array([[1,2,3],[4,5,6]]) M1 =np.array([[1,2,3],[4,5,6]])
import numpy as np M2 = M1.T a =5
print (np.matlib.rand(3,3)) print(M2) M2 = a * M1
print(M2)
Tips: use the following to print all # of floats in ndarrays with only 2 decimal places
np.set_printoptions(formatter={'float_kind': '{:.2f}'.format})
Numpy Matrix Operations
[[4 3] #This is A
[3 2]]
[[-2. 3.] #This is Inverse of A
[ 3. -4.]]
[[1. 0.] #A multiplies Inverse of A
[0. 1.]]
Practice
• Using Matrix Inverse to solve the following system of linear equations
4x - 3y + z = - 10
2x + y + 3z = 0
- x + 2y - 5z = 17
Solving System of Equations: numpy.linalg.solve
Examples
import numpy.matlib
3x - y = 7 import numpy as np
a = np.array([[3,-1], [2,1]])
2x + y = 8 b = np.array([7,8])
Solution: x = 3, y=2 x = np.linalg.solve(a, b)
print(x)
a = np.array([[4, -3,1], [2,1, 3], [-1,2,-5]])
b = np.array([-10,0,17])
How to solve? x = np.linalg.solve(a, b)
print(x)
4x - 3y + z = - 10
2x + y + 3z = 0
- x + 2y - 5z = 17 [3. 2.]
[ 1. 4. -2.]
Numpy: linear algebra
More to explore
References
https://www.tutorialspoint.com/numpy/numpy_linear_algebra.htm
https://www.tutorialspoint.com/numpy/numpy_matrix_library.htm
https://realpython.com/linear-regression-in-python/
https://realpython.com/linear-regression-in-python/#simple-linear-regression
Example
𝑏𝑏₀: intercept
𝑏𝑏₁: slope
Error Calculation
• Mean Absolute Error (MAE) • Mean Squared Error (MSE)