我正在尝试使用for循环替换字典中的值。但这有点特殊,因为它在括号内有值。
我的问题是:如何更新字典括号中的值?
需要
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)
似乎我做错了,因为我添加了一个新的值,而不是更新当前的值。
发布于 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)
发布于 2019-03-04 20:04:05
看起来你附加了新的价值观。对于更新,您必须使用数组索引赋值
https://stackoverflow.com/questions/54981849
复制相似问题