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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值