gpt4 book ai didi

python - KNN : Found input variables with inconsistent numbers of samples: [20, 499]

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

完整重述如下:https://repl.it/@JacksonEnnis/KNNPercentage

我正在尝试使用 sci-kit learn 中的 KNN 工具进行一些预测。

我有两个函数,recurse()和predict()。 recurse() 旨在迭代每个可能的功能组合,而 Predict 则应该执行实际的操作

  def predict(self, data, answers):
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split as tts

import numpy as np
if len(data) > 1:

print("length before transposition {}".format(len(data)))
#n_data = np.transpose(data)

#print("length after transposition {}".format(len(n_data)))
knn = KNeighborsClassifier(n_neighbors=1)
xTrain, xTest, yTrain, yTest = tts(data, answers)
print("xTrain data: {}".format(len(xTrain)))
knn.fit(xTrain, yTrain)
print(knn.score(xTest, yTest))

def recurse(self, data):
self.predict(data, self.y)
if len(data) > 0:
self.recurse(self.rLeft(data))
if len(data) > 1:
self.recurse(self.rMid(data))
if len(data) > 2:
self.recurse(self.rRight(data))

但是,当我运行该程序时,它指出训练/测试线有问题。我检查了每个功能中的示例以及答案,发现它们的长度都是相同的,所以我不确定为什么会发生这种情况。

Traceback (most recent call last):
File "main.py", line 12, in <module>
best = Config(apple)
File "/home/runner/Config.py", line 13, in __init__
self.predict(self.features, self.y)
File "/home/runner/Config.py", line 45, in predict
xTrain, xTest, yTrain, yTest = tts(data, answers)
File "/home/runner/.local/lib/python3.6/site-packages/sklearn/model_selection/_split.py", line 2096, in train_test_split
arrays = indexable(*arrays)
File "/home/runner/.local/lib/python3.6/site-packages/sklearn/utils/validation.py", line 230, in indexable
check_consistent_length(*result)
File "/home/runner/.local/lib/python3.6/site-packages/sklearn/utils/validation.py", line 205, in check_consistent_length
" samples: %r" % [int(l) for l in lengths])
ValueError: Found input variables with inconsistent numbers of samples: [20, 499]

最佳答案

你的轴颠倒了。格式是,对于每个数组,array.shape[0] 的大小必须相同。我建议您查看scikit docs了解更多示例。

tts(np.array(data).T, answers)

关于python - KNN : Found input variables with inconsistent numbers of samples: [20, 499],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56811515/

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