<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># normalize the dataset
scaler = MinMaxScaler()
sales_norm = scaler.fit_transform(sales.Total.values.reshape(-1, 1))
sales_norm
sales_norm = sales_norm.flatten().tolist()
sales['sales_norm'] = sales_norm
sales.head()
sales = sales.drop(['Paid at','Total','year','month','week','days','date'], axis = 1)
sales.head()
#adding lag to the sales
for lag in range(1,31):
    col_name = 'lag_' +str(lag)
    sales[col_name] = sales['sales_norm'].shift(lag)

#drop null val
sales = sales.dropna().reset_index(drop = True)
sales.head()
# tain and test dataset

train, test = sales[: int(len(sales)*0.8)].values, sales[int(len(sales)*0.8):].values
type(train)
X_train, y_train = train[:, 1:], train[:, 0:1]
X_train = X_train.reshape(X_train.shape[0], 1, X_train.shape[1])
X_test, y_test = test[:, 1:], test[:, 0:1]
X_test = X_test.reshape(X_test.shape[0], 1, X_test.shape[1])
a = np.arange(6)

a.reshape((3,2))
X_train = train[:, 1:]
X_train.reshape(X_train.shape[0], 1, X_train.shape[1])
K.clear_session()

model = Sequential()
model.add(LSTM(64, activation = 'relu', input_shape = ( X_train.shape[1], X_train.shape[2]), return_sequences=True))
model.add(LSTM(16, activation = 'relu', return_sequences = False))
model.add(Dropout(0.2))
model.add(Dense(1))#y_train.shape[1]
model.compile(optimizer='adam', loss = 'mse')
model.summary()
history = model.fit(np.asarray(X_train).astype('float32'), np.asarray(y_train).astype('float32'), epochs = 100, batch_size = 2, validation_split=0.2, verbose = 1)
pd.DataFrame(history.history).plot()
plt.grid(True)
plt.gca().set_ylim(0,1.1) # set the y range to [0,1]
plt.show()
print('Model Evalution Score')
print(model.evaluate(X_test, y_test))
#model.save(r"..\models\model.h5")
trainPredict = model.predict(X_train)
testPredict = model.predict(X_test)
X_train = sales[: int(len(sales)*0.8)].sales_norm
X_train

X_test = sales[int(len(sales)*0.8):].sales_norm
X_test
trainPredict = trainPredict.flatten().tolist()
testPredict = testPredict.flatten().tolist()
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(int(len(sales)*0.8)), y = sales[: int(len(sales)*0.8)].sales_norm, ax = ax)  
sns.lineplot(x = np.arange(int(len(sales)*0.8)), y =trainPredict , ax = ax)
plt.xlabel(r'$Days$', fontdict = font)
plt.ylabel(r'$Daily _ Sales_norm$ ', fontdict = font)
plt.yticks(fontsize = 14)
plt.show()
model.save(r'.\models\model_100_2.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(int(len(sales))- int(len(sales)*0.8)), y = sales[int(len(sales)*0.8):].sales_norm, ax = ax) 
sns.lineplot(x = np.arange(int(len(sales))- int(len(sales)*0.8)), y = testPredict, ax = ax)
plt.xlabel(r'$Days$', fontdict = font)
plt.ylabel(r'$Daily _ Sales_norm$ ', fontdict = font)
plt.yticks(fontsize = 14)
plt.show()
# calculate root mean squared error
trainScore = math.sqrt(mean_squared_error(X_train, trainPredict))
print('Train Score: %.2f RMSE' % (trainScore))
testScore = math.sqrt(mean_squared_error(X_test, testPredict))
print('Test Score: %.2f RMSE' % (testScore))<jupyter_output>Train Score: 0.06 RMSE
Test Score: 0.09 RMSE
{'fraction_non_alphanumeric': 0.18021646111250944, 'fraction_numerical': 0.0259249937075258, 'mean_word_length': 5.378812199036918, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '310897', 'license': 'MIT', 'n_tokens_mistral': 1624, 'n_tokens_neox': 1524, 'n_words': 306, 'path': 'EDA/model.ipynb', 'repo_name': 'jaybfn/Customer-Sales-Forecasting', 'size': '3973'}