Python 如何在经过训练的SVR模型上进行预测并解决误差值误差:X.shape[1]=1应等于22
我有超过2000行和23列的数据集,包括年龄列。我已经完成了SVR的所有流程。现在我想预测经过训练的SVR模型,我需要在模型中输入X_检验吗?我们遇到了一个错误,那就是Python 如何在经过训练的SVR模型上进行预测并解决误差值误差:X.shape[1]=1应等于22,python,machine-learning,scikit-learn,Python,Machine Learning,Scikit Learn,我有超过2000行和23列的数据集,包括年龄列。我已经完成了SVR的所有流程。现在我想预测经过训练的SVR模型,我需要在模型中输入X_检验吗?我们遇到了一个错误,那就是 ValueError: X.shape[1] = 1 should be equal to 22, the number of features at training time 我如何解决这个问题?如何编写代码,以便在经过训练的SVR模型上进行预测 import pandas as pd import numpy as np
ValueError: X.shape[1] = 1 should be equal to 22, the number of features at training time
我如何解决这个问题?如何编写代码,以便在经过训练的SVR模型上进行预测
import pandas as pd
import numpy as np
# Make fake dataset
dataset = pd.DataFrame(data= np.random.rand(2000,22))
dataset['age'] = np.random.randint(2, size=2000)
# Separate the target from the other features
target = dataset['age']
data = dataset.drop('age', axis = 1)
X_train, y_train = data.loc[:1000], target.loc[:1000]
X_test, y_test = data.loc[1001], target.loc[1001]
X_test = np.array(X_test).reshape((len(X_test), 1))
print(X_test.shape)
SupportVectorRefModel = SVR()
SupportVectorRefModel.fit(X_train, y_train)
y_pred = SupportVectorRefModel.predict(X_test)
输出:
ValueError: X.shape[1] = 1 should be equal to 22, the number of features at training time
您对X_测试的重塑不正确;应该是:
X_test = np.array(X_test).reshape(1, -1)
print(X_test.shape)
# (1, 22)
通过该更改,其余代码运行正常:
y_pred = SupportVectorRefModel.predict(X_test)
y_pred
# array([0.90156667])
更新
在代码中显示的情况下,显然X_测试由一个样本组成,如下所述:
X_test, y_test = data.loc[1001], target.loc[1001]
但是,如果我怀疑这不是您真正想要的,但实际上您想要其余的数据作为测试集,那么您应该将定义更改为:
X_test, y_test = data.loc[1001:], target.loc[1001:]
X_test.shape
# (999, 22)
而且没有任何重塑
y_pred = SupportVectorRefModel.predict(X_test)
y_pred.shape
# (999,)
i、 e.999预测的y_pred。在我将其编辑为正确的22之前,这是一个与错误消息中错误的57号完全相同的问题。请帮助我解决此问题