LSTM time series forecasting of covid-19 data
0
0
Entering edit mode
AbuZar • 0
@eabafcf2
Last seen 4.2 years ago
India

![here is an image of the plot][1]Hello World i have to predict the future using deep learning "LSTM time series Forecasting using Keras, i have a dataset of COVID-19 related to india where the no. of deaths and some other features are mentions from january 2020 to january 2021.i have to predict the future upcoming waves

from math import sqrt from numpy import concatenate from matplotlib import pyplot from pandas import read_csv from pandas import DataFrame from pandas import concat from sklearn.preprocessing import MinMaxScaler from sklearn.preprocessing import LabelEncoder from sklearn.metrics import mean_squared_error from keras.models import Sequential from keras.layers import Dense from keras.layers import LSTM

convert series to supervised learning

def series_to_supervised(data, n_in=1, n_out=1, dropnan=True): n_vars = 1 if type(data) is list else data.shape[1] df = DataFrame(data) cols, names = list(), list()

# input sequence (t-n, ... t-1)
for i in range(n_in, 0, -1):
    cols.append(df.shift(i))
    names += [('var%d(t-%d)' % (j+1, i)) for j in range(n_vars)]
# forecast sequence (t, t+1, ... t+n)
for i in range(0, n_out):
    cols.append(df.shift(-i))
    if i == 0:
        names += [('var%d(t)' % (j+1)) for j in range(n_vars)]
    else:
        names += [('var%d(t+%d)' % (j+1, i)) for j in range(n_vars)]
# put it all together
agg = concat(cols, axis=1)
agg.columns = names
# drop rows with NaN values
if dropnan:
    agg.dropna(inplace=True)
return agg

load dataset

dataset = read_csv('newtimeseriesdatare.csv', header=0, index_col=1) values = dataset.values

integer encode direction

encoder = LabelEncoder() values[:,4] = encoder.fit_transform(values[:,4])

ensure all data is float

values = values.astype('float32')

normalize features

scaler = MinMaxScaler(feature_range=(0, 1)) scaled = scaler.fit_transform(values)

specify the number of lag hours

n_days = 1 n_features = 19

frame as supervised learning

reframed = series_to_supervised(scaled, n_days, 1) print(reframed.shape)

the output of this is---->(672,46)

in my dataset i have only 23 features why it shows me 46?

split into train and test sets

design network

model = Sequential() model.add(LSTM(50, input_shape=(train_X.shape[1], train_X.shape[2]))) model.add(Dense(1)) model.compile(loss='mae', optimizer='adam')

fit network

history = model.fit(train_X, train_y, epochs=50, batch_size=8, validation_data=(test_X, test_y), verbose=2, shuffle=False)

plot history

pyplot.plot(history.history['loss'], label='train') pyplot.plot(history.history['loss'], label='test') pyplot.legend() pyplot.show()

the output of this is ----->a plot where i get on the curve of my test data it does not plot the train data curve.

make a prediction

yhat = model.predict(test_X) test_X = test_X.reshape((test_X.shape[0], n_days*n_features))

the output of this is an error----->"ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 1, 19), found shape=(None, 19)"

invert scaling for forecast

inv_yhat = concatenate((yhat, test_X[:, -19:]), axis=1) inv_yhat = scaler.inverse_transform(inv_yhat) inv_yhat = inv_yhat[:,0]

invert scaling for actual

test_y = test_y.reshape((len(test_y), 1)) inv_y = concatenate((test_y, test_X[:, -19:]), axis=1) inv_y = scaler.inverse_transform(inv_y) inv_y = inv_y[:,0]

the output of this is also an error---->"ValueError: operands could not be broadcast together with shapes (272,20) (23,) (272,20)"

```

#deeplearning #python #lstm #machinelearning • 1.3k views
ADD COMMENT

Login before adding your answer.

Traffic: 1399 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6