【问题标题】:Update values with brackets in a dictionary用括号更新字典中的值
【发布时间】:2019-03-04 10:59:21
【问题描述】:

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

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

需要

  1. 相应地更新n_estimator

  2. 使用模型(例如 BaggingClassifierRandomForestClassifier)相应地更新分类器。

初始化

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)

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

【问题讨论】:

    标签: python machine-learning scikit-learn jupyter-notebook


    【解决方案1】:

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

    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)
    

    【讨论】:

      【解决方案2】:

      您似乎添加了新值。对于更新,您必须使用索引数组分配值

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-28
        • 2021-06-16
        相关资源
        最近更新 更多