How to Inverse Transform a Predicted Output of a loaded pickle XGBoost model?

Question:

I am trying to run a program that could produce a predicted output using a loaded model (pickle file). The saved model (XGBoost) was trained to have its dataset to undergo transformation via StandardScaler before fitting it, and the predicted value needs to be inverse transformed to get the actual predicted value. The data consists of 2 input values, and 1 output value.

I already have done prediction using the pickle file. However, when I try to inverse transform the output, I get an error saying "sklearn.exceptions.NotFittedError: This StandardScaler instance is not fitted yet. Call ‘fit’ with appropriate arguments before using this estimator."

raw_data = pd.DataFrame(data, columns=columns)

raw_data['X'] = raw_data['X'].astype(float)
raw_data['Y'] = raw_data['Y'].astype(float)
print(raw_data)

from sklearn.preprocessing import StandardScaler
sc = StandardScaler()

xgb_model_loaded = pickle.load(open('model_1.pkl', 'rb'))

output = xgb_model_loaded.predict(raw_data)

output = sc.inverse_transform((output.reshape(-1,1)), copy=None)
print(output)

What could fix this error?

I also tried StandardScaler transform on the input variables of raw_data. Yet, I receive another error saying
"ValueError: non-broadcastable output operand" with shape (1,1) doesn’t match the broadcast shape (1,2)"

Asked By: qvecb

||

Answers:

There are three ways to do this:

1. Export StandardScaler as pickle as well
You can export the StandardScaler you used to train the model as a pickle as well and then load it with the model to use it.

2. Just save StandardScaler parameters
Save the parameters of the StandardScaler you used to train the model (sc.mean_ and sc.var_), it’s all you need to transform and inverse transform.

3. Using a pipeline (preferred method)
When training the model, use a pipeline to group StandardScaler and XGBoost in a single model:

from sklearn.pipeline import Pipeline

# Create a pipeline
model = Pipeline([
    ('StandardScaler', sc),
    ('XGBoost', xgb_model),
])

#Then train and save the model.
Answered By: Mattravel

I was able to solve this problem by saving the StandardScaler parameters, and loading it in my program. Now, the predicted data can be displayed in its inverse transform figure.

raw_data = pd.DataFrame(data, columns=columns)
raw_data['X'] = raw_data['X'].astype(float)
raw_data['Y'] = raw_data['Y'].astype(float)
print(raw_data)

from sklearn.preprocessing import StandardScaler
sc = StandardScaler()

xgb_model_loaded = pickle.load(open('model_1.pkl', 'rb'))

output = xgb_model_loaded.predict(raw_data)

sc = load('Std_Scaler_1.bin')
output = sc.inverse_transform((bgl_output.reshape(-1,1)), copy=None)

print(output)
Answered By: qvecb