我在二进制分类数据集上执行了Logistic回归。结果如下:
训练集精度分数为0.8523,测试集精度为0.8442.。
对于模型的评估和改进,可以使用Kfold和GridSearch cv:
褶皱验证
应用5倍交叉验证
from sklearn.model_selection import cross_val_score
scores = cross_val_score(model, X_test, y_test, cv = 5, scoring='accuracy')
print('Cross-validation scores:{}'.format(scores))
Cross-validation scores:[0.83913352 0.84428267 0.84872159 0.8460309 0.84123601]通过计算交叉验证精度的平均值,可以总结出交叉验证的准确性。
Compute Average cross-validation score
print('Average cross-validation score: {:.4f}'.format(scores.mean()))
Average cross-validation score: 0.8439原始模型得分为0.8523。交叉验证的平均分数为0.8518。因此,我们可以得出结论,交叉验证不会导致性能的提高。
基于GridSearch CV的超参数优化
from sklearn.model_selection import GridSearchCV
parameters = [{'penalty':['l1','l2']},
{'C':[1, 10, 100, 1000]}]
grid_search = GridSearchCV(estimator = model,param_grid = parameters,scoring = 'accuracy',cv = 5,verbose=0)
grid_search.fit(X_train, y_train)
GridSearchCV(cv=5,
estimator=LogisticRegression(random_state=0, solver='liblinear'),
param_grid=[{'penalty': ['l1', 'l2']}, {'C': [1, 10, 100, 1000]}],
scoring='accuracy')在GridSearchCV期间取得最佳成绩
print('GridSearch CV best score : {:.4f}\n\n'.format(grid_search.best_score_))
GridSearch CV best score : 0.8520打印获得最佳结果的参数
print('Parameters that give the best results :','\n\n', (grid_search.best_params_))
Parameters that give the best results :
{'C': 10}由GridSearch选择的打印估计器
print('\n\nEstimator that was chosen by the search :','\n\n', (grid_search.best_estimator_))搜索所选择的估计量:
LogisticRegression(C=10, random_state=0, solver='liblinear')
Calculate GridSearch CV score on train set
est
print('GridSearch CV score on test set: {0:0.4f}'.format(grid_search.score(X_test, y_test)))
GridSearch CV score on test set: 0.8446GridSearch在测试集上的简历评分:0.8525
我用了一组火车进行折叠和网格搜索。
我关心的是哪一套是作为结果培训还是测试。
发布于 2021-08-23 09:15:44
对训练集精度和测试集精度有较好的接近值.这意味着你的模型没有过分合适。但是梅比你还可以改进它。
如果您使用GridSearch简历进行超参数优化,您应该有:
若要评估最终性能,请使用测试集获得的值。这就像使用新的/从未见过的数据来评估模型一样。
此外,在您的问题:交叉验证不是用来改进模型,而是有一个平均的精度值,这应该比单一的精确演算更可靠。
您应该比较这两种方法的最终测试集得分值。非优化得分: 0.8442或0.8439,而优化得分: 0.8525
https://datascience.stackexchange.com/questions/100404
复制相似问题