如何使用 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 示例。
现在如果你的问题是 "I have those objects for which I can get a 'score' and I want to select the one with the higher 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]
现在请帮自己和世界一个忙:学会用所有相关信息和清除问题仅 相关信息。
https://whosebug.com/help/how-to-ask
这是我构建 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 示例。
现在如果你的问题是 "I have those objects for which I can get a 'score' and I want to select the one with the higher 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]
现在请帮自己和世界一个忙:学会用所有相关信息和清除问题仅 相关信息。
https://whosebug.com/help/how-to-ask