考虑到这个讨论,我仍然无法理解如何知道模型期望输入必须是某种格式的原因。
根据错误消息,你的输入数据格式是 [45000, 50000, 60000, …]。但是模型期望的输入格式是像 [[45000], [50000], [60000], …] – 一个列表的列表。因此,reshape(-1, 1) 只是改变了格式。
为什么 y 不需要是二维数组?为什么只有 X 必须是?
回答:
从sklearn.svm.SVC
的文档中,对于predict
方法:
参数
X: {array-like, sparse matrix} of shape (n_samples, n_features) or (n_samples_test, n_samples_train)
对于 kernel=”precomputed”,X 的预期形状是 (n_samples_test, n_samples_train)。
因此,即使你只有一个值X
需要预测,你的输入形状也需要是(1, n_features)
,因为你的nsamples
等于1
,而不是(n_features)
,这是不被支持的格式。