Long Short Term Memory
Navya Aggarwal
Code here
import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns from tensorflow import keras from keras.models import Sequential from keras.layers import Dense from keras.layers import LSTM df = pd.read_csv('monthly_milk_production.csv' index_col='Date', parse_dates=True) df.index.freq = 'MS' df.head() # Plotting graph b/w production and date df.plot(figsize=(12, 6)) from statsmodels.tsa.seasonal import seasonal_decompose results = seasonal_decompose(df['Production']) results.plot() train = df.iloc[:156] test = df.iloc[156:] from sklearn.preprocessing import MinMaxScaler scaler = MinMaxScaler() scaler.fit(train) scaled_train = scaler.transform(train) scaled_test = scaler.transform(test) from keras.preprocessing.sequence import TimeseriesGenerator n_input = 3 n_features = 1 generator = TimeseriesGenerator(scaled_train,scaled_train,length=n_input,batch_size=1) X, y = generator[0] print(f'Given the Array: {X.flatten()}') print(f'Predict this y: {y}') # We do the same thing, but now instead for 12 months n_input = 12 generator = TimeseriesGenerator(scaled_train, scaled_train,length=n_input,batch_size=1) # define model model = Sequential() model.add(LSTM(100, activation='relu',input_shape=(n_input, n_features))) model.add(Dense(1)) model.compile(optimizer='adam', loss='mse') model.summary() model.fit(generator, epochs=5)