def main(): loss_norm = [] loss_difference = [] countries = [ 'Afghanistan', 'Indien', 'Irak', 'Kolumbien', 'Pakistan', 'Philippinen', 'sandbox_attacks', 'test_exp_chirp' ] for country in countries: test_len = 30 data_name = country data_dir = '../../' + data_name + '.csv' train_data, train_date, test_data, test_date, std = prepare_data( data_dir, test_len=test_len, normalize=False) scale_down_mean = np.mean(train_data[-365 - 365 + test_len:-365]) scale_down_std = np.std(train_data[-365 - 365 + test_len:-365]) scale_up_mean = np.mean(train_data[-365 + 30:]) scale_up_std = np.std(train_data[-365 + 30:]) prediction = ((train_data[-365:-365 + 30] - scale_down_mean) / scale_down_std) * scale_up_std + scale_up_mean loss_norm.append(normed_loss(prediction, test_data)) loss_difference.append(differential_loss(prediction, test_data)) plt.figure() plt.plot_date(test_date, test_data, xdate=True, label='Labels', ls="-") plt.plot_date(test_date, prediction, xdate=True, label='Predictions', ls="-") plt.xticks(rotation="vertical") plt.title('Prediction') plt.legend() plt.xlabel('Days') plt.ylabel('Attack') save_fig(data_name, './Images/') loss_dict = { 'Countries': countries, 'Normed_loss': loss_norm, 'Differential_loss': loss_difference } pd.DataFrame(loss_dict).to_csv('./Last_year_scaling_loss.csv')
def main(): loss_norm = [] loss_difference = [] countries = [ 'Afghanistan', 'Indien', 'Irak', 'Kolumbien', 'Pakistan', 'Philippinen', 'sandbox_attacks', 'test_exp_chirp' ] for country in countries: data_name = country data_dir = '../../' + data_name + '.csv' train_data, train_date, test_data, test_date, std = prepare_data( data_dir, test_len=30, normalize=False) weight_aray = np.linspace(0, 1, 100) feed = train_data[-100:].flatten() pred = np.divide(np.sum(np.multiply(weight_aray, feed)), np.sum(weight_aray)) prediction = np.ones((len(test_data))) * pred loss_norm.append(normed_loss(prediction, test_data)) loss_difference.append(differential_loss(prediction, test_data)) plt.figure() plt.plot_date(test_date, test_data, xdate=True, label='Labels', ls="-") plt.plot_date(test_date, prediction, xdate=True, label='Predictions', ls="-") plt.xticks(rotation="vertical") plt.title('Prediction') plt.legend() plt.xlabel('Days') plt.ylabel('Attack') save_fig(data_name, './Images/') loss_dict = { 'Countries': countries, 'Normed_loss': loss_norm, 'Differential_loss': loss_difference } pd.DataFrame(loss_dict).to_csv('./Average_loss.csv')
def main(): loss_norm = [] loss_difference = [] countries = [ 'Afghanistan', 'Indien', 'Irak', 'Kolumbien', 'Pakistan', 'Philippinen', 'sandbox_attacks', 'test_exp_chirp' ] for country in countries: data_name = country data_dir = '../../' + data_name + '.csv' train_data_scaled, train_date, test_data, test_date, std = prepare_data( data_dir, normalize=True, scaling='minmax') model_dir = './model/' num_epoch = 1000 n_steps = 100 n_inputs = 1 n_neurons = 30 n_layers = 1 print(country) sess, train_loss, epoch_count = training(train_data_scaled, model_dir, num_epoch=num_epoch, n_steps=n_steps, n_inputs=n_inputs, n_neurons=n_neurons, n_layers=n_layers) print() prediction, true_labels, label_dates = predict(test_data, test_date, sess, std, model_dir, n_steps, n_inputs) # rescaled_prediction = std.inverse_transform(prediction.reshape(-1,1)) # rescaled_labels = std.inverse_transform(true_labels.reshape(-1,1)) loss_norm.append(normed_loss(prediction, true_labels)) loss_difference.append(differential_loss(prediction, true_labels)) plt.figure() plt.subplot(211) plt.plot(epoch_count, train_loss) plt.title('Training loss') plt.xlabel('Epoch') plt.ylabel('Training MSE') plt.subplot(212) plt.plot_date(label_dates, true_labels, xdate=True, label='Labels', ls="-") plt.plot_date(label_dates, prediction, xdate=True, label='Predictions', ls="-") plt.xticks(rotation="vertical") plt.title('Prediction') plt.legend() plt.xlabel('Days') plt.ylabel('Attack') save_fig('predicted value feedback' + data_name, './Images/') loss_dict = { 'Countries': countries, 'Normed_loss': loss_norm, 'Differential_loss': loss_difference } pd.DataFrame(loss_dict).to_csv('./RNN_loss.csv')
data_check = pd.read_csv("../random_new.csv",index_col=0) data_check_val = data_check.values print(data_check_val.shape) random_check_set = data_check_val[:,1:] random_check_dates = data_check_val[:,0] print(random_check_set.shape) print(random_check_dates.shape) #std = StandardScaler() random_check_std = std.transform(random_check_set) #%% img_dir = "../../../images/random_new/with_features/" with tf.Session() as sess: saver.restore(sess,'../saved_model/1_complex/with_features/') for i in range(5): X_check, y_check, date_check = fetch_batch(random_check_std,random_check_dates, 1, l, w, pred_window) #X_check, y_check = fetch_batch(test_set, 1, l, w, pred_window) prediction = sess.run(output,feed_dict={X:X_check, y: y_check}) plt.figure() plt.plot_date(date_check.reshape(-1),y_check[0,:],xdate=True,label='actual',ls="-") plt.plot_date(date_check.reshape(-1),prediction[0,:],xdate=True,label='predictions',ls="-") plt.xticks(rotation="vertical") plt.legend() plt.xlabel('Days') plt.ylabel('Attack') save_fig(i,img_dir)
train_error = sess.run(mse, feed_dict = {X:X_batch[:,:,:,2:], y: y_batch}) test_x, test_y,_ = fetch_batch(test_std,test_date, 1, l, w, pred_window) #test_x, test_y = fetch_batch(test_set, 1, l, w, pred_window) test_error = sess.run(mse, feed_dict = {X:test_x[:,:,:,2:], y: test_y}) print("Epoch: ",epoch, " Training error: ", train_error, " Test error: ", test_error) saver.save(sess,directory) #plt.figure() #plt.plot() #print(mse) #%% #X_check, y_check = fetch_batch(test_set, 1, l, w, pred_window) img_dir = "../../../images/1_complex/only_attack/" with tf.Session() as sess: saver.restore(sess,directory) for i in range(10): X_check, y_check, date_check = fetch_batch(test_std,test_date, 1, l, w, pred_window) #X_check, y_check = fetch_batch(test_set, 1, l, w, pred_window) prediction = sess.run(output,feed_dict={X:X_check[:,:,:,2:], y: y_check}) plt.figure() plt.plot_date(date_check.reshape(-1),y_check[0,:],xdate=True,label='actual',ls="-") plt.plot_date(date_check.reshape(-1),prediction[0,:],xdate=True,label='predictions',ls="-") plt.xticks(rotation="vertical") plt.legend() plt.xlabel('Days') plt.ylabel('Attack') save_fig(i+10,img_dir)