【问题标题】:ValueError: Found input variables with inconsistent numbers of samples: [11097, 1233]ValueError:发现样本数量不一致的输入变量:[11097, 1233]
【发布时间】:2020-05-18 13:01:32
【问题描述】:

我正在尝试创建一个交易网站并使用 sklearn 在 python 3.6 中识别交易模式。 我像这样从网站上获取数据:

buyer = "FORM54"
getBuyer = requests.get("https://url.com/buyer=%s/" % buyer)

然后使用 pandas 获取我需要的数据:

data = pd.read_json(StringIO(getBuyer.text))
data = data[["strike_price", "underlying_price", "notional_amount", "quantity"]]

并尝试像这样预测交易数量:

predict = "quantity"
X = np.array(data.drop([predict],1))
y = np.array(data[predict])

x_train, y_train, x_test, y_test = sklearn.model_selection.train_test_split(X, y, test_size =  0.1)

但是我得到了这个错误:

Traceback (most recent call last):
  File "C:/Users/HP Omen/PycharmProjects/untitled2/main.py", line 31, in <module>
    model.fit(x_train, y_train)
  File "C:\Users\HP Omen\PycharmProjects\untitled2\venv\lib\site-packages\sklearn\neighbors\_base.py", line 1130, in fit
    X, y = check_X_y(X, y, "csr", multi_output=True)
  File "C:\Users\HP Omen\PycharmProjects\untitled2\venv\lib\site-packages\sklearn\utils\validation.py", line 765, in check_X_y
    check_consistent_length(X, y)
  File "C:\Users\HP Omen\PycharmProjects\untitled2\venv\lib\site-packages\sklearn\utils\validation.py", line 212, in check_consistent_length
    " samples: %r" % [int(l) for l in lengths])
ValueError: Found input variables with inconsistent numbers of samples: [11097, 1233]

这是 X 和 y 形状:

>>> X.shape
(12330, 3)
>>> y.shape
(12330,)

但是当buyer 改变时它会改变

【问题讨论】:

  • 您需要显示完整的代码,包括产生错误的行

标签: python pandas scikit-learn python-3.6


【解决方案1】:

试试:

x_train, x_test, y_train, y_test = sklearn.model_selection.train_test_split(X, y, test_size =  0.1)

看看它是否能解决你的问题。

【讨论】:

  • 查看 train_test_split 的文档。正确的顺序是x_train, x_test, y_train, y_test
猜你喜欢
  • 2021-06-20
  • 2018-06-25
  • 2018-12-04
  • 2021-05-31
  • 2020-08-19
  • 2021-09-12
  • 2021-08-06
  • 2020-06-07
  • 2022-01-16
相关资源
最近更新 更多