How to reverse reshaped array values back to normal values?

43 views Asked by At

I am working on an LSTM model to predict stock prices for the next 30 days. I used below code to get my prediction:

# demonstrate prediction for next 30 days
from numpy import array

output=[]
n_steps=30
i=0
while(i<30):
    
    if(len(temp_input)>30):
        #print(temp_input)
        x_input=np.array(temp_input[1:])
        print("{} day input {}".format(i,x_input))
        x_input=x_input.reshape(1,-1)
        x_input = x_input.reshape((1, n_steps, 1))
        #print(x_input)
        yhat = model.predict(x_input, verbose=0)
        print("{} day output {}".format(i,yhat))
        temp_input.extend(yhat[0].tolist())
        temp_input=temp_input[1:]
        #print(temp_input)
        output.extend(yhat.tolist())
        i=i+1
    else:
        x_input = x_input.reshape((1, n_steps,1))
        yhat = model.predict(x_input, verbose=0)
        print(yhat[0])
        temp_input.extend(yhat[0].tolist())
        print(len(temp_input))
        output.extend(yhat.tolist())
        i=i+1
    

print(output)

As you can see there is some reshaping going above. I added my outputs to an array and flattened it as below:

predictions = np.array(output)
predictions.shape
prediction= predictions.flatten()
prediction

This gives me below array:

array([0.18659154, 0.1891453 , 0.19176196, 0.19444165, 0.19719107,
       0.20001771, 0.20292525, 0.20591702, 0.20900257, 0.21218635,
       0.21547087, 0.21886323, 0.2223666 , 0.22598156, 0.22970821,
       0.23355022, 0.23751454, 0.24160348, 0.24581996, 0.25016612,
       0.25464395, 0.25926396, 0.26403409, 0.2689575 , 0.27403867,
       0.27928692, 0.28470588, 0.29029819, 0.29606536, 0.30200851])

Now I want to reverse these values back to normal values, but I can't. I tried to use inverse_transform, but I wasn't successful.

Help please?

1

There are 1 answers

1
Kilian On

you could store the original shape in a variable and latter reshape it back.

sha=x.shape
x=x.flatten()
x=x.reshape(sha)