2

I have build a machine learning model using xgboost.XGBClassifier in python. I am using pickle to serialize the model right now but would like to have a json file of this model, too. How can I do it?

So, basically I have this code :

from xgboost import XGBClassifier
import pickle
import pandas as pd

# read data
df = pd.read_csv('data.csv', sep='\t')

# features and target : 
X = df.drop(columns=['target'],axis=1)
Y = df['target']

# split the data
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.3, random_state=0)

model = XGBClassifier().fit(X_train,Y_train)

pickle_out = open("pickle_file.pkl","wb")
pickle.dump(model, pickle_out)
pickle_out.close()

Nora_F
  • 431
  • 5
  • 17

1 Answers1

2

It's actually supported out of the box (as an experimental feature) from 1.0.0 onwards. See https://xgboost.readthedocs.io/en/latest/tutorials/saving_model.html

It's simply using a json as the extension for the output file.

model.save_model('model_file_name.json')

You may want to save hyperparameters in a seperate json file.

user787267
  • 2,550
  • 1
  • 23
  • 32