当前位置:   article > 正文

python回归取残差_如何用sklearn拟合线性回归后的残差方差

sklearn linearregression() 残差项

让我们来定义一下y_true = np.array([3, -0.5, 2, 7])

y_pred = np.array([2.5, 0.0, 2, 8])

平均绝对误差可定义为

^{pr2}$

绝对误差方差为np.var(np.abs(y_true - y_pred)) # 0.125

误差方差为np.var((y_true - y_pred)) # 0.3125

现在如何用scikit-learn实现它?在from sklearn import datasets

from sklearn.model_selection import train_test_split

from sklearn.linear_model import LinearRegression

# X and target data and train test split

boston = datasets.load_boston()

X, y = boston.data, boston.target

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)

# initialize and fit to your train data and predict on test data

clf = LinearRegression()

clf.fit(X_train, y_train)

preds = clf.predict(X_test)

# evaluate

mean_absolute_error(y_test, preds) == np.mean(np.abs(y_test - preds))

# get the variance of (absolute) residuals

np.var(np.abs(y_test - preds))

np.var((y_test - preds))

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/412037
推荐阅读
相关标签
  

闽ICP备14008679号