-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise1.py
More file actions
83 lines (61 loc) · 2.3 KB
/
exercise1.py
File metadata and controls
83 lines (61 loc) · 2.3 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import numpy as np
def computeCost(X, y, theta):
m = y.size
distance = y - (X @ theta)
J = (distance @ distance) / (2*m)
return J
# implemented in a slightly "un-vectorised" fashion just so that
# i can more easily visualise whats going on, in actuality
# we can do the calculations of the pde terms in one go
# with matrix mult which i will do in the one for
# multivariate version below
def gradientDescent(X, y, theta, alpha, num_iters):
m = y.shape[0]
theta = theta.copy()
J_history = []
for i in range(num_iters):
distance = (X @ theta) - y
# theta0 pde term doesn't need multiplication by x
# so we can just use the 1s column from X as a vector to sum all the distances
# it's like (d1 * 1) + (d2 * 1) + .... + (dm * 1)
# distance is m x 1 vector, 1s is m x 1 vector full of 1s
theta0_term = distance@X[:,0]
# for theta1 term, we need to multiple distance by i'th x
# so we can use the "actual" x values vector and do the dot prod
theta1_term = distance@X[:,1]
sum = np.array([theta0_term, theta1_term])
avg = sum/m
theta = theta - (avg * alpha)
J_history.append(computeCost(X, y, theta))
return theta, J_history
def featureNormalize(X):
X_norm = X.copy()
mu = np.zeros(X.shape[1])
sigma = np.zeros(X.shape[1])
# m = number of training set data points
# n = number of features
m, n = X.shape
# for each feature
for i in range(n):
mean = np.mean(X_norm[:, i])
std = np.std(X_norm[:, i])
mu[i] = mean
sigma[i] = std
X_norm[:, i] = X_norm[:, i] - mean
X_norm[:, i] = X_norm[:, i] / std
return X_norm, mu, sigma
def computeCostMulti(X, y, theta):
return computeCost(X, y, theta)
def gradientDescentMulti(X, y, theta, alpha, num_iters):
m, n = X.shape
theta = theta.copy()
J_history = []
for i in range(num_iters):
distance = (X @ theta) - y
pde_of_cost = distance @ X
avg = pde_of_cost / m
theta = theta - (avg * alpha)
J_history.append(computeCostMulti(X, y, theta))
return theta, J_history
def normalEqn(X, y):
return np.linalg.pinv(X.transpose() @ X) @ X.transpose() @ y