0

Initially, i have a csv file with 6 columns: date,electricity consumption and 4 other climate features that have an impact on the consumption ( such as temperature, humidity etc)

So far, i can run my LSTM on the consumption column only , and it has given me very accurate results, but i need to feed my LSTM with the other features. I tried to modify the python code according to previous comments here but still having a reshape error.

here s my code after some modifications:

import numpy
import matplotlib.pyplot as plt
import pandas
import math

from keras.models import Sequential
from keras.layers import Dense, LSTM, Dropout
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error


# convert an array of values into a dataset matrix

def create_dataset(dataset, look_back=1):
  dataX, dataY = [], []
  for i in range(len(dataset) - look_back - 1):
    a = dataset[i:(i + look_back), :]
    dataX.append(a)
    dataY.append(dataset[i + look_back, 2])
  return numpy.array(dataX), numpy.array(dataY)


  # fix random seed for reproducibility
numpy.random.seed(7)


# load the dataset
dataframe = pandas.read_csv('out_meteo.csv', engine='python') 
dataset = dataframe.values

# normalize the dataset
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(dataset)

# split into train and test sets
train_size = int(len(dataset) * 0.67) 
test_size = len(dataset) - train_size
train, test = dataset[0:train_size, :], dataset[train_size:len(dataset), :]

# reshape into X=t and Y=t+1
look_back = 3
trainX, trainY = create_dataset(train, look_back)  
testX, testY = create_dataset(test, look_back)

# reshape input to be  [samples, time steps, features]
trainX = numpy.reshape(trainX, (trainX.shape[0], look_back, 3))
testX = numpy.reshape(testX, (testX.shape[0],look_back, 3))

# create and fit the LSTM network

model = Sequential()
model.add(LSTM(4, input_shape=(look_back,3)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
history= model.fit(trainX, trainY,validation_split=0.33, nb_epoch=5, batch_size=32)



# make predictions
trainPredict = model.predict(trainX)
testPredict = model.predict(testX)

# Get something which has as many features as dataset
trainPredict_extended = numpy.zeros((len(trainPredict),3))
# Put the predictions there
trainPredict_extended[:,2] = trainPredict
# Inverse transform it and select the 3rd column.
trainPredict = scaler.inverse_transform(trainPredict_extended)[:,2]

print(trainPredict)
# Get something which has as many features as dataset
testPredict_extended = numpy.zeros((len(testPredict),3))
# Put the predictions there
testPredict_extended[:,2] = testPredict[:,0]
# Inverse transform it and select the 3rd column.
testPredict = scaler.inverse_transform(testPredict_extended)[:,2]   


trainY_extended = numpy.zeros((len(trainY),3))
trainY_extended[:,2]=trainY
trainY=scaler.inverse_transform(trainY_extended)[:,2]


testY_extended = numpy.zeros((len(testY),3))
testY_extended[:,2]=testY
testY=scaler.inverse_transform(testY_extended)[:,2]


# calculate root mean squared error
trainScore = math.sqrt(mean_squared_error(trainY, trainPredict))
print('Train Score: %.2f RMSE' % (trainScore))
testScore = math.sqrt(mean_squared_error(testY, testPredict))
print('Test Score: %.2f RMSE' % (testScore))

# shift train predictions for plotting
trainPredictPlot = numpy.empty_like(dataset)
trainPredictPlot[:, :] = numpy.nan
trainPredictPlot[look_back:len(trainPredict)+look_back, 2] = trainPredict

# shift test predictions for plotting
testPredictPlot = numpy.empty_like(dataset)
testPredictPlot[:, :] = numpy.nan
testPredictPlot[len(trainPredict)+(look_back*2)+1:len(dataset)-1, 2] = testPredict

 # plot baseline and predictions
plt.plot(scaler.inverse_transform(dataset))
plt.plot(trainPredictPlot)
plt.plot(testPredictPlot)
plt.show()

the error i am getting is the following

Traceback (most recent call last):
  File "desp.py", line 48, in <module>
    trainX = numpy.reshape(trainX, (trainX.shape[0], look_back, 3))
  File "/usr/local/lib/python2.7/dist-packages/numpy/core/fromnumeric.py",  line 232, in reshape
    return _wrapfunc(a, 'reshape', newshape, order=order)
  File "/usr/local/lib/python2.7/dist-packages/numpy/core/fromnumeric.py",  line 57, in _wrapfunc
    return getattr(obj, method)(*args, **kwds)
ValueError: cannot reshape array of size 35226 into shape (1957,3,3)

Please note that I am still a newbie and that the reshape concept is still a little ambigus to me.

Yosr_mzg
  • 13
  • 6
  • this is the answer: cannot reshape array of size 35226 into shape (1957,3,3) – Vadim Jul 24 '17 at 13:28
  • but ok, show me please shapes of the trainX and testX. – Vadim Jul 24 '17 at 13:28
  • trainX.shape= (1957, 3, 6) testX.shape= (963, 3, 6) – Yosr_mzg Jul 24 '17 at 13:40
  • 1
    you see the issue? you have a tensor with shape (1957, 3, 6), which is equal to 1957 * 3 * 6 = 35226. And you want to reshape your tensor to a new shape (1957,3,3) = 17613. So, the problem is your new dimensions are not fit. As a work around, I suggest to remove all reshape functions. And change input shape into (look_back, 6) – Vadim Jul 24 '17 at 13:45

3 Answers3

0

As an answer to your question I would suggest to check about multidimensional lists / arrays in python / numpy.

Also, here is a link to explanation regarding shapes of the tensor in Keras

https://github.com/fchollet/keras/issues/2045

Vadim
  • 4,219
  • 1
  • 29
  • 44
0

here is my final code that takes all of the columns

import numpy
import matplotlib.pyplot as plt
import pandas
import math

from keras.models import Sequential
from keras.layers import Dense, LSTM, Dropout
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
# convert an array of values into a dataset matrix
def create_dataset(dataset, look_back=1):
   dataX, dataY = [], []
   for i in range(len(dataset) - look_back - 1):
     a = dataset[i:(i + look_back), :]
     dataX.append(a)
     dataY.append(dataset[i + look_back, 2])
   return numpy.array(dataX), numpy.array(dataY)


 # fix random seed for reproducibility
numpy.random.seed(7)

#load the dataset
dataframe = pandas.read_csv('out_meteo.csv', engine='python') 
dataset = dataframe.values

# normalize the dataset
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(dataset)

# split into train and test sets
train_size = int(len(dataset) * 0.7) 
test_size = len(dataset) - train_size
train, test = dataset[0:train_size, :], dataset[train_size:len(dataset), :]

# reshape into X=t and Y=t+1
look_back = 3
trainX, trainY = create_dataset(train, look_back)  
testX, testY = create_dataset(test, look_back)


# create and fit the LSTM network

model = Sequential()
model.add(LSTM(20, input_shape=(look_back,6)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
history= model.fit(trainX, trainY,validation_split=0.33, nb_epoch=15, batch_size=15)

# make predictions
trainPredict = model.predict(trainX)
testPredict = model.predict(testX)

print(trainPredict)

# calculate root mean squared error
trainScore = math.sqrt(mean_squared_error(trainY, trainPredict))
print('Train Score: %.2f RMSE' % (trainScore))
testScore = math.sqrt(mean_squared_error(testY, testPredict))
print('Test Score: %.2f RMSE' % (testScore))

# shift train predictions for plotting
trainPredictPlot = numpy.empty_like(dataset)
trainPredictPlot[:, :] = numpy.nan
trainPredictPlot[look_back:len(trainPredict)+look_back, :] = trainPredict

# shift test predictions for plotting
testPredictPlot = numpy.empty_like(dataset)
testPredictPlot[:, :] = numpy.nan
testPredictPlot[len(trainPredict)+(look_back*2)+1:len(dataset)-1, :] = testPredict

 # plot baseline and predictions
plt.plot((dataset))
plt.plot(trainPredictPlot)
plt.plot(testPredictPlot)
plt.show()

So far, it works well on all of my csv columns, i have also removed many lines ( reshape , MinMAxScaler transformation) but still cannot visualize my final data correctly ( with real values), it shows really small values or a strict line.
the return train and test score for this dataset are respectively 0.03 and 0.05

Yosr_mzg
  • 13
  • 6
0

Before plotting, try to do

testPredict = scaler.inverse_transform(testPredict)
groznybear
  • 53
  • 7