【发布时间】:2021-08-24 17:57:47
【问题描述】:
我在一个名为 build.py 的文件中实现了以下 scikit-learn 管道,后来,成功地对其进行了腌制。
preprocessor = ColumnTransformer(transformers=[
('target', TargetEncoder(), COL_TO_TARGET),
('one_hot', OneHotEncoder(drop_invariant=False, handle_missing='value',
handle_unknown='value', return_df=True, use_cat_names=True,
verbose=0), COL_TO_DUM),
('construction', OrdinalEncoder(mapping=mapping),['ConstructionPeriod'])
], remainder='passthrough')
test_pipeline = Pipeline(steps=[
('preprocessor', preprocessor),
('std_scale', StandardScaler()),
('XGB_model',
xgb.XGBRegressor(
booster = 'gbtree', colsample_bylevel=0.75,colsample_bytree=0.75,
max_depth = 20,grow_policy = 'depthwise',learning_rate = 0.1
)
)
])
test_pipeline.fit(X_train, y_train)
import pickle
pickle.dump(open('final_pipeline.pkl','wb'), test_pipeline)
然后在另一个文件 app.py 中读取腌制管道,该文件接受用户数据以通过未腌制管道进行预测。
pipeline = pickle.load(open('final_pipeline.pkl', 'rb'))
# data is the coming from the user via frontend
input_df = pd.DataFrame(data.dict(), index=[0])
# using the pipeline to predict
prediction = pipeline.predict(input_df)
我遇到的挑战是未腌制的管道期望传入的测试数据具有类似于用于训练管道 (X_train) 的列结构。
为了解决这个问题,我需要对传入的测试数据列进行排序以匹配 X_train 的列。
- 肮脏的解决方案,将 X_train 列名称导出到文件中,然后在
app.py中读取它以重新排列传入测试数据的列。
关于如何以python方式解决此问题的任何建议?
【问题讨论】:
标签: python scikit-learn scikit-learn-pipeline