为什么AdaBoost不能与DecisionTree一起工作?

2024-05-28 20:09:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我将sklearn0.19.1与DecisionTree和AdaBoost一起使用。你知道吗

我有一个DecisionTree分类器,可以很好地工作:

clf = tree.DecisionTreeClassifier()

train_split_perc = 10000
test_split_perc = pdf.shape[0] - train_split_perc

train_pdf_x = pdf[:train_split_perc]
train_pdf_y = YY[:train_split_perc]

test_pdf_x = pdf[-test_split_perc:]
test_pdf_y = YY[-test_split_perc:]

clf.fit(train_pdf_x, train_pdf_y)

pred2 = clf.predict(test_pdf_x)

但是当尝试添加AdaBoost时,它会在predict函数上抛出一个错误:

treeclf = tree.DecisionTreeClassifier(max_depth=3)
adaclf = AdaBoostClassifier(base_estimator=treeclf, n_estimators=500, learning_rate=0.5)

train_split_perc = 10000
test_split_perc = pdf.shape[0] - train_split_perc

train_pdf_x = pdf[:train_split_perc]
train_pdf_y = YY[:train_split_perc]

test_pdf_x = pdf[-test_split_perc:]
test_pdf_y = YY[-test_split_perc:]

adaclf.fit(train_pdf_x, train_pdf_y)

pred2 = adaclf.predict(test_pdf_x)

具体来说,错误是:

ValueError: bad input shape (236821, 6)

它似乎指向的数据集是train_pdf_y,因为它的形状是(236821, 6),我不明白为什么。你知道吗

从AdaBoostClassifierin the docs的描述中,我可以理解使用数据的实际分类器是DecisionTree:

An AdaBoost 1 classifier is a meta-estimator that begins by fitting a classifier on the original dataset and then fits additional copies of the classifier on the same dataset but where the weights of incorrectly classified instances are adjusted such that subsequent classifiers focus more on difficult cases

但我还是犯了这个错误。你知道吗

code examples I've found中,即使在sklearn的网站上学习如何使用AdaBoost,我也不能理解我做错了什么。你知道吗

感谢您的帮助。你知道吗


Tags: thetestpdf错误trainpredictsplitclf
1条回答
网友
1楼 · 发布于 2024-05-28 20:09:27

如果给定y的形状,看起来您正在尝试执行Multi-Output classification problem,否则,您正在从yadaclf.fit(train_pdf_x, train_pdf_y)进行n维馈送是没有意义的。你知道吗

假设是这样,问题是sciket Learn的DecisionTreeClassifier确实支持多输出问题,这就是y输入的形状[n_samples, n_outputs]。但是AdaBoostClassifier的情况并非如此,因为根据文档,标签必须是:

y : array-like of shape = [n_samples]

相关问题 更多 >

    热门问题