【发布时间】:2013-07-20 13:11:24
【问题描述】:
我正在尝试使用带有决策树树桩的 AdaBoostClassifier 作为基本分类器。我注意到 AdaBoostClassifier 所做的权重调整一直给我 SAMME.R 和 SAMME 选项的错误。
以下是我正在做的事情的简要概述
def train_adaboost(features, labels):
uniqLabels = np.unique(labels)
allLearners = []
for targetLab in uniqLabels:
runs=[]
for rrr in xrange(10):
feats,labs = get_binary_sets(features, labels, targetLab)
baseClf = DecisionTreeClassifier(max_depth=1, min_samples_leaf=1)
baseClf.fit(feats, labs)
ada_real = AdaBoostClassifier( base_estimator=baseClf,
learning_rate=1,
n_estimators=20,
algorithm="SAMME")
runs.append(ada_real.fit(feats, labs))
allLearners.append(runs)
return allLearners
我查看了每个决策树分类器的拟合度,它们能够预测一些标签。 然而,当我使用这个基分类器查看 AdaBoostClassifier 时,我得到了关于权重提升算法的错误。
def compute_confidence(allLearners, dada, labbo):
for ii,thisLab in enumerate(allLearners):
for jj, thisLearner in enumerate(thisLab):
#accessing thisLearner's methods here
这些方法会出现如下错误:
ipdb> thisLearner.predict_proba(myData)
PATHTOPACKAGE/lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.py:727: RuntimeWarning: invalid value encountered in double_scalars
proba /= self.estimator_weights_.sum()
*** ValueError: 'axis' entry is out of bounds
ipdb> thisLearner.predict(myData)
PATHTOPACKAGE/lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.py:639: RuntimeWarning: invalid value encountered in double_scalars
pred /= self.estimator_weights_.sum()
*** IndexError: 0-d arrays can only use a single () or a list of newaxes (and a single ...) as an index
我为 adaboost 尝试了 SAMME.R 算法,但由于这个错误,我什至无法适应这种情况下的 adaboost
[...]
File "PATH/sklearn/ensemble/weight_boosting.py", line 388, in fit
return super(AdaBoostClassifier, self).fit(X, y, sample_weight)
File "PATH/sklearn/ensemble/weight_boosting.py", line 124, in fit
X_argsorted=X_argsorted)
File "PATH/sklearn/ensemble/weight_boosting.py", line 435, in _boost
X_argsorted=X_argsorted)
File "PATH/sklearn/ensemble/weight_boosting.py", line 498, in _boost_real
(estimator_weight < 0)))
ValueError: non-broadcastable output operand with shape (1000) doesn't match the broadcast shape (1000,1000)
数据的维度实际上与分类器所期望的格式兼容,无论是在使用 adaboost 之前还是在我尝试测试经过训练的分类器时。这些错误表明什么?
【问题讨论】:
-
您能根据类型(数组还是列表?)、dtype、形状来描述
myData是什么。 -
当然可以。在我收到这些错误的情况下,类型、dtype 和形状就像 myData(用作下面的专长)和实验室的这些。对于专长:dtype('float64'),
, (1000, 20) 对于实验室:dtype('int8'), , (1000, 1)。这个问题的解决方案是微不足道的(我在下面写了一个答案),我只是不熟悉 python 的数组形状。
标签: python machine-learning scikit-learn