Scikit learn 将xgboost.Booster的实例转换为实现scikit学习API的模型

Scikit learn 将xgboost.Booster的实例转换为实现scikit学习API的模型,scikit-learn,save,xgboost,mlflow,xgbclassifier,Scikit Learn,Save,Xgboost,Mlflow,Xgbclassifier,我正在尝试使用mlflow保存模型,然后稍后加载以进行预测 我正在使用xgboost.XGBRegressor模型及其sklearn函数.predict()和.predict_proba()进行预测,但结果是mlflow不支持实现sklearn API的模型,因此稍后从mlflow加载模型时,mlflow返回xgboost.Booster的实例,它不实现.predict()或.predict\u proba()函数 有没有办法将xgboost.Booster转换回实现sklearn API函数的

我正在尝试使用
mlflow
保存模型,然后稍后加载以进行预测

我正在使用
xgboost.XGBRegressor
模型及其sklearn函数
.predict()
.predict_proba()
进行预测,但结果是
mlflow
不支持实现sklearn API的模型,因此稍后从mlflow加载模型时,mlflow返回
xgboost.Booster
的实例,它不实现
.predict()
.predict\u proba()
函数


有没有办法将
xgboost.Booster
转换回实现sklearn API函数的
xgboost.sklearn.XGBRegressor
对象?

您是否尝试过在自定义类中封装模型,使用
mlflow.pyfunc.PythonModel
记录并加载它? 我举了一个简单的例子,加载回模型后,它正确地将
显示为一个类型

例如:

import xgboost as xgb
xg_reg = xgb.XGBRegressor(...)

class CustomModel(mlflow.pyfunc.PythonModel):
    def __init__(self, xgbRegressor):
        self.xgbRegressor = xgbRegressor

    def predict(self, context, input_data):
        print(type(self.xgbRegressor))
        
        return self.xgbRegressor.predict(input_data)

# Log model to local directory
with mlflow.start_run():
     custom_model = CustomModel(xg_reg)
     mlflow.pyfunc.log_model("custome_model", python_model=custom_model)


# Load model back
from mlflow.pyfunc import load_model
model = load_model("/mlruns/0/../artifacts/custome_model")
model.predict(X_test)
输出:

<class 'xgboost.sklearn.XGBRegressor'>
[ 9.107417 ]

[ 9.107417 ]