# Launch the graph with tf.Session() as sess: sess.run(init) batch_size = 1 # Training cycle for epoch in range(25): avg_cost = 0. for batch in range(int(len(training_X) / batch_size)): x_batch = training_X[batch * batch_size:batch * batch_size + batch_size] #tf.nn.batch_normalization(x_batch) y_batch = training_Y[batch * batch_size:batch * batch_size + batch_size] #tf.nn.batch_normalization(y_batch) # Run optimization op (backprop) and cost op (to get loss value) _, c = sess.run([optimizer, cost_func], feed_dict={ x: x_batch, y: y_batch }) # Compute average loss avg_cost += c / int(len(training_X) / batch_size) print(avg_cost) # Test model prediction = pred.eval(feed_dict={x: testing_X}, session=sess) mape = eval.mape(prediction, testing_Y) print(mape) print(np.mean(mape))
def run_regression(X_train, Y_train, X_test, Y_test, lambda_value=0.1, normalize=False, batch_size=10, alpha=1e-8): x_train = feature_normalize(X_train) if normalize else X_train y_train = Y_train x_test = X_test y_test = Y_test session = tf.Session() number_rows = training_X.shape[0] number_col_x = training_X.shape[1] number_col_y = training_Y.shape[1] X = tf.placeholder('float', [None, number_col_x], name="X") Y = tf.placeholder('float', [None, number_col_y], name="Y") theta = tf.Variable(tf.random_normal([number_col_x, number_col_y], stddev=0.01), name="Theta") lambda_val = tf.constant(lambda_value) y_predicted = tf.matmul(X, theta) with tf.name_scope('cost') as scope: cost_func = (tf.nn.l2_loss(y_predicted - Y) + lambda_val * tf.nn.l2_loss(theta)) / float(batch_size) cost_summary = tf.summary.scalar('cost', cost_func) training_func = tf.train.GradientDescentOptimizer(alpha).minimize( cost_func) with tf.name_scope("test") as scope: correct_prediction = tf.subtract( tf.cast(1, 'float'), tf.reduce_mean(tf.subtract(y_predicted, Y))) accuracy = tf.cast(correct_prediction, "float") saver = tf.train.Saver() merged = tf.summary.merge_all() writer = tf.summary.FileWriter("/tmp", session.graph) init = tf.global_variables_initializer() session.run(init) for i in range(1, int(len(x_train) / batch_size)): session.run(training_func, feed_dict={ X: x_train[i * batch_size:i * batch_size + batch_size], Y: y_train[i * batch_size:i * batch_size + batch_size] }) if i % batch_size == 0: print("test accuracy %g" % session.run(accuracy, feed_dict={ X: x_test, Y: y_test })) print("final test accuracy %g" % session.run(accuracy, feed_dict={ X: x_test, Y: y_test })) prediction = y_predicted.eval(feed_dict={X: x_test}, session=session) mape = evaluation.mape(prediction, y_test) mean_mape = np.mean(np.array(mape)) print("MAPE: %g" % mean_mape) session.close()
import src.vector_gen.generate_VectorY as vecY import src.misc.split_train_valid as split import src.misc.paths as path from sklearn import linear_model import pandas as pd import numpy as np from sklearn.externals import joblib from src.misc import evaluation as eval np.set_printoptions(threshold=np.nan) df = pd.read_csv(path.trajectories_training_file2) #X_train, X_test, Y_train, Y_test = model_selection.train_test_split(X,Y,test_size=0.2) training, validation, testing = split.split_dataset(df) X_train = vecX.generate_x_df(training) Y_train = vecY.generate_VectorY_df(training) X_test = vecX.generate_x_df(testing) Y_test = vecY.generate_VectorY_df(testing) clf = linear_model.MultiTaskElasticNet() clf.fit(X_train, Y_train) Y_pred = clf.predict(X_test) error = eval.mape(Y_pred, Y_test) print(error) print(np.mean(np.array(error)))
#joblib.dump(regr_multi_svr, 'regr_multi_svr_timeInformation.pkl') # # evaluate y_test_predict = regr_multi_svr.predict(x_test) y_test_predict_df = pd.DataFrame(y_test_predict, columns=y_cols) y_test_predict_df.head() from sklearn import metrics print(metrics.mean_absolute_error(y_test_predict, y_test)) print(metrics.mean_squared_error(y_test_predict, y_test)) import src.misc.evaluation as ev mymape = ev.mape(y_test_predict, y_test) print(np.mean(np.array(mymape))) # #plot # import matplotlib.pyplot as plt # alpha=0.8 # lw=0.7 # # fig, ax = plt.subplots(figsize = (26,8)) # # ax.scatter(res.index.values, res['y_test'], color='green', label='y_test', s=0.8) # # ax.plot(res.index.values, res['y_pred_rbf'], color='red', label='y_pred_rbf', alpha=alpha, lw=lw) # #plt.plot(res.index.values, res['y_pred_sigmoid'], color='blue', label='y_pred_sigmoid', alpha=alpha, lw=lw) # ax.plot(res.index.values, res['y_pred_lin'], color='orange', label='y_pred_lin', alpha=alpha, lw=lw) # #plt.plot(res.index.values, res['y_pred'], color='darkorange', label='y_pred_rbf')
testing_X = pd.read_csv( "../../../../python/traffic-prediction/src/misc/splitting_csv_files/test_X.csv", index_col=0) #------------------------------------------------------------------------------- # Linear Model LR = linear_model.LinearRegression() LR.fit(training_X, training_Y) print("\n Linear Regression: \n") print("Coefficients b0.0,...,b0.6:") print(LR.intercept_) print("Coefficients b1.1,...,b6.6:") print(LR.coef_) print("R^2 score: %.5f" % LR.score(testing_X, testing_Y)) mape_LR = evaluation.mape(LR.predict(testing_X), testing_Y) mean_mape_LR = np.mean(np.array(mape_LR)) print("MAPE's:") print(mape_LR) print("Mean of MAPE's: %.5f" % mean_mape_LR) #------------------------------------------------------------------------------- # Partial leas squares regression pls = PLSRegression(n_components=2) pls.fit(training_X, training_Y) print("\n PLS Regression: \n") print("Coefficients b1.1,..,b6.6:") print(pls.coef_) print("R^2 score: %.5f" % pls.score(testing_X, testing_Y)) mape_pls = evaluation.mape(pls.predict(testing_X), testing_Y)
epoch_loss += c [train_accuracy] = sess.run([cost_func], feed_dict={ x: x_batch, y: y_batch }) print("epoch %d, loss %d, training accuracy %g" % (epoch, epoch_loss, train_accuracy)) s = sess.run(merged_summary, feed_dict={x: x_batch, y: y_batch}) writer.add_summary(s, epoch) print('Epoch', epoch, 'loss', epoch_loss) # TODO FALSCH!!!??? prediction = y_pred.eval(feed_dict={x: testing_X}, session=sess) mape = evaluation.mape(prediction, testing_Y) print('mean MAPE\n', np.mean(mape)) print('MAPE\n', mape) # In[7]: #pd.DataFrame(prediction, index=testing_Y.index, columns=testing_Y.columns) # In[8]: #testing_Y
from sklearn.multioutput import MultiOutputRegressor import pandas as pd import sklearn.svm as svm from src.misc.evaluation import mape import numpy as np x_train = pd.read_csv('train_X.csv', index_col =0) x_test = pd.read_csv('test_X.csv', index_col =0) y_train = pd.read_csv('train_Y.csv', index_col =0) y_test = pd.read_csv('test_Y.csv', index_col =0) clf = svm.SVR(C=8, epsilon=0.0, cache_size=2000, kernel='rbf', tol=0.001, verbose=False) regr_multi_svr = MultiOutputRegressor(clf, n_jobs=-1) regr_multi_svr.fit(x_train, y_train) y_test_predict = regr_multi_svr.predict(x_test) mymape = mape(y_test_predict, y_test) print(np.mean(np.array(mymape)))
def test_get_simple_result(self): y_true = np.array([1, 2, 3, 4]) y_pred = np.array([-1, -2, -3, -4]) self.assertEqual(evaluation.mape(y_true, y_pred), 2)
def self_test(self): y_true = np.array([1, 2, 3, 4]) self.assertEqual(evaluation.mape(y_true, y_true), 0.0)