han*_*zgs 5 python pipeline scikit-learn
如何使用自定义函数创建 sklearn 管道?\n我有两个函数,一个用于清理数据,第二个用于构建模型。
\n\ndef preprocess(df):\n \xe2\x80\xa6\xe2\x80\xa6\xe2\x80\xa6\xe2\x80\xa6\xe2\x80\xa6\xe2\x80\xa6.\n # clean data\n return df_clean\n\ndef model(df_clean):\n \xe2\x80\xa6\xe2\x80\xa6\xe2\x80\xa6\xe2\x80\xa6\xe2\x80\xa6\xe2\x80\xa6\xe2\x80\xa6\n #split data train and test and build randomForest Model\n return model\nRun Code Online (Sandbox Code Playgroud)\n\n所以我使用 FunctionTransformer 并创建了管道
\n\nfrom sklearn.pipeline import Pipeline, make_pipeline\nfrom sklearn.preprocessing import FunctionTransformer\n\npipe = Pipeline([("preprocess", FunctionTransformer(preprocess)),("model",FunctionTransformer(model))])\n\npred = pipe.predict_proba(new_test_data)\nprint(pred)\nRun Code Online (Sandbox Code Playgroud)\n\n我知道上面是错误的,不知道如何处理,在管道中我需要先传递训练数据,然后我必须传递 new_test_data ?
\n你需要创建自己的类继承sklearn的BaseEstimator、TransformerMixin。
然后在您自己的类的 fit/transform/fit_transform / Predict/predict_prob 等函数中指定您的函数。
| 归档时间: |
|
| 查看次数: |
4278 次 |
| 最近记录: |