|
14 | 14 | boston = load_boston() |
15 | 15 |
|
16 | 16 | # Index number five in the number of rooms |
17 | | -plt.scatter(boston.data[:, 5], boston.target) |
18 | | -plt.xlabel("Number of rooms (RM)") |
19 | | -plt.ylabel("House Price") |
| 17 | +fig,ax = plt.subplots() |
| 18 | +ax.scatter(boston.data[:, 5], boston.target) |
| 19 | +ax.set_xlabel("Number of rooms (RM)") |
| 20 | +ax.set_ylabel("House Price") |
20 | 21 |
|
21 | 22 | x = boston.data[:, 5] |
22 | 23 | # fit (used below) takes a two-dimensional array as input. We use np.atleast_2d |
|
29 | 30 | lr = LinearRegression(fit_intercept=False) |
30 | 31 | lr.fit(x, y) |
31 | 32 |
|
32 | | -plt.plot([0, boston.data[:, 5].max() + 1], |
| 33 | +ax.plot([0, boston.data[:, 5].max() + 1], |
33 | 34 | [0, lr.predict(boston.data[:, 5].max() + 1)], '-', lw=4) |
34 | | -plt.savefig('Figure1.png', dpi=150) |
| 35 | +fig.savefig('Figure1.png') |
35 | 36 |
|
36 | 37 | mse = mean_squared_error(y, lr.predict(x)) |
37 | 38 | rmse = np.sqrt(mse) |
|
42 | 43 |
|
43 | 44 | lr.fit(x, y) |
44 | 45 |
|
45 | | -plt.clf() |
46 | | -plt.xlabel("Number of rooms (RM)") |
47 | | -plt.ylabel("House Price") |
48 | | -plt.scatter(boston.data[:, 5], boston.target) |
| 46 | +fig,ax = plt.subplots() |
| 47 | +ax.set_xlabel("Number of rooms (RM)") |
| 48 | +ax.set_ylabel("House Price") |
| 49 | +ax.scatter(boston.data[:, 5], boston.target) |
49 | 50 | xmin = x.min() |
50 | 51 | xmax = x.max() |
51 | | -plt.plot([xmin, xmax], lr.predict([[xmin], [xmax]]) , '-', lw=4) |
52 | | -plt.savefig('Figure2.png', dpi=150) |
| 52 | +ax.plot([xmin, xmax], lr.predict([[xmin], [xmax]]) , '-', lw=4) |
| 53 | +fig.savefig('Figure2.png') |
53 | 54 |
|
54 | 55 | mse = mean_squared_error(y, lr.predict(x)) |
55 | 56 | print("Mean squared error (of training data): {:.3}".format(mse)) |
|
0 commit comments