-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMachineLearningModels.py
More file actions
61 lines (49 loc) · 1.69 KB
/
Copy pathMachineLearningModels.py
File metadata and controls
61 lines (49 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import numpy as np
#Input all lists/matrices as an np.array
def inverse_matrix(matrix):
mat = np.array(matrix)
det = np.linalg.det(mat)
#Check if invertible
if det == 0:
return "The matrix has no inverse"
return np.linalg.inv(mat)
def LinearRegression(X, y):
#Need to add a column of ones at start to account for a constant
X = addOnes(X)
#Calculating Theta using Linear Algebra
XT = np.transpose(X)
XTX = XT @ X
XTX_inv = inverse_matrix(XTX)
XTy = XT @ y
theta = XTX_inv @ XTy
return theta
def GradientDescent(X, y, alpha, steps):
#Need to add a column of ones at start to account for a constant
X = addOnes(X)
#Initialize theta as a zero vector
shape = (X.shape[1], 1)
theta = np.zeros(shape)
m = X.shape[0]
for _ in range(steps):
#Calculate Hypothesis Function output
h = X @ theta
#Diff between hypothesis and actual (calc of residual)
difference = h - y
#Calculating Gradient
gradient = (np.transpose(X) @ difference) / m
#Updating theta (alpha is also called the learning rate)
theta -= alpha*gradient
return theta
def addOnes(matrix):
ones = np.array([[1] for _ in range(len(matrix))])
rows, cols = np.shape(matrix)
matrix = np.append(ones, matrix, axis=1).reshape(rows, cols+1)
return matrix
def predict(theta, x):
#Adding a one to the start to account for a constant
x = np.append([1], x)
return np.transpose(theta) @ x
# Random data I can use for testing
# x = np.array([[0, 1], [2, 3], [3, 5], [4, 4], [10, 15]])
# y = np.array([[1], [2], [3], [6], [12]])
# print(GradientDescent(x, y, 0.001, 10000), LinearRegression(x, y))