赞
踩
让我们来定义一下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))
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。