首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在字典中使用括号更新值

在字典中使用括号更新值
EN

Stack Overflow用户
提问于 2019-03-04 18:59:21
回答 2查看 94关注 0票数 -1

我正在尝试使用for循环替换字典中的值。但这有点特殊,因为它在括号内有值。

我的问题是:如何更新字典括号中的值?

需要

  1. 使用模型(例如,RandomForestClassifier).

BaggingClassifier )相应地更新分类器的BaggingClassifier

Initialization

n_estimator = [5, 10, 20]

models = {'Bagging': BaggingClassifier(random_state=12345),
          'RandomForest': RandomForestClassifier(random_state=12345)
         }

循环

for x in list(models):
    for y in n_estimator:
       models[x] = x + Classifier(random_state=12345, n_estimators=y); 
    print(x)

当前结果

Bagging
RandomForest

BaggingClassifier(base_estimator=None, bootstrap=True,
         bootstrap_features=False, max_features=1.0, max_samples=1.0,
         n_estimators=10, n_jobs=1, oob_score=False, random_state=12345,
         verbose=0, warm_start=False)

似乎我做错了,因为我添加了一个新的值,而不是更新当前的值。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-03-05 09:16:52

我猜你完全可以把字典扔掉。以下是创建具有不同参数的不同分类器实例的一种可能方法:

from sklearn.ensemble import RandomForestClassifier, BaggingClassifier

for model in [RandomForestClassifier, BaggingClassifier]:
    for n in [5, 10, 20]:
        clf = model(random_state=12345, n_estimators=n)
        print(clf)

上面的代码产生了:

RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',
            max_depth=None, max_features='auto', max_leaf_nodes=None,
            min_impurity_decrease=0.0, min_impurity_split=None,
            min_samples_leaf=1, min_samples_split=2,
            min_weight_fraction_leaf=0.0, n_estimators=5, n_jobs=1,
            oob_score=False, random_state=12345, verbose=0,
            warm_start=False)
RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',
            max_depth=None, max_features='auto', max_leaf_nodes=None,
            min_impurity_decrease=0.0, min_impurity_split=None,
            min_samples_leaf=1, min_samples_split=2,
            min_weight_fraction_leaf=0.0, n_estimators=10, n_jobs=1,
            oob_score=False, random_state=12345, verbose=0,
            warm_start=False)
RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',
            max_depth=None, max_features='auto', max_leaf_nodes=None,
            min_impurity_decrease=0.0, min_impurity_split=None,
            min_samples_leaf=1, min_samples_split=2,
            min_weight_fraction_leaf=0.0, n_estimators=20, n_jobs=1,
            oob_score=False, random_state=12345, verbose=0,
            warm_start=False)
BaggingClassifier(base_estimator=None, bootstrap=True,
         bootstrap_features=False, max_features=1.0, max_samples=1.0,
         n_estimators=5, n_jobs=1, oob_score=False, random_state=12345,
         verbose=0, warm_start=False)
BaggingClassifier(base_estimator=None, bootstrap=True,
         bootstrap_features=False, max_features=1.0, max_samples=1.0,
         n_estimators=10, n_jobs=1, oob_score=False, random_state=12345,
         verbose=0, warm_start=False)
BaggingClassifier(base_estimator=None, bootstrap=True,
         bootstrap_features=False, max_features=1.0, max_samples=1.0,
         n_estimators=20, n_jobs=1, oob_score=False, random_state=12345,
         verbose=0, warm_start=False)
票数 1
EN

Stack Overflow用户

发布于 2019-03-04 20:04:05

看起来你附加了新的价值观。对于更新,您必须使用数组索引赋值

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54981849

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档