def cv_LinearRegression_ci(xM, yV, n_folds=5, scoring='median_absolute_error', disp=False):
"""
metrics.explained_variance_score(y_true, y_pred) Explained variance regression score function
metrics.mean_absolute_error(y_true, y_pred) Mean absolute error regression loss
metrics.mean_squared_error(y_true, y_pred[, ...]) Mean squared error regression loss
metrics.median_absolute_error(y_true, y_pred) Median absolute error regression loss
metrics.r2_score(y_true, y_pred[, ...]) R^2 (coefficient of determination) regression score function.
"""
if disp:
print(xM.shape, yV.shape)
clf = linear_model.LinearRegression()
kf5_c = model_selection.KFold(n_splits=n_folds, shuffle=True)
kf5 = kf5_c.split(xM)
cv_score_l = list()
ci_l = list()
for train, test in kf5:
# clf.fit( xM[train,:], yV[train,:])
# yV is vector but not a metrix here. Hence, it should be treated as a
# vector
clf.fit(xM[train, :], yV[train])
yVp_test = clf.predict(xM[test, :])
# Additionally, coef_ and intercept_ are stored.
ci_l.append((clf.coef_, clf.intercept_))
if scoring == 'median_absolute_error':
cv_score_l.append(
metrics.median_absolute_error(yV[test], yVp_test))
else:
raise ValueError("{} scoring is not supported.".format(scoring))
if disp: # Now only this flag is on, the output will be displayed.
print('{}: mean, std -->'.format(scoring),
np.mean(cv_score_l), np.std(cv_score_l))
return cv_score_l, ci_l
评论列表
文章目录