From 3ffc07c4fd491066ee09871dadb165eed34dfde0 Mon Sep 17 00:00:00 2001 From: amilashanaka <dsa.amilashanaka@gmail.com> Date: Thu, 1 Dec 2022 03:55:39 +0000 Subject: [PATCH] lstm and optimize --- model.py | 204 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 203 insertions(+), 1 deletion(-) diff --git a/model.py b/model.py index 60baa90..c2f9197 100644 --- a/model.py +++ b/model.py @@ -108,4 +108,206 @@ plt.show() # Monthly pivot table df = (df.pivot(index='Year', columns='Month', values='OrderDemand')) df = df.loc[:, ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']] -df \ No newline at end of file +print(df) + + + +custom_dict = {'Jan':0, 'Feb':1, 'Mar':2, 'Apr':3, 'May':4, 'Jun':5, + 'Jul':6, 'Aug':7, 'Sep':8, 'Oct':9, 'Nov':10, 'Dec':11} +temp_data = data.copy() +temp_data.Month.replace([1,2,3,4,5,6,7,8,9,10,11,12], ['Jan', 'Feb', 'Mar', 'Apr', 'May', + 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], inplace=True) +df = temp_data[["OrderDemand", 'Month', 'Year']].groupby(["Year", + "Month"]).sum().reset_index().sort_values(by=['Year', + 'Month'], ascending=True) +df = df.iloc[df['Month'].map(custom_dict).argsort()] +f, ax=plt.subplots(figsize=(15, 5)) +sns.pointplot(x='Month', y="OrderDemand", data=df, hue="Year") +plt.show() + +print(df.describe()) + + + +df = data[["OrderDemand", 'Year', 'Warehouse']].groupby(["Year", + "Warehouse"]).sum().reset_index().sort_values(by=['Warehouse','Year'], ascending=False) +f, ax=plt.subplots(figsize=(15, 5)) +sns.pointplot(x='Year', y="OrderDemand", data=df, hue="Warehouse") +plt.show() + +df = (df.pivot(index='Year', columns='Warehouse', values='OrderDemand')) +print(df) + + +print(df.describe()) + + +#Product Category Based Analysis + + +df = data[["OrderDemand", + 'ProductCategory', 'Warehouse']].groupby(["ProductCategory", + "Warehouse"]).sum().reset_index().sort_values(by=['OrderDemand'], + ascending=False) +df = df.pivot(index='ProductCategory', columns='Warehouse', values='OrderDemand') +print(df) + +#Forecast the Order Demand with LSTM Model + +df = data[(data['Date']>='2012-01-01') & (data['Date']<='2016-12-31')].sort_values('Date', ascending=True) +df = df.groupby('Date')['OrderDemand'].sum().reset_index() +print(df) + +# Visualize the order demand as time series +plt.figure(figsize=(16, 8)) +plt.title("Order Demand Graph") +plt.plot(df["Date"], df["OrderDemand"]) +plt.xlabel("Time", fontsize=14,) +plt.ylabel("Order Demand", fontsize=14) +plt.show() + + +# Create new data with only the "OrderDemand" column +orderD = df.filter(["OrderDemand"]) +# Convert the dataframe to a np array +orderD_array = orderD.values +# See the train data len +train_close_len = math.ceil(len(orderD_array) * 0.8) +print(train_close_len) + + +# Normalize the data +scaler = MinMaxScaler() +scaled_data = scaler.fit_transform(orderD_array) +print(scaled_data) + + + +# Create the training dataset +train_data = scaled_data[0 : train_close_len, :] +# Create X_train and y_train +X_train = [] +y_train = [] +for i in range(60, len(train_data)): + X_train.append(train_data[i - 60 : i, 0]) + y_train.append(train_data[i, 0]) + if i <= 60: + print(X_train) + print(y_train) + +# make X_train and y_train np array +X_train, y_train = np.array(X_train), np.array(y_train) + +# reshape the data +X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1)) +print(X_train.shape) + + +# create the testing dataset +test_data = scaled_data[train_close_len - 60 : , :] +# create X_test and y_test +X_test = [] +y_test = df.iloc[train_close_len : , :] +for i in range(60, len(test_data)): + X_test.append(test_data[i - 60 : i, 0]) + + +# convert the test data to a np array and reshape the test data +X_test = np.array(X_test) +X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1)) + + + +# Build the LSTM Model +model = Sequential() + +model.add(LSTM(units=512, return_sequences=True, activation='relu', input_shape=(X_train.shape[1], 1))) + + +model.add(LSTM(units=256, activation='relu', return_sequences=False)) + + +model.add(Dense(units=1)) + + +# compile the LSTM model +model.compile(optimizer="Adam", loss="mean_squared_error", metrics=['mae']) + + + +# train the LSTM model +model.fit(X_train, y_train, + epochs=3, + batch_size=100, + verbose=1) + + + +# predict with LSTM model +predictions = model.predict(X_test) +predictions = scaler.inverse_transform(predictions) + +# plot the data +train = orderD[:train_close_len] +valid = orderD[train_close_len:] +valid["Predictions"] = predictions +#visualize the data +plt.figure(figsize=(16, 8)) +plt.title("Forecast with LSTM Model") +plt.xlabel("Time", fontsize=14) +plt.ylabel("Order Demand", fontsize=14) +plt.plot(df["Date"][:train_close_len], train["OrderDemand"]) +plt.plot(df["Date"][train_close_len:], valid[["OrderDemand", "Predictions"]]) +plt.legend(["Train", "Validation", "Predictions"], loc="lower right") +plt.show() + + +# change the parameters of first LSTM model and build the Optimized LSTM Model +optimized_model = Sequential() + +optimized_model.add(LSTM(512, activation='relu', return_sequences=True, input_shape=(X_train.shape[1], 1))) + +optimized_model.add(LSTM(256, activation='relu', return_sequences=False)) + +optimized_model.add(Dense(128)) + +optimized_model.add(Dense(64)) + +optimized_model.add(Dense(32)) + +optimized_model.add(Dense(1)) + + +# compile the model +optimized_model.compile(optimizer="Adam", loss="mean_squared_error", metrics=['mae']) + + + +# train the optimized model +optimized_model.fit(X_train, y_train, + batch_size=32, + epochs=20, + verbose=1) + + + + +# Predict with optimized LSTM model +o_predictions = optimized_model.predict(X_test) +o_predictions = scaler.inverse_transform(o_predictions) + +# plot the data +train = orderD[:train_close_len] +valid = orderD[train_close_len:] +valid["Predictions"] = o_predictions +#visualize the data +plt.figure(figsize=(16, 8)) +plt.title("Forecast with Optimized LSTM Model") +plt.xlabel("Time", fontsize=14) +plt.ylabel("Order Demand", fontsize=14) +plt.plot(df["Date"][:train_close_len], train["OrderDemand"]) +plt.plot(df["Date"][train_close_len:], valid[["OrderDemand", "Predictions"]]) +plt.legend(["Train", "Validation", "Predictions"], loc="upper right") +plt.show() + + -- GitLab