码迷,mamicode.com
首页 > Web开发 > 详细

LabelEncoder save 离线使用

时间:2019-05-14 13:18:34      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:out   sklearn   nbsp   import   object   dir   cti   dep   roc   

 

For me the easiest way was exporting LabelEncoder as .pkl file for each column. You have to export the encoder for each column after using the fit_transform() function

For example

from sklearn.preprocessing import LabelEncoder
import pickle
import pandas as pd
df_train = pd.read_csv(‘traing_data.csv‘)
le = LabelEncoder()    
df_train[‘Departure‘] = le.fit_transform(df_train[‘Departure‘])
#exporting the departure encoder
output = open(‘Departure_encoder.pkl‘, ‘wb‘)
pickle.dump(le, output)
output.close()

Then in the testing project, you can load the LabelEncoder object and apply transform() function directly

from sklearn.preprocessing import LabelEncoder
import pandas as pd
df_test = pd.read_csv(‘testing_data.csv‘)
#load the encoder file
import pickle 
pkl_file = open(‘Departure_encoder.pkl‘, ‘rb‘)
le_departure = pickle.load(pkl_file) 
pkl_file.close()
df_test[‘Departure‘] = le_departure.transform(df_test[‘Departure‘])

LabelEncoder save 离线使用

标签:out   sklearn   nbsp   import   object   dir   cti   dep   roc   

原文地址:https://www.cnblogs.com/bonelee/p/10861506.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!