首页 > 解决方案 > 如何对输入进行与 .pkl 文件相同的 LabelEncode 输入?

问题描述

假设我使用以下代码对我的数据集进行编码以创建机器学习模型:-

dataset = pd.read_csv('crop_production.csv')
from sklearn import preprocessing
le = preprocessing.LabelEncoder()
dataset = dataset.apply(le.fit_transform)

我将此模型保存为 .pkl 文件。

现在我想打电话

t = le_new.fit_transform(['Andaman and Nicobar Islands','NICOBARS',2000,'Kharif','Arecanut',1254])
# Predicting the Test set results
y_pred = regressor.predict([t])

我怎样才能在烧瓶中实现这一点,所以当我使用LabelEconder它时,它的编码与le

例子 -

le编码t0 427 3 1 2 2026

所以le_new也应该像这样编码它只是为了准确预测

标签: pythonscikit-learn

解决方案


那么我们可以做的不是 LabelEncoding 它是:-

数据集 = pd.read_csv('crop_production.csv')

from sklearn import preprocessing

# Replace categorical data with one-hot encoded data
features_df = pd.get_dummies(dataset, columns=['State_Name', 'District_Name' , 'Season', 'Crop'])
X = features_df.iloc[:, :-1].values
y = features_df.iloc[:, -1].values

推荐阅读