"Python Sklearn交叉验证/标签数量与样本数量不匹配"

2024-05-15 14:53:20 发布

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

做一个机器学习的课程,我想把数据分成训练集和测试集。我想把它分开,用Decisiontree进行训练,然后打印出我的测试集的分数。给出了代码中的交叉验证参数。有人看到我做错什么了吗?在

我得到的错误是:

Traceback (most recent call last):
  File "/home/stephan/ud120-projects/validation/validate_poi.py", line 36, in <module>
    clf = clf.fit(features_train, labels_train)
  File "/home/stephan/.local/lib/python2.7/site-packages/sklearn/tree/tree.py", line 221, in fit
    "number of samples=%d" % (len(y), n_samples))
ValueError: Number of labels=29 does not match number of samples=66

这是我的代码:

^{pr2}$

Tags: of代码inpytreenumberhomelabels
2条回答

您需要通过列车分割功能中的测试_size=0.5

train_test_split(...,test_size=0.5,...)

您的变量似乎与^{}的返回模式不匹配

尝试:

features_train, features_test, labels_train, labels_test = ...

相关问题 更多 >