<jupyter_start><jupyter_text>Objective:- learn to use `joblib` to serialize XGBoost model<jupyter_code>import gc
import os
import joblib
import numpy as np
import pandas as pd
from xgboost import XGBClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# read in data
df = pd.read_csv("../../data/iris.data", header=None)
# split into features and label
X, y = df.iloc[:, :-1].values, df.iloc[:, -1].values
# label encode string class values
label_encoder = LabelEncoder()
y = label_encoder.fit_transform(y)
# split into training and testing set
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.3, stratify=y, shuffle=True, random_state=89
)
# train the model
clf = XGBClassifier()
clf.fit(X_train, y_train)
# create output directory
output_dir = "../../output"
if not os.path.exists(output_dir):
    os.mkdir(output_dir)
model_dir = os.path.join(output_dir, "model")
if not os.path.exists(model_dir):
    os.mkdir(model_dir)
# save model
model_filepath = os.path.join(model_dir, "iris_xgb.joblib.dat")
joblib.dump(clf, model_filepath)

del clf
gc.collect()
# reload trained model and predict
clf = joblib.load(model_filepath)
preds_test = clf.predict(X_test)
acc_test = accuracy_score(y_test, preds_test)
print("[INFO] The test accuracy of XGBoost is: %.2f%%" % (acc_test * 100))<jupyter_output>[INFO] The test accuracy of XGBoost is: 95.56%
{'fraction_non_alphanumeric': 0.11512261580381472, 'fraction_numerical': 0.009536784741144414, 'mean_word_length': 5.528888888888889, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '231192', 'license': 'MIT', 'n_tokens_mistral': 533, 'n_tokens_neox': 522, 'n_words': 143, 'path': 'ipython/ii_intermediate/08_save_&_reload_trained_model_by_using_joblib.ipynb', 'repo_name': 'Genpeng/xgboost-examples', 'size': '1468'}