3
$\begingroup$

I use a neural network with 3 inputs and 1 output with Keras. I'm using MinMaxScaler from sklearn to normalize my inputs in the range [0,1]

my input shape is (XX,3) my output shape is (XX,1)

I don't have any input while scaling the input and output arrays

    self.scaler = MinMaxScaler(feature_range=(0,1))

    dataX = self.scaler.fit_transform(dataX)
    dataY = self.scaler.fit_transform(dataY)

    self.model.fit(dataX,dataY,nb_epoch=1000)

After the training I have able to make prediction like this :

    data    = numpy.array([[ val1,val2,val3]])
    data = self.scaler.fit_transform(data)
    prediction = self.model.predict(data)

but my output is not scaled correctly, it make sense, as I didn't call inverse transform to apply the inverse scaling

but when I call it, I get a ValueError , I tried to apply the transformation on a single prediction or on an array of prediction but the problem is the same

self.scaler.inverse_transform(predictions) # prediction shape is (18,) :

ValueError: operands could not be broadcast together with shapes (18,) (3,) (18,)

ValueError: non-broadcastable output operand with shape (18,1) doesn't match the broadcast shape (18,3)

I undestand it is a shape issue, but the error message does not help so much

(12,1) was accepted by fit_transform (18,1) seems pretty similar.. so I dont get the error.

$\endgroup$
2
$\begingroup$

You made some mistakes on MinMaxScaler.

MinMaxScaler shouldn't be fitted twice(as internal parameters inside MinMaxScaler will be changed), and dataX & dataY should have their own scaler(as they have different minimum and maximum values.

Try to do something like this:

self.x_scaler = MinMaxScaler()
self.y_scaler = MinMaxScaler()

dataX = self.x_scaler.fit_transform(dataX)
dataY = self.y_scaler.fit_transform(dataY)

self.model.fit(dataX, dataY, nb_epoch=1000)

data = numpy.array([[val1, val2, val3]])
data = self.x_scaler.transform(data)
prediction = self.model.predict(data)
real_prediction = self.y_scaler.inverse_transform(prediction)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.