2014-09-26 00:56:37 +08:00
|
|
|
"""
|
|
|
|
|
====================================
|
|
|
|
|
Plotting Cross-Validated Predictions
|
|
|
|
|
====================================
|
|
|
|
|
|
2019-09-05 17:37:05 +08:00
|
|
|
This example shows how to use
|
|
|
|
|
:func:`~sklearn.model_selection.cross_val_predict` to visualize prediction
|
2014-09-26 00:56:37 +08:00
|
|
|
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()
|
2020-01-31 02:02:06 +08:00
|
|
|
X, y = datasets.load_diabetes(return_X_y=True)
|
2014-09-26 00:56:37 +08:00
|
|
|
|
|
|
|
|
# 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:
|
2019-08-20 10:08:23 +08:00
|
|
|
predicted = cross_val_predict(lr, X, y, cv=10)
|
2014-09-26 00:56:37 +08:00
|
|
|
|
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()
|