gpt4 book ai didi

Python - linear_model.Lasso 的 k 折交叉验证

转载 作者:太空宇宙 更新时间:2023-11-03 13:34:17 24 4
gpt4 key购买 nike

我有以下使用 linear_model.Lasso 的代码:

X_train, X_test, y_train, y_test = cross_validation.train_test_split(X,y,test_size=0.2)
clf = linear_model.Lasso()
clf.fit(X_train,y_train)
accuracy = clf.score(X_test,y_test)
print(accuracy)

我想执行 k 次(具体是 10 次)cross_validation。这样做的正确代码是什么?

最佳答案

这是我用来对线性回归模型执行交叉验证并获取详细信息的代码:

from sklearn.model_selection import cross_val_score
scores = cross_val_score(clf, X_Train, Y_Train, scoring="neg_mean_squared_error", cv=10)
rmse_scores = np.sqrt(-scores)

this 中所述在第 108 页预订,这就是我们使用 -score 的原因:

Scikit-Learn cross-validation features expect a utility function (greater is better) rather than a cost function (lower is better), so the scoring function is actually the opposite of the MSE (i.e., a negative value), which is why the preceding code computes -scores before calculating the square root.

并使用这个简单的函数可视化结果:

def display_scores(scores):
print("Scores:", scores)
print("Mean:", scores.mean())
print("Standard deviation:", scores.std())

关于Python - linear_model.Lasso 的 k 折交叉验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42228459/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com