<jupyter_start><jupyter_text>**When using the sigmoid (default) or tanh activation functions, LSTMs are sensitive to the scale of the input data. Rescaling data to the 0-to-1 range, also known as normalizing, is a recommended technique. Using the MinMaxScaler preprocessing class from the scikit-learn package, we can quickly normalize the dataset.**<jupyter_code># sales = sales_tot.groupby(by = ['date'])['Total'].sum()
# sales = pd.DataFrame(sales)
# sales.reset_index(inplace = True)
# sales.head()
# #weekley group by based on year and week
# weekley_groupby = sales_tot.groupby(by =['year','week']).sum()
# weekley_groupby = pd.DataFrame(weekley_groupby)
# weekley_groupby.reset_index(inplace=True)
# weekley_groupby.drop(['month','days'], axis = 1, inplace = True)
# weekley_groupby.head(5)
# # adding a weeks col which contains continous week numbers 
# weekley_groupby['weeks'] = np.arange(len(weekley_groupby))
# weekley_groupby['weeks'] = weekley_groupby['weeks'] +1 
# weekley_groupby.head()
sales_tot = sales_tot.drop(['year','week','weeks'], axis = 1)
sales_ = sales_tot.copy()
sales_.head()
len(sales_)
window_size = 8
#tot = sales['Total].tolist()
i = 0
moving_averages = []
while i < len(sales_['Total']) - window_size + 1:
    this_window = sales_['Total'][i : i + window_size]
    window_average = sum(this_window) / window_size
    moving_averages.append(window_average)
    i += 1

#print(moving_averages)
sales = pd.DataFrame(moving_averages, columns=['Total'])
sales.head()
len(sales)
# normalize the dataset
scaler = MinMaxScaler()
sales_norm = scaler.fit_transform(sales.Total.values.reshape(-1, 1))
sales_norm = sales_norm.flatten().tolist()
sales['sales_norm'] = sales_norm

sales = sales.drop(['Total'],axis = 1)
#adding lag to the sales for multistep forecasting
def lags(dataframe, lags):
    for lag in range(1,lags):
        col_name = 'lag_' +str(lag)
        dataframe[col_name] = dataframe['sales_norm'].shift(lag)

    #drop null val
    dataframe = dataframe.dropna().reset_index(drop = True)
    return dataframe
sales = lags(sales,6)
sales

# tain and test dataset
def split_train_test(dataframe,trainsize):
    """ This function splits the dataframe in to train and test sets and converts in to LSTM readable format
    It needs 2 input: 
        1. Dataframe to split the dta into train and test
        2. trainsize in percentage ratio.
            eg: if you want 80% of the data as training then plug in 0.8"""

    train = dataframe[: int(len(dataframe)*trainsize)].values
    test =  dataframe[int(len(dataframe)*trainsize):].values
    X_train = train[:, 1:]
    y_train = train[:, 0:1]
    X_train = X_train.reshape(X_train.shape[0], 1, X_train.shape[1])
    X_test = test[:, 1:]
    y_test = test[:, 0:1]
    X_test = X_test.reshape(X_test.shape[0], 1, X_test.shape[1])
    return X_train, y_train, X_test, y_test
X_train, y_train, X_test, y_test = split_train_test(lags(sales,6), 0.9)
X_train.shape
# building an LSTM model

K.clear_session()
#tf.random.set_seed(89)
model = Sequential()
model.add(Bidirectional(LSTM(64, activation = 'tanh', input_shape = ( X_train.shape[1], X_train.shape[2]), return_sequences=True)))
#model.add(Bidirectional(LSTM(512, activation = 'tanh', return_sequences = True)))
#model.add(Bidirectional(LSTM(256,activation = 'tanh', return_sequences = True)))
#model.add(Bidirectional(LSTM(128, activation = 'tanh', return_sequences = True)))
#model.add(Bidirectional(LSTM(64, activation = 'tanh', return_sequences = True)))
model.add(Bidirectional(LSTM(32, activation = 'tanh', return_sequences = False)))
model.add(Dropout(0.5))
#model.add(Dense(1024, activation = 'tanh'))
#model.add(Dense(512, activation = 'tanh'))#256,
#model.add(Dense(100, activation = 'tanh'))#256,128
model.add(Dense(50, activation = 'tanh'))
model.add(Dense(y_train.shape[1]))
model.compile(optimizer='adam', 
                loss='mse')#metrics=['mse']


#model.summary()
cb = keras.callbacks.EarlyStopping(monitor='val_loss',
                                    min_delta=0.01,
                                    patience=499,
                                    verbose=1,
                                    mode="min",
                                    baseline=None,
                                    restore_best_weights=False)
history = model.fit(X_train,y_train, 
                    epochs = 500, 
                    batch_size = 10, 
                    validation_split=0.2, 
                    verbose = 1,
                    callbacks=[cb],
                    shuffle= True)
#np.asarray(X_train).astype('float32'), np.asarray(y_train).astype('float32')
pd.DataFrame(history.history).plot()
plt.grid(True)
plt.gca() # set the y range to [0,1]
plt.show()
print('Model Evalution Score')
print(model.evaluate(X_test, y_test))
trainPredict = model.predict(X_train)
testPredict = model.predict(X_test)
#X_train, y_train, X_test, y_test = split_train_test(lags(sales, 11), 0.8)
trainPredict = scaler.inverse_transform(trainPredict)
y_train = scaler.inverse_transform(y_train)
testPredict = scaler.inverse_transform(testPredict)
y_test = scaler.inverse_transform(y_test)
type(trainPredict)
sale_week = np.concatenate((y_train , y_test), axis = 0)
sale_pred = np.concatenate((trainPredict , testPredict), axis = 0)
trainScore = math.sqrt(mean_squared_error(y_train[:,0], trainPredict[:,0]))
trainScore
testScore = math.sqrt(mean_squared_error(y_test[:,0], testPredict[:,0]))
testScore
train_r2 = r2_score(y_train[:,0], trainPredict[:,0])
train_r2
test_r2 = r2_score(y_test[:,0], testPredict[:,0])
test_r2
sns.set_theme(context='notebook',
    style="whitegrid",
    font='sans-serif',
    font_scale=1,
    color_codes=True,
    rc=None)
fig_dims = (15,8 )
fig, ax = plt.subplots(figsize=fig_dims)

font = {'family': 'serif',
        'color':  'black',
        'weight': 'normal',
        'size': 16,
        }

sns.lineplot(x = np.arange(len(trainPredict)), y = y_train[:,0].tolist() , ax = ax)  #sales[: int(len(sales)*0.8)].sales_norm
sns.scatterplot(x = np.arange(len(trainPredict)), y = y_train[:,0].tolist(), markers= '*', ax = ax )
sns.lineplot(x = np.arange(len(trainPredict)), y =trainPredict[:,0].tolist() , ax = ax)
plt.xlabel(r'$Weeks$', fontdict = font)
plt.ylabel(r'$Sales$ ', fontdict = font)
plt.yticks(fontsize = 14)
plt.show()
sns.set_theme(context='notebook',
    style="whitegrid",
    font='sans-serif',
    font_scale=1,
    color_codes=True,
    rc=None)
fig_dims = (15,8 )
fig, ax = plt.subplots(figsize=fig_dims)

font = {'family': 'serif',
        'color':  'black',
        'weight': 'normal',
        'size': 16,
        }
sns.lineplot(x = np.arange(len(testPredict)), y = y_test[:,0].tolist() , ax = ax)  #sales[: int(len(sales)*0.8)].sales_norm
sns.lineplot(x = np.arange(len(testPredict)), y =testPredict[:,0].tolist() , ax = ax)
plt.xlabel(r'$Weeks$', fontdict = font)
plt.ylabel(r'$Sales$ ', fontdict = font)
plt.yticks(fontsize = 14)
plt.show()
#model.save(r".\models\model_final.h5")

sns.set_theme(context='notebook',
    style="whitegrid",
    font='sans-serif',
    font_scale=1,
    color_codes=True,
    rc=None)
fig_dims = (15,8 )
fig, ax = plt.subplots(figsize=fig_dims)

font = {'family': 'serif',
        'color':  'black',
        'weight': 'normal',
        'size': 16,
        }
sns.lineplot(x = np.arange(len(sale_week)), y = sale_week[:,0].tolist() , ax = ax)
sns.scatterplot(x = np.arange(len(sale_week)), y = sale_week[:,0].tolist(), markers= '*', ax = ax )
sns.lineplot(x = np.arange(len(sale_pred)), y =sale_pred[:,0].tolist() , ax = ax)
plt.xlabel(r'$Weeks$', fontdict = font)
plt.ylabel(r'$Sales$ ', fontdict = font)
plt.yticks(fontsize = 14)
plt.show()
sale_week = np.concatenate((y_train , y_test), axis = 0)[:,0].tolist()
sale_pred = np.concatenate((trainPredict , testPredict), axis = 0)[:,0].tolist()

result = pd.DataFrame({'sales': sale_week,'sales_pred': sale_pred})
result.head()
result.to_csv('./data/results_1.csv')
res = pd.read_csv('./data/results_1.csv', index_col=0)
res.head()
len(res)
week = np.arange(len(res['sales']))
res['week'] = week
res.to_csv('./data/results_week.csv')
len(res.sales[:109])
res.sales[:109]
sns.set_theme(context='notebook',
    style="whitegrid",
    font='sans-serif',
    font_scale=1,
    color_codes=True,
    rc=None)
fig_dims = (15,8 )
fig, ax = plt.subplots(figsize=fig_dims)

font = {'family': 'serif',
        'color':  'black',
        'weight': 'normal',
        'size': 16,
        }
sns.lineplot(x = res.week[:109], y = res.sales[:109] , ax = ax)
sns.scatterplot(x = res.week[:109], y = res.sales[:109] , ax = ax, markers= '*')
sns.lineplot(x = res.week[:109], y = res.sales_pred[:109] , ax = ax)

sns.lineplot(x = res.week[109:], y = res.sales[109:] , ax = ax)
sns.scatterplot(x = res.week[109:], y = res.sales[109:] , ax = ax, markers= '*')
sns.lineplot(x = res.week[109:], y = res.sales_pred[109:] , ax = ax)


plt.xlabel(r'$Weeks$', fontdict = font)
plt.ylabel(r'$Sales$ ', fontdict = font)
plt.yticks(fontsize = 14)
plt.show()
pred = pd.read_csv('./data/results_week.csv', index_col=0)
pred.head()
forecast = pd.read_csv('./data/forecast_12weeks.csv')
forecast.head()

#week = np.arange(len(pred['Total']))
#pred['week'] = week
avg_forecast = forecast[['forecast1',
'forecast2','forecast3','forecast4','forecast5',
'forecast6','forecast7','forecast8','forecast9',
'forecast10','forecast11']].mean(axis  =1)
forecast['mean'] = avg_forecast
forecast['std'] = forecast[['forecast1',
'forecast2','forecast3','forecast4','forecast5',
'forecast6','forecast7','forecast8','forecast9',
'forecast10','forecast11']].std(axis  =1)
forecast.to_csv('./forecast_data/forecast_avg.csv')
forecast = pd.read_csv('./forecast_data/forecast_avg.csv')
forecast.head()
#pred.to_csv('./data/forecast_week.csv')
pred.tail(15)
fig_dims = (15,8 )
fig, ax = plt.subplots(figsize=fig_dims)
plt.style.use("dark_background")
plt.style.use("cyberpunk")

font = {'family': 'serif',
        'color':  'white',
        'weight': 'normal',
        'size': 16,
        }

sns.lineplot(x = res.week[:109], y = res.sales[:109] , ax = ax)
sns.scatterplot(x = res.week[:109], y = res.sales[:109] , ax = ax, markers= '*')
sns.lineplot(x = res.week[:109], y = res.sales_pred[:109] , ax = ax)

sns.lineplot(x = res.week[109:122], y = res.sales[109:122] , ax = ax)
sns.scatterplot(x = res.week[109:122], y = res.sales[109:122] , ax = ax, markers= '*')
sns.lineplot(x = res.week[109:122], y = res.sales_pred[109:122] , ax = ax)

#sns.lineplot(x = forecast['week'], y = forecast['mean'] , ax = ax)
#sns.scatterplot(x = forecast['week'], y = forecast['mean'] , ax = ax, markers= '*')

ax.plot(forecast['week'], forecast['mean'], '-')
ax.fill_between(forecast['week'], forecast['mean'] - forecast['std'], forecast['mean'] + forecast['std'], alpha=0.2)
ax.plot(forecast['week'], forecast['mean'], '*')

plt.xlabel(r'$Weeks$', fontdict = font)
plt.ylabel(r'$Sales$ ', fontdict = font)
plt.yticks(fontsize = 14)

mplcyberpunk.add_glow_effects()
plt.show()
fig_dims = (15,8 )
fig, ax = plt.subplots(figsize=fig_dims)
plt.style.use("dark_background")
plt.style.use("cyberpunk")

font = {'family': 'serif',
        'color':  'white',
        'weight': 'normal',
        'size': 16,
        }

ax.plot(forecast['week'], forecast['mean'], '-')
ax.fill_between(forecast['week'], forecast['mean'] - forecast['std'], forecast['mean'] + forecast['std'], alpha=0.2)
ax.plot(forecast['week'], forecast['mean'], 'o')
plt.xlabel(r'$Weeks$', fontdict = font)
plt.ylabel(r'$Sales$ ', fontdict = font)
plt.yticks(fontsize = 14)
mplcyberpunk.add_glow_effects()
plt.show()
#forecast['week'], forecast['mean'], '-'
import plotly.express as px
import plotly.io as pio
#pio.renderers.default = "vscode"
pio.renderers.default = "notebook_connected"
#fig = px.scatter(forecast, x  ='week', y = 'mean')

fig = px.line(forecast, x  ='week', y = 'mean',  markers = True)
#fig = ax.fill_between(forecast['week'], forecast['mean'] - forecast['std'], forecast['mean'] + forecast['std'], alpha=0.2)

fig.show()
from numpy import array
X = array([[10, 20, 30], [20, 30, 40], [30, 40, 50], [40, 50, 60]])
y = array([[40,50],[50,60],[60,70],[70,80]])
# reshape from [samples, timesteps] into [samples, timesteps, features]
#X = X.reshape((X.shape[0], X.shape[1], 1))
#y = y.reshape((y.shape[0], y.shape[1], 1))
print(X)<jupyter_output>[[[10]
  [20]
  [30]]

 [[20]
  [30]
  [40]]

 [[30]
  [40]
  [50]]

 [[40]
  [50]
  [60]]]
StarcoderdataJupyterStructuredCleanDedup