1

here is my code

scaler = MinMaxScaler() #default set 0~1
dataset= scaler.fit_transform(dataset)
...
make model
...
predicted = model.predict(X_test) #shape : (5, 1)

and when I run predict = scaler.inverse_transform(predicted)

ValueError occur ValueError: non-broadcastable output operand with shape (5,1) doesn't match the broadcast shape (5,2)

My model have 2 feature as input

I tried scaler.inverse_transform(predict)[:, [0]] and reshape in several directions

but occur same ValueError

how can I solve this Problem? please give me some advice

I need your priceless opinion and will be very much appreciated.

desertnaut
  • 57,590
  • 26
  • 140
  • 166

1 Answers1

0

You are using inverse_transform in a wrong way: while you have used fit_transform to your features, you are using inverse_transform to your predictions, which are of a different shape, hence the error.

This is not the intended usage of inverse_transform; have a look at the docs for more:

inverse_transform(self, X)

Undo the scaling of X according to feature_range.

Parameters: X : array-like, shape [n_samples, n_features]

Input data that will be transformed. It cannot be sparse.

It is not clear from your post why you attempt to "transform back" your predictions; this only makes sense if you already have transformed your labels (it is not clear from your post if you have done so), and you want, say, to scale back measures like MSE in the original scale of the labels. In such a case, you should use a separate scaler for your labels - see own answer in How to interpret MSE in Keras Regressor for details (the example there is with StandardScaler, but the rationale is the same).

Community
  • 1
  • 1
desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • thank you but in my code can't use this becasue `X_test` and `X_train` is 3-dimension https://gist.github.com/Lay4U/04136c26c1dcfad858dc6325fd54ca18 –  Aug 24 '19 at 08:48
  • @랜덤상자 not sure why you think that this has something to do with the fact that you should scale your labels `y` separately before trying to invert-transform them, as suggested here; plus, it's a mistake to transform *before* splitting to train & test sets... – desertnaut Aug 24 '19 at 08:52
  • Please use the translational literary body if possible because I'm not good at English. Okay, So I tried trasnform after split, you can see comment in gist and I got ValueError How can I sove it? –  Aug 24 '19 at 09:10
  • With `LSTM`, one value is output using five features; thus, the shape of (5,5) is output as (5,1) via `model.predict`. The answer you said has nothing to do with my question: no matter how you scale, shape deformation is bound to happen by `LSTM`.So my question is, how do I `transform` this when the shape of (5,5) can only be changed to the shape of (5, 1)? –  Aug 24 '19 at 09:58
  • 1
    @랜덤상자 answer simply says that what you attempt to do, i.e. use the scaler used for your *features* to scale back your *predictions*, is invalid and wrong, since these will always be of different shapes, hence the error. This is true irrespectively of LSTMs etc, about which there is nothing in your original question; that's why I am advising to accept the answer and move on to a **new** question, with more details as needed - see how to create a [mcve]. – desertnaut Aug 24 '19 at 10:10