ValueError: Number of features of the model must match the input. Model n_features is 464 and input n_features is 2
ValueError: Number of features of the model must match the input. Model n_features is 464 and input n_features is 2
我正在尝试预测香水的成本,但在“answer = (clf.predict(result))”行中出现错误
cursor.execute('SELECT * FROM info')
info = cursor.fetchall()
for line in info:
z.append(line[0:2])
y.append(line[2])
enc.fit(z)
x = enc.transform(z).toarray()
result = []
clf = tree.DecisionTreeClassifier()
clf = clf.fit(x, y)
new = input('enter the Model, Volume of your perfume to see the cost: example = Midnighto,50 ').split(',')
enc.fit([new])
result = enc.transform([new]).toarray()
answer = (clf.predict(result))
print(answer)
您不必再次用新输入拟合 enc
,只需使用适合 X 的函数进行变换(您所做的是 OneHotEncoding 新样本,仅考虑该特征的一个可能值,您必须考虑 X 数据中所有可能的特征类别)。所以删除下一行:
enc.fit([new])
之后,请检查 X 和结果是否具有相同数量的特征。您可以使用函数 shape
.
此外,我建议您使用训练和测试数据来查看您的模型是否过拟合。然后,您可以应用您的个人预测。
我正在尝试预测香水的成本,但在“answer = (clf.predict(result))”行中出现错误
cursor.execute('SELECT * FROM info')
info = cursor.fetchall()
for line in info:
z.append(line[0:2])
y.append(line[2])
enc.fit(z)
x = enc.transform(z).toarray()
result = []
clf = tree.DecisionTreeClassifier()
clf = clf.fit(x, y)
new = input('enter the Model, Volume of your perfume to see the cost: example = Midnighto,50 ').split(',')
enc.fit([new])
result = enc.transform([new]).toarray()
answer = (clf.predict(result))
print(answer)
您不必再次用新输入拟合 enc
,只需使用适合 X 的函数进行变换(您所做的是 OneHotEncoding 新样本,仅考虑该特征的一个可能值,您必须考虑 X 数据中所有可能的特征类别)。所以删除下一行:
enc.fit([new])
之后,请检查 X 和结果是否具有相同数量的特征。您可以使用函数 shape
.
此外,我建议您使用训练和测试数据来查看您的模型是否过拟合。然后,您可以应用您的个人预测。