I am trying to reuse a saved XGBClassifer that after training makes the following predictions via .predict_proba() method:
ID -- scores
1 -- 0.072253475
2 -- 0.165827038
3 -- 0.098182471
4 -- 0.148302316
However, after reloading the object after either pickling it or using Skleans Joblib module, the predictions are totally off even after using the exact same test set:
ID -- scores
1 -- 0.46986327
2 -- 0.63513994
3 -- 0.45958066
4 -- 0.8958819
This is the classifier:
XGBClassifier(base_score=0.5, booster='gbtree',colsample_bylevel=1,
colsample_bytree=0.8, gamma=1, learning_rate=0.01, max_delta_step=0,
max_depth=4, min_child_weight=1, missing=nan, n_estimators=1500,
n_jobs=-1, nthread=None, objective='binary:logistic',
random_state=777, reg_alpha=2, reg_lambda=1,
scale_pos_weight=0.971637216356233, seed=777, silent=True,
subsample=0.6, verbose=2)
I pickle the object using using two different methods, Joblib provided in sklearn package and the standard pickle.dump using these functions:
def save_as_pickled_object(obj, filepath):
import pickle
import os
import sys
"""
This is a defensive way to write pickle.write, allowing for very large files on all platforms
"""
max_bytes = 2**31 - 1
"""
Adding protocol = 4 as an argument to pickle.dumps because it allows for seralizing data greater than 4GB
reference link: https://stackoverflow.com/questions/29704139/pickle-in-python3-doesnt-work-for-large-data-saving
"""
bytes_out = pickle.dumps(obj, protocol=4)
n_bytes = sys.getsizeof(bytes_out)
with open(filepath, 'wb') as f_out:
for idx in range(0, n_bytes, max_bytes):
f_out.write(bytes_out[idx:idx+max_bytes])
def try_to_load_as_pickled_object_or_None(filepath):
import pickle
import os
import sys
"""
This is a defensive way to write pickle.load, allowing for very large files on all platforms
"""
max_bytes = 2**31 - 1
try:
input_size = os.path.getsize(filepath)
bytes_in = bytearray(0)
with open(filepath, 'rb') as f_in:
for _ in range(0, input_size, max_bytes):
bytes_in += f_in.read(max_bytes)
obj = pickle.loads(bytes_in)
except:
return None
return obj
Regardless of how I save the file (joblib or pickle) the results are the same; that is, predict proba scores are totally different then when I use the method on the XGBClassifier object immediately after training.
On another note, On the same kernal, I am doing the same thing using an SGDClassifier, for which I am not experiencing this same problem.
One odd thing that I noticed is that if I save the classifier after training it, and then load it within the same kernal session (Using Jupyterlab), the probabilities are the same. However, if I restart the kernal, and load the object via either one of the two methods described above, then I no longer get the same probabilities.
My expectation is that I should be getting the same or nearly identical probabilities using the predict proba method on XGBClassifier.
Thanks