python 想要通过网页 flask 从用户输入预测产品部门的销售量

q7solyqu  于 2023-05-16  发布在  Python
关注(0)|答案(1)|浏览(116)

我已经应用了一个热编码,然后决策树,并得到了一个很好的分数为我的部门和数量列。
当我这样做:

prediction = dt_clf.predict([[0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]])
print(prediction)

我得到以下输出:

ValueError: could not convert string to float: 'Beef'

但是我想把它转换成pickle并部署到pythonflask,这样用户就可以给予并检索预测。
我必须做一个热编码再次如果是这样请纠正这段代码为我:

from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import train_test_split

# Split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=10)

# Train a decision tree regression model with the best hyperparameters found by grid search
dt_clf = DecisionTreeRegressor(criterion='friedman_mse', splitter='random')
dt_clf.fit(X_train, y_train)

# Evaluate the model on the test set
dt_score = dt_clf.score(X_test, y_test)
print("Decision Tree Regression Test Score:", dt_score)`

我希望用户通过网页给予他们的输入来预测。有什么反馈吗?

kr98yfug

kr98yfug1#

您需要确保数据格式在训练、评估和测试中保持一致。对于one-hot-encoding,您需要在训练集上调用fit_transform()编码器,然后只在测试中调用transform()
参见here以获取官方文档。您需要决定如何处理未知数(即,如果测试集包含训练集中看不到的标签)。一个简单的例子改编自文档:

from sklearn.preprocessing import OneHotEncoder

encoder = OneHotEncoder(handle_unknown = "ignore")
x_train = encoder.fit_transform(x_train)
x_test = encoder.transform(x_test)

相关问题