1.绘制交叉验证预测。
# Plotting Cross-Validated Predictions
from sklearn import datasets
from sklearn.model_selection import cross_val_predict
# Generate cross-validated estimates for each input data point
from sklearn import linear_model
import matplotlib.pyplot as plt
lr = linear_model.LinearRegression()
boston = datasets.load_boston()
# Load and return the boston house-prices dataset (regression).
# from sklearn.datasets import load_boston
# boston = load_boston()
# print(boston.data.shape)
y = boston.target
# cross_val_predict returns an array of the same size as `y` where each entry
# is a prediction obtained by cross validation:
predicted = cross_val_predict(lr, boston.data, y, cv=10)
fig, ax = plt.subplots()
ax.scatter(y, predicted)
ax.plot([y.min(), y.max()], [y.min(), y.max()], 'k--', lw=4)
ax.set_xlabel('Measured')
ax.set_ylabel('Predicted')
plt.show()
结果:
2.保序回归。
print(__doc__)
# Author: Nelle Varoquaux <nelle.varoquaux@gmail.com>
# Alexandre Gramfort <alexandre.gramfort@inria.fr>
# License: BSD
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
from sklearn.linear_model import LinearRegression
# LinearRegression 普通最小二乘线性回归
from sklearn.isotonic import IsotonicRegression
# IsotonicRegression 保序回归模型
from sklearn.utils import check_random_state
# check_random_state:Turn seed into a np.random.RandomState instance
n = 100
x = np.arange(n)
rs = check_random_state(0) # seed = 0
y = rs.randint(-50, 50, size=(n,)) + 50. * np.log(1 + np.arange(n))
# Fit IsotonicRegression and LinearRegression models
ir = IsotonicRegression()
y_ = ir.fit_transform(x, y)
lr = LinearRegression()
lr.fit(x[:, np.newaxis], y) # x needs to be 2d for LinearRegression
# plot result
segments = [[[i, y[i]], [i, y_[i]]] for i in range(n)]
lc = LineCollection(segments, zorder=0)
lc.set_array(np.ones(