i = 0 # Perform dimensionality reduction on the feature vectors pca = PCA(n_components=dimensions[x]) pca.fit(X_train) xTrainRed = pca.transform(X_train) xTestRed = pca.transform(X_test) predicted_values = fit_predict(xTrainRed, y_train, xTestRed, 40, neurons[x]) mape = statistics.mape((y_test + shifted_value) * 1000, (predicted_values + shifted_value) * 1000) print('MAPE is ', mape) mae = statistics.mae((y_test + shifted_value) * 1000, (predicted_values + shifted_value) * 1000) print('MAE is ', mae) mse = statistics.meanSquareError((y_test + shifted_value) * 1000, (predicted_values + shifted_value) * 1000) print('MSE is ', mse) rmse = math.sqrt(mse) print('RMSE is ', rmse) nrmse = statistics.normRmse((y_test + shifted_value) * 1000, (predicted_values + shifted_value) * 1000) print('NRMSE is ', nrmse) print (i+1) preds.append(predicted_values) # show fig = plt.figure() colors = ["g","r","b","c","m","y","k","w"] legendVars = [] for j in range(len(preds)): print(j) x, = plt.plot(preds[j]+shifted_value, color=colors[j]) legendVars.append(x) plt.xlabel('Hour') plt.ylabel('Electricity load, kW') plt.legend(legendVars, names) plt.show()
plt.show() ''' # show result of predictions # pr = pred2.predicted_mean['2016-07-10 19:00:00':'2016-07-31 23:00:00']+shifted_value # ax = (data['2016-07-10 19:00:00':'2016-07-31 23:00:00']+shifted_value).plot(figsize=(20, 16)) pr = pred2.predicted_mean['2016-12-01 22:00:00':'2017-05-16 21:00:00'] ax = (data['2016-12-01 22:00:00':'2017-05-16 21:00:00']).plot(figsize=(20, 16)) # plot the results fig = plt.figure() plt.plot(ax, label="$true$", c='green') plt.plot(pr, label="$predict$", c='red') plt.xlabel('Hour') plt.ylabel('Electricity load (*1e3)') plt.legend() plt.show() fig.savefig('../result/sarimax.jpg', bbox_inches='tight') # quantify the accuracy of the prediction prediction = pred2.predicted_mean['2016-12-01 22:00:00':'2017-05-16 21:00:00'].values # flatten nested list truth = list(itertools.chain.from_iterable(test_data.values)) # evaluation mape = np.mean(np.abs((truth - prediction) / truth)) print('mape is {:.5f}'.format(mape)) mae = statistics.mae(truth,prediction) print('MAE is ', mae) mse = statistics.meanSquareError(truth,prediction) print('MSE is ', mse) rmse = statistics.Rmse(mse) print('RMSE is ', rmse) nrmse = statistics.normRmse(truth,prediction) print('NRMSE is ', nrmse)
mape = statistics.mape([(y_test_true[i] + shifted_value) * 1000 for i in range(0, len(y_test_true))], (predicted_values + shifted_value) * 1000) print('MAPE is ', mape) mae = statistics.mae([(y_test_true[i] + shifted_value) * 1000 for i in range(0, len(y_test_true))], (predicted_values + shifted_value) * 1000) print('MAE is ', mae) mse = statistics.meanSquareError([(y_test_true[i] + shifted_value) * 1000 for i in range(0, len(y_test_true))], (predicted_values + shifted_value) * 1000) print('MSE is ', mse) rmse = math.sqrt(mse) print('RMSE is ', rmse) nrmse = statistics.normRmse([(y_test_true[i] + shifted_value) * 1000 for i in range(0, len(y_test_true))], (predicted_values + shifted_value) * 1000) print('NRMSE is ', nrmse) fig = plt.figure(figsize=(12, 9), dpi=100) plt.subplot(2, 1, 1) plt.plot(ca_y_test, label="$lowpass_real$", c='green') plt.plot(ca_pred, label="$lowpass_prediction$", c='red') plt.legend(['lowpass_real', 'lowpass_prediction'], loc='upper right') plt.title('lowpass coefficient prediction result', fontsize=16) plt.subplot(2, 1, 2) # plt.plot(cd[train_row:], label="$highpass_real$", c='green') plt.plot(cd_y_test, label="$highpass_real$", c='green') plt.plot(cd_pred, label="$highpass_prediction$", c='red') plt.legend(['highpass_real', 'highpass_prediction'], loc='upper right') plt.title('highpass coefficient prediction result', fontsize=16)
# save model model.save('../model/seq2seq_code.h5') model = load_model('../model/seq2seq_code.h5') # evaluate on some new patterns result = model.predict(X_test, batch_size=50, verbose=0) # calculate error,evaluate the result predicted = one_hot_decode(result, series_min, series_max, n_unique) # plot the results fig = plt.figure() plt.plot(test[:, n_in:] + shifted_value, label="$true$", c='green') plt.plot(predicted + shifted_value, label="$predict$", c='red') plt.xlabel('Hour') plt.ylabel('Electricity load') plt.legend() plt.show() fig.savefig('../result/seq2seq_code_result.jpg', bbox_inches='tight') # evaluation mape = statistics.mape(test[:, n_in:] + shifted_value, predicted + shifted_value) print('MAPE is ', mape) nrmse = statistics.normRmse(test[:, n_in:] + shifted_value, predicted + shifted_value) print('NRMSE is ', nrmse) mae = statistics.mae(test[:, n_in:] + shifted_value, predicted + shifted_value) print('MAE is ', mae) mse = statistics.mse(test[:, n_in:] + shifted_value, predicted + shifted_value) print('MSE is ', mse) rmse = sqrt(mse) print('RMSE is ', rmse)