如何反向转换加载的 pickle XGBoost 模型的预测输出?

huangapple go评论66阅读模式
英文:

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

问题

以下是翻译好的内容:

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.

我正在尝试运行一个程序,使用加载的模型(pickle文件)来产生预测输出。保存的模型(XGBoost)在拟合之前经过StandardScaler的数据集转换,需要将预测值逆向转换以获得实际预测值。数据包括2个输入值和1个输出值。

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."

我已经使用pickle文件进行了预测。然而,当我尝试逆向转换输出时,出现错误,提示:"sklearn.exceptions.NotFittedError: This StandardScaler instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator."

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).

我还尝试对raw_data的输入变量进行了StandardScaler转换。然而,我收到另一个错误,提示:"ValueError: non-broadcastable output operand",形状为(1,1),与广播形状(1,2)不匹配。

英文:

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)"

答案1

得分: 1

  1. 将StandardScaler导出为pickle文件,然后与模型一起加载以供使用。

  2. 只需保存用于训练模型的StandardScaler参数(sc.mean_sc.var_),这就足够用于进行变换和逆变换。

  3. 使用管道(首选方法):在训练模型时,使用管道将StandardScaler和XGBoost组合成一个单一的模型:

from sklearn.pipeline import Pipeline

# 创建一个管道
model = Pipeline([
    ('StandardScaler', sc),
    ('XGBoost', xgb_model),
])

# 然后训练并保存模型。
英文:

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.

答案2

得分: 0

以下是翻译好的部分:

"我通过保存StandardScaler参数并在我的程序中加载它来解决了这个问题。现在,预测数据可以以其逆转换的形式显示。

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)

希望这对你有帮助。

英文:

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)

huangapple
  • 本文由 发表于 2023年3月7日 23:06:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663712.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定