目的:
测试岭回归在含有离群值(outlier)的训练数据集上训练的模型有什么异常行为。
结论:训练集中如果有离群值,训练的模型交叉验证的得分比较差, 但模型在实际测试数据中(测试数据没有离群值)并没有表现的那么差。可见岭回归的鲁棒性还是不错的。所以,如果以后碰到类似的情况,大致可以判断出训练数据有离群值。
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_validate
from sklearn.linear_model import Ridge
from sklearn.metrics import mean_squared_error
train_size = 1000
test_size=400
seed=42
scoring=‘neg_mean_squared_error’
#noises = np.random.randn(size)
noises_train = stats.norm.rvs(loc=0, scale=1, size=train_size, random_state=seed).reshape(-1,1)
X_train = np.arange(train_size).reshape(-1,1)
y