epochs=EPOCHS, validation_data=(X_valid, y_valid), callbacks=[earlystop], verbose=1) # Test the model X_test = test_inputs['X'] y1_test = test_inputs['target_load'] y1_preds = model.predict(X_test) y1_test = y_scaler.inverse_transform(y1_test) y1_preds = y_scaler.inverse_transform(y1_preds) y1_test, y1_preds = flatten_test_predict(y1_test, y1_preds) mse = mean_squared_error(y1_test, y1_preds) rmse_predict = RMSE(mse) evs = explained_variance_score(y1_test, y1_preds) mae = mean_absolute_error(y1_test, y1_preds) mse = mean_squared_error(y1_test, y1_preds) meae = median_absolute_error(y1_test, y1_preds) r_square = r2_score(y1_test, y1_preds) mape_v = mape(y1_preds.reshape(-1, 1), y1_test.reshape(-1, 1)) # print("mse:", mse, 'rmse_predict:', rmse_predict, "mae:", mae, "mape:", mape_v, "r2:", r_square, # "meae:", meae, "evs:", evs) print('rmse_predict:', rmse_predict, "evs:", evs, "mae:", mae, "mse:", mse, "meae:", meae, "r2:", r_square, "mape", mape_v)
predictions = model.predict(X_test) # compare predictions with actual ones eval_df = pd.DataFrame(predictions, columns=['t+' + str(t) for t in range(1, HORIZON + 1)]) eval_df['timestamp'] = test_shifted.index eval_df = pd.melt(eval_df, id_vars='timestamp', value_name='prediction', var_name='h') eval_df['actual'] = np.transpose(y_test).ravel() eval_df[['prediction', 'actual' ]] = scaler.inverse_transform(eval_df[['prediction', 'actual']]) eval_df.head() # Compute the mean absolute percentage error over all predictions mape(eval_df['prediction'], eval_df['actual']) # plot the result eval_df[eval_df.timestamp < '2014-11-08'].plot(x='timestamp', y=['prediction', 'actual'], style=['r', 'b'], figsize=(15, 8)) plt.xlabel('timestamp', fontsize=12) plt.ylabel('load', fontsize=12) plt.show() ## clean up the model for m in glob('model_*.h5'): os.remove(m) print("done")
X_train = X_train.sort_index() y_train = y_train.sort_index() regressor = LinearRegression() regressor.fit(X_train, y_train) #training the algorithm y_pred = regressor.predict(X_test) # print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, y_pred)) # print('Mean Squared Error:', metrics.mean_squared_error(y_test, y_pred)) # print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, y_pred))) # print("R2:", metrics.r2_score(y_test, y_pred)) predicted_Y_entire = regressor.predict(X) print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, y_pred)) print('Mean Squared Error:', metrics.mean_squared_error(y_test, y_pred)) print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, y_pred))) mape_v = mape(y_pred.reshape(-1, 1), y_test.values.reshape(-1, 1)) print('mape:', mape_v) r2 = metrics.r2_score(y_test, y_pred) print("R2:", r2) store_predict_points( Y_entire, predicted_Y_entire, 'output/test_linear_without_prediction_r2_' + str(r2) + '.csv') # df = pd.DataFrame({'Actual': y_test, 'Predicted': y_pred}) # df.plot(kind='bar',figsize=(10, 8)) # plt.grid(which='major', linestyle='-', linewidth='0.5', color='green') # plt.grid(which='minor', linestyle=':', linewidth='0.5', color='black') # plt.show()
if index % 10 == 0: print("rebuilding the model", index) model = ARIMA(history, order=(time_step_lag, 1, 0)) model_fit = model.fit(disp=0) output = model_fit.forecast() yhat = output[0] predictions.append(yhat) obs = test[t] history.append(obs) print('predicted=%f, expected=%f' % (yhat, obs)) error = mean_squared_error(test, predictions) print('Test MSE: %.3f' % error) # plot # plt.plot(test) # plt.plot(predictions, color='red') # plt.show() mse = mean_squared_error(test, predictions) rmse_predict = RMSE(mse) evs = explained_variance_score(test, predictions) mae = mean_absolute_error(test, predictions) meae = median_absolute_error(test, predictions) predictions = np.array(predictions) r_square = r2_score(test, predictions) mape_v = mape(predictions.reshape(-1, 1), test.reshape(-1, 1)) print("mse:", mse, 'rmse_predict:', rmse_predict, "mae:", mae, "mape:", mape_v, "r2:", r_square, "meae:", meae, "evs:", evs)
look_back_dt = dt.datetime.strptime( test_start_dt, '%Y-%m-%d %H:%M:%S') - dt.timedelta(hours=T - 1) test = energy.copy()[test_start_dt:][['load', 'temp']] test[['load', 'temp']] = X_scaler.transform(test) test_inputs = TimeSeriesTensor(test, 'load', HORIZON, tensor_structure) predictions = model.predict(test_inputs['X']) eval_df = create_evaluation_df(predictions, test_inputs, HORIZON, y_scaler) eval_df.head() eval_df['APE'] = (eval_df['prediction'] - eval_df['actual']).abs() / eval_df['actual'] eval_df.groupby('h')['APE'].mean() print("MAPE:", mape(eval_df['prediction'], eval_df['actual'])) plot_df = eval_df[(eval_df.timestamp < '2014-11-08') & (eval_df.h == 't+1')][['timestamp', 'actual']] for t in range(1, HORIZON + 1): plot_df['t+' + str(t)] = eval_df[(eval_df.timestamp < '2014-11-08') & (eval_df.h == 't+' + str(t))]['prediction'].values fig = plt.figure(figsize=(15, 8)) ax = plt.plot(plot_df['timestamp'], plot_df['actual'], color='red', linewidth=4.0) ax = fig.add_subplot(111) ax.plot(plot_df['timestamp'],