onehotencoder应用于训练数据如何通过测试数据得到模型预测

How to pass test data to obtain model predictions if onehotencoder applied to train data

我正在使用 Sklearn.preprocessing 预处理 (onehotencoder) 分类数据。

onehotencoder = OneHotEncoder()
pre_loc_data1 = onehotencoder.fit_transform(pre_loc_data1.astype(str)).toarray()
print(pre_loc_data1)

X_train, X_test, y_train, y_test = train_test_split(pre_loc_data1, pre_loc_target, test_size=0.2)

此处 X-train 现在是编码数据。 如果我将 y_train 数据提供给预测模型,它工作正常。 因为它也是编码数据。 但我想使用单个记录作为模型的输入来预测而不像下面这样编码

(clf.predict(['Hyderabad / Secunderabad','0 Year(s) 8 Month(s)','android','java']))

如何将此类数据作为输入模型进行测试。

提前致谢!

您需要将 onehotencoder 应用于输入(假设 clf 是您训练的模型):

clf.predict(onehotencoder.transform([['Hyderabad / Secunderabad','0 Year(s) 8 Month(s)','android','java']]))