What does ValueError: Found input variables with inconsistent numbers of samples: [75, 1] signify in Python?

What does ValueError: Found input variables with inconsistent numbers of samples: [75, 1] signify in Python?

我正在浏览 Youtube 上的 "Writing Our First Classifier - Machine Learning Recipes #5" 机器学习视频。我跟着这个例子,但我不确定为什么我无法获得代码 运行.

注意:这不是 KNN 分类器的最终代码。这是初始测试阶段。

#implementing KNN Classifier without using import statement
import random

class ScrappyKNN():
    def fit(self, X_train, Y_train):
        self.X_train=X_train
        self.Y_train=Y_train

    def predict(self, X_test):
        predictions=[]
        for row in X_test:
            label = random.choice(self.Y_train)
            predictions.append(label)

            return predictions

from sklearn.datasets import load_iris
iris=load_iris()
X=iris.data 
Y=iris.target 

from sklearn.cross_validation import train_test_split

X_train,X_test,Y_train,Y_test=train_test_split(X,Y,test_size=0.5)

clf=ScrappyKNN()
clf.fit (X_train,Y_train)
predictions_result=clf.predict(X_test)

from sklearn.metrics import accuracy_score

print(accuracy_score(Y_test,predictions_result))

我收到错误 "ValueError: Found input variables with inconsistent numbers of samples: [75, 1]"。我认为列表中存在一些大小不一致,因为训练和测试数据集被从 150 个样本中拆分为每个样本 75 个(我使用了 test_size=0.5)。我真的深陷其中。你能告诉我这个错误是什么意思吗?我在堆栈溢出上搜索了类似的答案,但不幸的是,无法找出导致此错误的原因。我刚开始使用 Python 机器 Learning.Can 有人可以帮助我吗?

这是完整的 Stacktrace

/Users/joyjitchatterjee/anaconda3/envs/machinelearning/bin/python /Users/joyjitchatterjee/PycharmProjects/untitled1/ml_5.py
Traceback (most recent call last):
  File "/Users/joyjitchatterjee/PycharmProjects/untitled1/ml_5.py", line 36, in <module>
    print(accuracy_score(Y_test,predictions_result))
  File "/Users/joyjitchatterjee/anaconda3/envs/machinelearning/lib/python3.6/site-packages/sklearn/metrics/classification.py", line 176, in accuracy_score
    y_type, y_true, y_pred = _check_targets(y_true, y_pred)
  File "/Users/joyjitchatterjee/anaconda3/envs/machinelearning/lib/python3.6/site-packages/sklearn/metrics/classification.py", line 71, in _check_targets
    check_consistent_length(y_true, y_pred)
  File "/Users/joyjitchatterjee/anaconda3/envs/machinelearning/lib/python3.6/site-packages/sklearn/utils/validation.py", line 173, in check_consistent_length
    " samples: %r" % [int(l) for l in lengths])
ValueError: Found input variables with inconsistent numbers of samples: [75, 4]

Process finished with exit code 1

Screenshot of code

最后一个 return 语句的缩进在您的代码中是错误的。应该是

def predict(self, X_test):
    predictions=[]
    for row in X_test:
        label = random.choice(self.Y_train)
        predictions.append(label)

    return predictions