如何使用python动态选择最佳模型

问题描述 投票:0回答:1

这是我构建 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)
python models
1个回答
1
投票

如果您的问题是“我有那些可以获得‘分数’的对象,并且我想选择分数较高的对象”,那么这很简单:将分数与对象一起存储,根据分数进行排序,然后保留得分最高的那个:

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]
© www.soinside.com 2019 - 2024. All rights reserved.