如何使用python动态选择最佳模型 [英] How to choose the best model dynamically using python
问题描述
这是我的代码,我正在构建6个模型,而我正在获得准确性,我如何动态地选择精度更高的那个,而我只想执行那个精度最高的模型.
"prepare configuration for cross validation test harness"
seed = 7
"prepare models"
models = []
models.append(('LR', LogisticRegression()))
models.append(('LDA', LinearDiscriminantAnalysis()))
models.append(('KNN', KNeighborsClassifier()))
models.append(('CART', DecisionTreeClassifier()))
models.append(('NB', GaussianNB()))
models.append(('RF',RandomForestClassifier()))
#models.append(('SVM', SVC()))
"evaluate each model in turn"
results = []
names = []
scoring = 'accuracy'
for name, model in models:
kfold = model_selection.KFold(n_splits=10, random_state=seed)
cv_results = model_selection.cross_val_score(model, orginal_telecom_80p_test[features], orginal_telecom_80p_test["Churn"], cv=kfold, scoring=scoring)
results.append(cv_results)
names.append(name)
msg = "%s: %f (%f)" % (name, cv_results.mean(), cv_results.std())
print(msg)
这是我的准确性
LR: 0.787555 (0.039036)
LDA: 0.780460 (0.039821)
KNN: 0.759916 (0.030417)
CART: 0.706669 (0.035827)
NB: 0.731637 (0.050813)
RF: 0.752054 (0.048660)
正如我在评论中提到的那样,您的大部分代码片段都是完全不相关的,应替换为简化的 runnable 示例.>
现在,如果您的问题是我有那些我可以得到分数"的对象,并且我想选择得分更高的对象",这很简单:将分数与对象一起存储,对这个基数进行排序得分,并保持得分最高的得分:
import random
def get_score(model):
# dumbed down example
return random.randint(1, 10)
class Model1(object):
pass
class Model2(object):
pass
class Model3(object):
pass
models = [Model1, Model2, Model3]
# build a list of (score, model) tuples
scores = [(get_score(model), model) for model in models]
# sort it on score
scores.sort(key=item[0])
# get the model with the best score, which is the
# the second element of the last item
best = scores[-1][1]
现在,请为自己和整个世界提供帮助:了解有关 ALL 相关信息和仅相关信息的清除问题
.https://stackoverflow.com/help/how-to-ask
https://stackoverflow.com/help/mcve
https://stackoverflow.com/help/tagging
Here is my code im building 6 models and i am getting accuracy in that, how do i choose that dynamically which accuracy is greater and i want to execute only that model which as highest accuracy.
"prepare configuration for cross validation test harness"
seed = 7
"prepare models"
models = []
models.append(('LR', LogisticRegression()))
models.append(('LDA', LinearDiscriminantAnalysis()))
models.append(('KNN', KNeighborsClassifier()))
models.append(('CART', DecisionTreeClassifier()))
models.append(('NB', GaussianNB()))
models.append(('RF',RandomForestClassifier()))
#models.append(('SVM', SVC()))
"evaluate each model in turn"
results = []
names = []
scoring = 'accuracy'
for name, model in models:
kfold = model_selection.KFold(n_splits=10, random_state=seed)
cv_results = model_selection.cross_val_score(model, orginal_telecom_80p_test[features], orginal_telecom_80p_test["Churn"], cv=kfold, scoring=scoring)
results.append(cv_results)
names.append(name)
msg = "%s: %f (%f)" % (name, cv_results.mean(), cv_results.std())
print(msg)
This is my accuracy
LR: 0.787555 (0.039036)
LDA: 0.780460 (0.039821)
KNN: 0.759916 (0.030417)
CART: 0.706669 (0.035827)
NB: 0.731637 (0.050813)
RF: 0.752054 (0.048660)
As I mentionned in a comment, most of your snippet is totally irrelevant and should be replaced by a simplified runnable example.
Now if you question is "I have those objects for which I can get a 'score' and I want to select the one with the higher score", it's quite simple: store the scores along with the objects, sort this base on score and keep the one with the highest score:
import random
def get_score(model):
# dumbed down example
return random.randint(1, 10)
class Model1(object):
pass
class Model2(object):
pass
class Model3(object):
pass
models = [Model1, Model2, Model3]
# build a list of (score, model) tuples
scores = [(get_score(model), model) for model in models]
# sort it on score
scores.sort(key=item[0])
# get the model with the best score, which is the
# the second element of the last item
best = scores[-1][1]
Now please do yourself and the world a favour: LEARN TO ASK CLEAR QUESTIONS WITH ALL RELEVANT INFORMATIONS AND ONLY RELEVANT INFORMATIONS.
https://stackoverflow.com/help/how-to-ask
https://stackoverflow.com/help/mcve
https://stackoverflow.com/help/tagging
这篇关于如何使用python动态选择最佳模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!