2014-09-26 00:56:37 +08:00
|
|
|
"""
|
|
|
|
|
====================================
|
|
|
|
|
Plotting Cross-Validated Predictions
|
|
|
|
|
====================================
|
|
|
|
|
|
|
|
|
|
This example shows how to use `cross_val_predict` to visualize prediction
|
|
|
|
|
errors.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
from sklearn import datasets
|
2015-09-11 02:26:39 +08:00
|
|
|
from sklearn.model_selection import cross_val_predict
|
2014-09-26 00:56:37 +08:00
|
|
|
from sklearn import linear_model
|
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
|
|
|
lr = linear_model.LinearRegression()
|
|
|
|
|
boston = datasets.load_boston()
|
|
|
|
|
y = boston.target
|
|
|
|
|
|
|
|
|
|
# cross_val_predict returns an array of the same size as `y` where each entry
|
2016-05-17 20:24:25 +08:00
|
|
|
# is a prediction obtained by cross validation:
|
2014-09-26 00:56:37 +08:00
|
|
|
predicted = cross_val_predict(lr, boston.data, y, cv=10)
|
|
|
|
|
|
2015-07-28 14:34:09 +08:00
|
|
|
fig, ax = plt.subplots()
|
2017-06-20 02:51:33 +08:00
|
|
|
ax.scatter(y, predicted, edgecolors=(0, 0, 0))
|
2014-09-26 00:56:37 +08:00
|
|
|
ax.plot([y.min(), y.max()], [y.min(), y.max()], 'k--', lw=4)
|
|
|
|
|
ax.set_xlabel('Measured')
|
|
|
|
|
ax.set_ylabel('Predicted')
|
2015-07-28 14:34:09 +08:00
|
|
|
plt.show()
|