[sklearn][standardscaler] can I inverse the standardscaler for the model output?


I have some data structured as below, trying to predict t from the features.


t: time to predict
f1: feature1
f2: feature2 

Can t be scaled with StandardScaler, so I instead predict t' and then inverse the StandardScaler to get back the real time?

For example:

from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
train_df['t']= scaler.transform(train_df['t'])

run regression model,

check score,

!! check predicted t’ with real time value(inverse StandardScaler) <- possible?

Asked By: hyon



Yeah, and it’s conveniently called inverse_transform.

The documentation provides examples of its use.

Answered By: Arya McCarthy

Here is sample code. You can replace here data with train_df['colunm_name'].
Hope it helps.

from sklearn.preprocessing import StandardScaler
data = [[1,1], [2,3], [3,2], [1,1]]
scaler = StandardScaler()
scaled = scaler.transform(data)

# for inverse transformation
inversed = scaler.inverse_transform(scaled)
Answered By: rohan chikorde

While @Rohan’s answer generally worked for me and my DataFrame column, I had to reshape the data according to the below StackOverflow answer.

Sklearn transform error: Expected 2D array, got 1D array instead

scaler = StandardScaler()
scaled = scaler.transform(df[[col_name]])
Answered By: JasonG
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.