为决策树分类器传递参数时出错



我正在尝试使用字符串中的参数的决策树分类器。

print d    # d= 'max_depth=100'
clf = DecisionTreeClassifier(d)
clf.fit(X[:3000,], labels[:3000])

在这种情况下,我得到以下错误。如果我使用clf = DecisionTreeClassifier(max_depth=100)它可以正常工作。

Traceback (most recent call last):
File "train.py", line 120, in <module>
grid_search_generalized(X, labels, {"max_depth":[i for i in range(100, 200)]})
File "train.py", line 51, in grid_search_generalized
clf.fit(X[:3000,], labels[:3000])
File "/usr/local/lib/python2.7/dist-packages/sklearn/tree/tree.py", line 790, in fit
X_idx_sorted=X_idx_sorted)
File "/usr/local/lib/python2.7/dist-packages/sklearn/tree/tree.py", line 326, in fit
criterion = CRITERIA_CLF[self.criterion](self.n_outputs_,
KeyError: 'max_depth=100'

您将参数作为字符串对象传递,而不是作为可选参数传递。
如果确实必须使用此字符串调用构造函数,则可以使用以下代码:

arg = dict([d.split("=")])
clf = DecisionTreeClassifier(**arg)

您可以在此链接
中阅读有关参数解压缩的更多信息 将字典作为关键字参数传递给 python 中的函数

关键字变量参数尚未在 DecisionTreeClassifier 函数中定义.max_depth 可以作为关键字参数传递。请尝试以下代码:

d= 'max_depth=100'
arg = dict([d.split("=")])
i = int(next(iter(arg.values())))
k = next(iter(arg.keys()))
clf = DecisionTreeClassifier(max_depth=args['max_depth'])
clf.fit(X[:3000,], labels[:3000])

输出:

DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=100,
max_features=None, 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, presort=False,
random_state=None, splitter='best')

最新更新