-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboston1numpy.py
31 lines (24 loc) · 857 Bytes
/
boston1numpy.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
# This code is supporting material for the book
# Building Machine Learning Systems with Python
# by Willi Richert and Luis Pedro Coelho
# published by PACKT Publishing
#
# It is made available under the MIT License
# This script shows an example of simple (ordinary) linear regression
import numpy as np
from sklearn.datasets import load_boston
import pylab as plt
boston = load_boston()
x = np.array([np.concatenate((v, [1])) for v in boston.data])
y = boston.target
# np.linal.lstsq implements least-squares linear regression
s, total_error, _, _ = np.linalg.lstsq(x, y)
rmse = np.sqrt(total_error[0] / len(x))
print('Residual: {}'.format(rmse))
# Plot the prediction versus real:
plt.plot(np.dot(x, s), boston.target, 'ro')
# Plot a diagonal (for reference):
plt.plot([0, 50], [0, 50], 'g-')
plt.xlabel('predicted')
plt.ylabel('real')
plt.show()