forked from lazyprogrammer/machine_learning_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlr_poly.py
52 lines (39 loc) · 1.36 KB
/
lr_poly.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# shows how linear regression analysis can be applied to polynomial data
#
# notes for this course can be founda at:
# https://www.udemy.com/data-science-linear-regression-in-python
import numpy as np
import matplotlib.pyplot as plt
# load the data
X = []
Y = []
for line in open('data_poly.csv'):
x, y = line.split(',')
x = float(x)
X.append([1, x, x*x]) # add the bias term x0 = 1
Y.append(float(y))
# let's turn X and Y into numpy arrays since that will be useful later
X = np.array(X)
Y = np.array(Y)
# let's plot the data to see what it looks like
plt.scatter(X[:,1], Y)
plt.show()
# apply the equations we learned to calculate a and b
# numpy has a special method for solving Ax = b
# so we don't use x = inv(A)*b
# note: the * operator does element-by-element multiplication in numpy
# np.dot() does what we expect for matrix multiplication
w = np.linalg.solve(np.dot(X.T, X), np.dot(X.T, Y))
Yhat = np.dot(X, w)
# let's plot everything together to make sure it worked
plt.scatter(X[:,1], Y)
plt.plot(sorted(X[:,1]), sorted(Yhat))
# note: shortcut since monotonically increasing
# x-axis values have to be in order since the points
# are joined from one element to the next
plt.show()
# determine how good the model is by computing the r-squared
d1 = Y - Yhat
d2 = Y - Y.mean()
r2 = 1 - d1.dot(d1) / d2.dot(d2)
print("the r-squared is:", r2)