Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading xgboost model trained in python to scala

I have trained a model using sklearn api and saved the model

from xgboost.sklearn import XGBClassifier
model = XGBClassifier()
model.fit(X_train, y_train)
with open(path + '.pkl', 'wb') as file:
    cPickle.dump(model, file)

Is there a way to load it with scala API?

If I train it using the python API (not the sklearn API) and save it using the:

model.save_model("trained_model.model")

then I can load it with scala using:

import ml.dmlc.xgboost4j.scala.XGBoost
val model = XGBoost.loadModel("trained_model.model")

Is there a similar way for loading a pkl formated model in scala?

Or alternatively is it possible to convert a sklearned-api-xgboost-model to a python-api-model? and after the python-api-model can be loaded using the above code.

Any insights?

like image 685
Mpizos Dimitris Avatar asked Oct 28 '25 04:10

Mpizos Dimitris


1 Answers

For those who come up to this question, the solution is:

When you train a model using the sklearn API you can save as:

model._Booster.save_model("trained_model.model")

save_model is used in python API.

Later this model can be loaded in scala API as described in the question:

val model = XGBoost.loadModel("trained_model.model")
like image 175
Mpizos Dimitris Avatar answered Oct 29 '25 18:10

Mpizos Dimitris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!