1

I am using StandardScaler() to standardize the inputs.
How can I convert prediction back to original data? I am using the following code, but it throws me an error.

X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

#custom inputs for prediction after training
sample = pd.DataFrame({'salary': [1211], 'age': [30]})
sample = sc.transform(sample)
sample_predict = sc.inverse_transform(sample_predict)

print (sample_predict)
  • shape of X_test: (3000, 2)
  • shape of sample_predict: (1, 2)

Error:

X *= self.scale_
ValueError: non-broadcastable output operand with shape (1,1) doesn't match the broadcast shape (1,2)
Kaveh
  • 4,618
  • 2
  • 20
  • 33
NormA
  • 21
  • 2
  • 1
    Your code seems to be ok, if `sample_predict` shape is `(1,2)`. I think you meant `sample`, since you are defining `sample` and transforming it before the `inverse_transform`. – Kaveh Aug 09 '21 at 13:22
  • @Kaveh My code seems to be working what I have done is get mean and std dev from `y_train` samples, and used them to get back the original value of predict. `y_train= np.array(y_train).reshape(-1,1) standardY = StandardScaler().fit(y_train)` ... `original_values= standardY.inverse_transform(y_pred)` I am not sure if my this is correct, do you have any idea if it's correct approach? thanks! – NormA Aug 09 '21 at 14:00
  • Yes.it is the correct approach. You should use the same scaler for y_train to get back prediction, to the original scale with inverse_transform. Note that it is not recommended to apply standardization on target values. – Kaveh Aug 09 '21 at 14:06
  • Thanks man! I think `StandardScaler().fit(y_train)` only computes the parameters, and does not perform the transformation on targets. – NormA Aug 09 '21 at 14:11
  • Yes, `fit()` only compute the parameters, and does not make any transform. for transform you may use `fit_transform()` or `transform()` – Kaveh Aug 09 '21 at 15:28

0 Answers0