gpt4 book ai didi

python - 值错误: y contains non binary labels for VotingClassifier with my GradientBoostingClassifier

转载 作者:行者123 更新时间:2023-11-30 09:11:28 25 4
gpt4 key购买 nike

您好,我正在尝试将 VotingClassifier 与我的 GradientBoostingClassifier 一起使用,我在 GradientBoostingClassifier 周围放置了一个包装器,以便利用sample_weight。但是,我收到以下错误,并且不知道如何修复它。

代码:

class MyGradientBoostingClassifier(GradientBoostingClassifier):
def fit(self, X , y=None):
return super(GradientBoostingClassifier, self).fit(X, y, sample_weight=y)


rf = RandomForestClassifier(n_jobs=-1)
mygb = MyGradientBoostingClassifier()

vc = VotingClassifier(estimators=[('rf', rf), ('mygb', mygb)],
voting='soft',
weights=[1,2])

mygb.fit(X5, y5)

y 的样本为 [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 1. 0. 0. 0. 0.] 它是 np 数组

错误:

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-62-c56d4cac146f> in <module>()
13 weights=[1,2])
14
---> 15 mygb.fit(X5, y5)

<ipython-input-62-c56d4cac146f> in fit(self, X, y)
3 print np.shape(y), np.shape(X), Counter(y), type(y)
4 print y[:20]
----> 5 return super(GradientBoostingClassifier, self).fit(X, y, sample_weight=y)
6
7

/Users/a/anaconda/lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyc in fit(self, X, y, sample_weight, monitor)
987
988 # fit initial model - FIXME make sample_weight optional
--> 989 self.init_.fit(X, y, sample_weight)
990
991 # init predictions

/Users/a/anaconda/lib/python2.7/site-packages/sklearn/ensemble/gradient_boosting.pyc in fit(self, X, y, sample_weight)
117
118 if neg == 0 or pos == 0:
--> 119 raise ValueError('y contains non binary labels.')
120 self.prior = self.scale * np.log(pos / neg)
121

ValueError: y contains non binary labels.

最佳答案

对于分类模型,y 预计为整数类标签(0 和 1),因此将其用作分类目标和样本权重是没有意义的。

模型会忽略所有权重为 0 的样本,并且不可能仅使用来自训练集的同一单类的样本来训练二元分类模型。

关于python - 值错误: y contains non binary labels for VotingClassifier with my GradientBoostingClassifier,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35570160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com