-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex1_2.py
30 lines (18 loc) · 887 Bytes
/
ex1_2.py
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
import matplotlib.pyplot as plt
import numpy as np
import ml.linear_regression as lire
from ml.gradient_descent import gradient_descent
import ml.feature as ft
data = np.loadtxt("ml_course_material/machine-learning-ex1/ex1/ex1data2.txt", delimiter=",")
(m, feature_length_with_result) = data.shape
x = data[:, 0:feature_length_with_result - 1].reshape((m, feature_length_with_result - 1))
y = data[:, feature_length_with_result - 1].reshape((m, 1))
normalizer = ft.FeatureNormalizer(x)
normalized_x_m = np.hstack((np.ones((m, 1)), normalizer.normalized_x_m))
(theta, costs) = gradient_descent(normalized_x_m, y, lire.linear_regression_cost, lire.linear_regression_cost_derivative)
plt.plot(costs)
plt.show()
example = np.array([2400, 6])
normalized_example = normalizer.normalize(example)
estimation = np.hstack((np.array([1]), normalized_example)) @ theta
print(estimation)