机器学习,sklearn kfold,clfs之间的区别在哪里?

2024-06-25 22:44:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我用kfold创建了10个分类器。 现在我需要选择一个分类器来预测。 这些量词中有一个比其他量词好还是没有区别

kf=KFold(10,True)
sum_jing=0
sum_recall=0
for train_index,test_index in kf.split(x2):
    x_train,x_test=x2.loc[train_index],x2.loc[test_index]
    y_train,y_test=y2.loc[train_index],y2.loc[test_index]
    #clf = RandomForestClassifier(n_estimators=100, max_depth=2, random_state=0,class_weight="balanced")#balanced比较准0.93#0.7
    #clf = RandomForestClassifier(n_estimators=100, random_state=0,class_weight="balanced",max_depth=2,max_features="auto")#0.93#0.83
    clf= RandomForestClassifier(n_estimators=100, random_state=0,class_weight="balanced_subsample",max_depth=2,max_features="auto")#0.93#0.73-0.89
    clf.fit(x_train,y_train)
    #print("精度",clf.score(x_test,y_test))
    y_predict=clf.predict(x_test)
    sum_jing=sum_jing+clf.score(x_test,y_test)
    sum_recall=sum_recall+metrics.recall_score(y_test,y_predict,)
    #print("召回率",metrics.recall_score(y_test,y_predict))
print(sum_jing/10)
print(sum_recall/10)
from sklearn.externals import joblib
import os
os.chdir("chen")
joblib.dump(clf, "train_model.m")

Tags: testindextrainpredictlocmaxscoresum
1条回答
网友
1楼 · 发布于 2024-06-25 22:44:38

如果我理解正确的话,你是在比较三种不同的模型? 它们可能都是相同的,这取决于超参数对结果模型的实际影响程度

除非你确定这三个是唯一的选择, 我建议进行超参数调整

Scikit learn提供了网格搜索或随机搜索,可以尝试所有/多个参数组合,返回最佳组合

所以在上面显示的代码之前执行GridSearchCV和RandomizedSearchCV

https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.RandomizedSearchCV.htmlhttps://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GridSearchCV.html

要获取有关该概念的更多信息,请执行以下操作:

https://scikit-learn.org/stable/modules/grid_search.html

相关问题 更多 >