Skip to content

Commit 13d9d6e

Browse files
committed
Save predictions in diabetes_y_pred
No need for `regr.predict(diabetes_X_test)` to run twice.
1 parent 9f6b849 commit 13d9d6e

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

examples/linear_model/plot_ols.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,20 @@
4848
# Train the model using the training sets
4949
regr.fit(diabetes_X_train, diabetes_y_train)
5050

51+
# Make predictions using the testing set
52+
diabetes_y_pred = regr.predict(diabetes_X_test)
53+
5154
# The coefficients
5255
print('Coefficients: \n', regr.coef_)
5356
# The mean squared error
5457
print("Mean squared error: %.2f"
55-
% np.mean((regr.predict(diabetes_X_test) - diabetes_y_test) ** 2))
58+
% np.mean((diabetes_y_pred - diabetes_y_test) ** 2))
5659
# Explained variance score: 1 is perfect prediction
5760
print('Variance score: %.2f' % regr.score(diabetes_X_test, diabetes_y_test))
5861

5962
# Plot outputs
6063
plt.scatter(diabetes_X_test, diabetes_y_test, color='black')
61-
plt.plot(diabetes_X_test, regr.predict(diabetes_X_test), color='blue',
62-
linewidth=3)
64+
plt.plot(diabetes_X_test, diabetes_y_pred, color='blue', linewidth=3)
6365

6466
plt.xticks(())
6567
plt.yticks(())

0 commit comments

Comments
 (0)