def sarima1(input_df, kunag, matnr, n, l, p, d, q): index = str(kunag) + "-" + str(matnr) dfw = n_series(df, kunag, matnr) test1 = test(df, kunag, matnr) mae = [] order = (p, d, q) for i in range(0, l): k = n - i lst = [] train1, cv1 = train_cv_split(df, kunag, matnr, k, l) y_hat_avg = cv1.copy() for j in range(k, l, -1): train, cv = train_cv_split(df, kunag, matnr, j, l) dd = np.asarray(train["quantity"]) fit1 = sm.tsa.statespace.SARIMAX(train["quantity"], order=order, enforce_stationarity=False, enforce_invertibility=False, trend="n").fit() pred = fit1.predict(1) lst.append(pred.iloc[-1]) pd.DataFrame(lst) y_hat_avg['pred_column'] = lst rms = sqrt(mean_squared_error(test1.quantity, y_hat_avg.pred_column)) mae1 = mean_absolute_error(test1.quantity, y_hat_avg.pred_column) mae.append(mae1) l = l - 1 return mae
def ses1(input_df, kunag, matnr, n, l, alpha): index = str(kunag) + "-" + str(matnr) dfw = n_series(df, kunag, matnr) test1 = test(df, kunag, matnr) mae = [] for i in range(0, l): k = n - i lst = [] train1, cv1 = train_cv_split(df, kunag, matnr, k, l) y_hat_avg = cv1.copy() for j in range(k, l, -1): train, cv = train_cv_split(df, kunag, matnr, j, l) dd = np.asarray(train["quantity"]) fit2 = SimpleExpSmoothing(np.asarray(train['quantity'])).fit( smoothing_level=alpha, optimized=False) y_hat_avg['SES'] = fit2.forecast(len(cv1)) pred = y_hat_avg['SES'] lst.append(pred.iloc[-1]) pd.DataFrame(lst) y_hat_avg['pred_column'] = lst rms = sqrt(mean_squared_error(cv1.quantity, y_hat_avg.pred_column)) mae1 = mean_absolute_error(cv1.quantity, y_hat_avg.pred_column) mae.append(mae1) del y_hat_avg['SES'] l = l - 1 return mae
def moving_average1(input_df, kunag, matnr, n, l, roll): index = str(kunag) + "-" + str(matnr) dfw = n_series(df, kunag, matnr) test1 = test(df, kunag, matnr) mae = [] for i in range(0, l): k = n - i lst = [] train1, cv1 = train_cv_split(df, kunag, matnr, k, l) y_hat_avg = cv1.copy() for j in range(k, l, -1): train, cv = train_cv_split(df, kunag, matnr, j, l) dd = np.asarray(train["quantity"]) y_hat_avg['moving_avg_forecast'] = train['quantity'].rolling( roll).mean().iloc[-1] pred = y_hat_avg['moving_avg_forecast'] lst.append(pred.iloc[-1]) pd.DataFrame(lst) y_hat_avg['pred_column'] = lst rms = sqrt(mean_squared_error(cv1.quantity, y_hat_avg.pred_column)) mae1 = mean_absolute_error(cv1.quantity, y_hat_avg.pred_column) mae.append(mae1) del y_hat_avg['moving_avg_forecast'] l = l - 1 return mae
def main(input_df, kunag, matnr, n, l, roll, alpha, p, d, q): index = str(kunag) + "-" + str(matnr) dfw = n_series(df, kunag, matnr) test1 = test(df, kunag, matnr) mae = [] order1 = (0, 1, 1) order2 = (0, 1, 2) order3 = (1, 1, 1) lst = [] # print("count of predictions done") for i in range(0, 16): k = n - i lst1 = [] lst2 = [] lst3 = [] lst4 = [] lst5 = [] nlst = [] train1, cv1 = train_cv_split(df, kunag, matnr, k, l) y_hat_avg = cv1.copy() for j in range(k, l, -1): train, cv = train_cv_split(df, kunag, matnr, j, l) dd = np.asarray(train["quantity"]) #model 1 : MOVING AVERAGE y_hat_avg['moving_avg_forecast'] = train['quantity'].rolling( roll).mean().iloc[-1] pred1 = y_hat_avg['moving_avg_forecast'] lst1.append(pred1.iloc[-1]) #model 2 : SES fit1 = SimpleExpSmoothing(np.asarray(train['quantity'])).fit( smoothing_level=alpha, optimized=False) y_hat_avg['SES'] = fit1.forecast(len(cv1)) pred2 = y_hat_avg['SES'] lst2.append(pred2.iloc[-1]) #model 3 : ARIMA fit2 = sm.tsa.statespace.SARIMAX(train["quantity"], order=order1, enforce_stationarity=False, enforce_invertibility=False, trend="n").fit() pred3 = fit2.predict(1) lst3.append(pred3.iloc[-1]) #model 4 : ARIMA fit3 = sm.tsa.statespace.SARIMAX(train["quantity"], order=order2, enforce_stationarity=False, enforce_invertibility=False, trend="n").fit() pred4 = fit3.predict(1) lst4.append(pred4.iloc[-1]) #model 5 : ARIMA fit4 = sm.tsa.statespace.SARIMAX(train["quantity"], order=order3, enforce_stationarity=False, enforce_invertibility=False, trend="n").fit() pred5 = fit4.predict(1) lst5.append(pred4.iloc[-1]) pd.DataFrame(lst1) pd.DataFrame(lst2) pd.DataFrame(lst3) pd.DataFrame(lst4) pd.DataFrame(lst5) y_hat_avg['pred_ma'] = lst1 y_hat_avg['pred_ses'] = lst2 y_hat_avg['pred_arima011'] = lst3 y_hat_avg['pred_arima012'] = lst4 y_hat_avg['pred_arima111'] = lst5 rms1 = sqrt(mean_squared_error(test1.quantity, y_hat_avg.pred_ma)) mae1 = mean_absolute_error(test1.quantity, y_hat_avg.pred_ma) rms2 = sqrt(mean_squared_error(test1.quantity, y_hat_avg.pred_ses)) mae2 = mean_absolute_error(test1.quantity, y_hat_avg.pred_ses) rms3 = sqrt(mean_squared_error(test1.quantity, y_hat_avg.pred_arima011)) mae3 = mean_absolute_error(test1.quantity, y_hat_avg.pred_arima011) rms4 = sqrt(mean_squared_error(test1.quantity, y_hat_avg.pred_arima012)) mae4 = mean_absolute_error(test1.quantity, y_hat_avg.pred_arima012) rms5 = sqrt(mean_squared_error(test1.quantity, y_hat_avg.pred_arima111)) mae5 = mean_absolute_error(test1.quantity, y_hat_avg.pred_arima111) # print(mae1,mae2,mae3) nlst = [mae1, mae2, mae3, mae4, mae5] # print(nlst) m = min(nlst) # print(m) if (mae1 == m): # print("mae1:",mae1) y_hat_avg['moving_avg_forecast'] = train['quantity'].rolling( roll).mean().iloc[-1] pred = y_hat_avg['moving_avg_forecast'] elif (mae2 == m): # print("mae2:",mae2) fit1 = SimpleExpSmoothing(np.asarray(train['quantity'])).fit( smoothing_level=alpha, optimized=False) y_hat_avg['SES'] = fit1.forecast(len(cv1)) pred = y_hat_avg['SES'] elif (mae3 == m): # print("mae3:",mae3) fit2 = sm.tsa.statespace.SARIMAX(train["quantity"], order=order1, enforce_stationarity=False, enforce_invertibility=False, trend="n").fit() pred = fit2.predict(1) elif (mae4 == m): # print("mae4:",mae4) fit3 = sm.tsa.statespace.SARIMAX(train["quantity"], order=order2, enforce_stationarity=False, enforce_invertibility=False, trend="n").fit() pred = fit3.predict(1) elif (mae5 == m): # print("mae5:",mae5) fit4 = sm.tsa.statespace.SARIMAX(train["quantity"], order=order3, enforce_stationarity=False, enforce_invertibility=False, trend="n").fit() pred = fit4.predict(1) lst.append(pred.iloc[-1]) pd.DataFrame(lst) l = l - 1 y_hat_avg['pred_column'] = lst # plt.figure(figsize=(12,8)) # plt.plot( train.set_index("date")['quantity'], label='Train',marker = '.') # plt.plot(cv1.set_index("date")['quantity'], label='Test',marker = '.') # plt.plot(y_hat_avg.set_index("date")['pred_column'], label='MAIN' + "_" + str(order),marker = '.') # plt.legend(loc='best') # plt.title("MAIN") # plt.show() rms = sqrt(mean_squared_error(test1.quantity, y_hat_avg.pred_column)) mae = mean_absolute_error(test1.quantity, y_hat_avg.pred_column) del y_hat_avg["moving_avg_forecast"] del y_hat_avg["SES"] # print(y_hat_avg) return y_hat_avg, mae
def lstm(df, kunag, matnr, epoch, batch, order, lstm_units, verb, folder, train_=True): train, test = train_test_split(df, kunag, matnr, 16) dataframe = n_series(df, kunag, matnr) index = str(kunag) + "_" + str(matnr) order = order test_points = 16 df_testing_complete = dataframe[-16:] test_predictions = [] df = dataframe[:-test_points] df_training_complete = df df_training_processed = df_training_complete.iloc[:, 1:2].values scaler = MinMaxScaler(feature_range=(0, 1)) df_training_scaled = scaler.fit_transform(df_training_processed) for k in range(0, test_points): if (k == 0) and (train_): print("Training model for " + str(index) + "...") df = dataframe[:-test_points + k] df_training_complete = df df_training_processed = df_training_complete.iloc[:, 1:2].values scaler = MinMaxScaler(feature_range=(0, 1)) df_training_scaled = scaler.fit_transform(df_training_processed) features_set = [] labels = [] for i in range(order + 1, df.shape[0]): features_set.append(df_training_scaled[i - order:i, 0]) labels.append(df_training_scaled[i, 0]) features_set, labels = np.array(features_set), np.array(labels) features_set = np.reshape( features_set, (features_set.shape[0], features_set.shape[1], 1)) # print(features_set.size) model = Sequential() model.add( LSTM(units=lstm_units, return_sequences=False, input_shape=(features_set.shape[1], 1))) model.add(Dropout(0.2)) model.add(Dense(units=1)) model.compile(optimizer='adam', loss='mean_squared_error') model.fit(features_set, labels, epochs=epoch, batch_size=batch, verbose=verb) model_json = model.to_json() with open("pretrained/weights/model_(" + str(index) + ").json", "w") as json_file: json_file.write(model_json) # serialize weights to HDF5 model.save_weights("pretrained/weights/model_(" + str(index) + ").h5") print("Saved model to disk") # load json and create model json_file = open( "pretrained/weights/model_(" + str(index) + ").json", 'r') loaded_model_json = json_file.read() json_file.close() loaded_model = model_from_json(loaded_model_json) # load weights into new model loaded_model.load_weights("pretrained/weights/model_(" + str(index) + ").h5") print("Loaded model from disk") loaded_model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy']) elif k == 0 and not train_: # load json and create model json_file = open( "pretrained/weights/model_(" + str(index) + ").json", 'r') loaded_model_json = json_file.read() json_file.close() loaded_model = model_from_json(loaded_model_json) # load weights into new model loaded_model.load_weights("pretrained/weights/model_(" + str(index) + ").h5") print("Loaded model from disk") # evaluate loaded model on test data loaded_model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy']) df_testing_processed = df_testing_complete.iloc[k:k + 1, 1:2].values df_total = pd.concat((df_training_complete['quantity'], df_testing_complete['quantity']), axis=0) test_inputs = df_total[len(df_total) - len(df_testing_complete) - order + k:].values test_inputs = test_inputs.reshape(-1, 1) test_inputs = scaler.transform(test_inputs) test_features = [] test_features.append(test_inputs[1:order + 1, 0]) test_features = np.array(test_features) test_features = np.reshape( test_features, (test_features.shape[0], test_features.shape[1], 1)) predictions = loaded_model.predict(test_features) predictions = scaler.inverse_transform(predictions) test_predictions.append(predictions) test_predictions_1 = [i[0][0] for i in test_predictions] df_c = pd.DataFrame(data=test_predictions_1) test.reset_index(inplace=True) pred = pd.concat([test, df_c], axis=1, join_axes=[test.index]) pred.set_index("date", inplace=True) pred.drop(['index', 'quantity'], axis=1, inplace=True) test.drop(['index'], axis=1, inplace=True) mae = mean_absolute_error(test.quantity, pred[0]) rms = sqrt(mean_squared_error(test.quantity, pred[0])) print("mae :", mae) print("rms :", rms) dataframe.set_index('date', inplace=True) train.set_index('date', inplace=True) test.set_index('date', inplace=True) # plt.figure(figsize=(16,8)) # plt.plot(dataframe, marker='.', color='blue') # plt.plot(train, marker='.', color='blue',label = "Train") # plt.plot(test, marker='.', color='orange',label = "Test") # plt.plot(pred,marker = ".",color = 'green',label = "Prediction") # plt.xlabel("time") # plt.ylabel('quantity') # plt.legend(loc='best') # plt.title("batch : " + str(batch)+"_" + "epochs : " + str(epoch)) # plt.savefig(folder+"/" + 'Graph_{}_{}_{}_{}.png'.format(index,batch,epoch,lstm_units), format="PNG") return pred, mae
def train_test_split(input_df, kunag, matnr, i): data = n_series(input_df, kunag, matnr) train = data[0:-i] test = data[-i:] return train, test
def main1(input_df, kunag, matnr, n, l, roll, alpha, p, d, q): index = str(kunag) + "-" + str(matnr) dfw = n_series(df, kunag, matnr) test1 = test(df, kunag, matnr) mae = [] order = (p, d, q) lst = [] # print("count of predictions done") for i in range(0, l): k = n - i lst1 = [] lst2 = [] lst3 = [] nlst = [] train1, cv1 = train_cv_split(df, kunag, matnr, k, l) y_hat_avg = cv1.copy() for j in range(k, l, -1): train, cv = train_cv_split(df, kunag, matnr, j, l) dd = np.asarray(train["quantity"]) y_hat_avg['moving_avg_forecast'] = train['quantity'].rolling( roll).mean().iloc[-1] pred1 = y_hat_avg['moving_avg_forecast'] lst1.append(pred1.iloc[-1]) fit2 = SimpleExpSmoothing(np.asarray(train['quantity'])).fit( smoothing_level=alpha, optimized=False) y_hat_avg['SES'] = fit2.forecast(len(cv1)) pred2 = y_hat_avg['SES'] lst2.append(pred2.iloc[-1]) fit1 = sm.tsa.statespace.SARIMAX(train["quantity"], order=order, enforce_stationarity=False, enforce_invertibility=False, trend="n").fit() pred3 = fit1.predict(1) lst3.append(pred3.iloc[-1]) pd.DataFrame(lst1) pd.DataFrame(lst2) pd.DataFrame(lst3) y_hat_avg['pred_ma'] = lst1 y_hat_avg['pred_ses'] = lst2 y_hat_avg['pred_arima'] = lst3 rms1 = sqrt(mean_squared_error(test1.quantity, y_hat_avg.pred_ma)) mae1 = mean_absolute_error(test1.quantity, y_hat_avg.pred_ma) rms2 = sqrt(mean_squared_error(test1.quantity, y_hat_avg.pred_ses)) mae2 = mean_absolute_error(test1.quantity, y_hat_avg.pred_ses) rms3 = sqrt(mean_squared_error(test1.quantity, y_hat_avg.pred_arima)) mae3 = mean_absolute_error(test1.quantity, y_hat_avg.pred_arima) nlst = [mae1, mae2, mae3] m = min(nlst) if (mae1 == m): y_hat_avg['moving_avg_forecast'] = train['quantity'].rolling( roll).mean().iloc[-1] pred = y_hat_avg['moving_avg_forecast'] elif (mae2 == m): fit2 = SimpleExpSmoothing(np.asarray(train['quantity'])).fit( smoothing_level=alpha, optimized=False) y_hat_avg['SES'] = fit2.forecast(len(cv1)) pred = y_hat_avg['SES'] elif (mae3 == m): fit1 = sm.tsa.statespace.SARIMAX(train["quantity"], order=order, enforce_stationarity=False, enforce_invertibility=False, trend="n").fit() pred = fit1.predict(1) lst.append(pred.iloc[-1]) # print(len(lst) ) pd.DataFrame(lst) l = l - 1 y_hat_avg['pred_column'] = lst # plt.figure(figsize=(12,8)) # plt.plot( train.set_index("date")['quantity'], label='Train',marker = '.') # plt.plot(cv1.set_index("date")['quantity'], label='Test',marker = '.') # plt.plot(y_hat_avg.set_index("date")['pred_main'], label='MAIN' + "_" + str(order),marker = '.') # plt.legend(loc='best') # plt.title("MAIN") # plt.show() rms = sqrt(mean_squared_error(test1.quantity, y_hat_avg.pred_column)) mae = mean_absolute_error(test1.quantity, y_hat_avg.pred_column) del y_hat_avg["moving_avg_forecast"] del y_hat_avg["SES"] # print(y_hat_avg) print("mae :", mae) return y_hat_avg, mae