Beispiel #1
0
from sklearn.grid_search import GridSearchCV

from data.TrafficFeeder import TrafficFeeder
from experiments.initializer import *
from utils.SplitTrainTest import SplitTrainTest

param_dicts = {
    "Q": np.arange(0.01, 0.1, step=0.01),
    "epsilon": np.arange(0.1, 1.0, step=0.05)
}
n_sliding_window = 4
n_periodic = 1
n_input = n_sliding_window + n_periodic
neural_shape = [n_input, 15, 1]
dataFeeder = TrafficFeeder()
X, y = dataFeeder.fetch_traffic_training(n_sliding_window, n_periodic,
                                         (40, 46))
estimator = ACOEstimator()
archive_solution = construct_solution(estimator.number_of_solutions,
                                      neural_shape)
cv = SplitTrainTest(X.shape[0])
fit_param = {'neural_shape': neural_shape, "archive": archive_solution}
# estimator.fit(X,y,**fit_param)
gs = GridSearchCV(estimator,
                  param_grid=param_dicts,
                  cv=cv,
                  n_jobs=-1,
                  fit_params=fit_param,
                  scoring='mean_squared_error')
gs.fit(X, y)
print gs.best_estimator_
print gs.best_estimator_.score(X, y)
    plt.show()


if __name__ == '__main__':
    best_estimator = None
    best_score = np.Inf
    for loop in np.arange(1, 20):
        n_input = 4
        n_periodic = 1
        n_hidden = 15
        neural_shape = [n_input + n_periodic, n_hidden, 1]
        Q = 0.09
        epsilon = 0.55

        dataFeeder = TrafficFeeder()
        X_train, y_train = dataFeeder.fetch_traffic_training(
            n_input, 1, (40, 46))
        X_test, y_test = dataFeeder.fetch_traffic_test(n_input, 1, (46, 48))
        # retrieve = [n_input+1,(X_train,y_train,X_test,y_test)]
        acoNet = ACOEstimator(Q=Q, epsilon=epsilon)
        fit_param = {"neural_shape": neural_shape}
        acoNet.fit(X_train, y_train, **fit_param)
        fit_param["weights_matrix"] = acoNet.best_archive
        neuralNet = NeuralFlowRegressor()
        neuralNet.fit(X_train, y_train, **fit_param)
        y_pred = dataFeeder.convert(neuralNet.predict(X_test))
        score = np.sqrt(mean_squared_error(y_pred, y_test))
        if (score < best_score):
            best_estimator = acoNet
            print score
        # plot_fig(y_pred,y_test)
    print best_score, best_estimator
Beispiel #3
0
import numpy as np

if __name__ == '__main__':
    np.random.seed(7)

    n_input = 4
    n_periodic = 0
    n_hidden = 15
    neural_shape = [n_input + n_periodic, n_hidden, 1]

    cross_rate = 0.6
    mutation_rate = 0.04
    pop_size = 50

    dataFeeder = TrafficFeeder()
    X_train, y_train = dataFeeder.fetch_traffic_training(
        n_input, n_periodic, (40, 47))
    X_test, y_test = dataFeeder.fetch_traffic_test(n_input, n_periodic,
                                                   (48, 50))
    # retrieve = [n_input+1,(X_train,y_train,X_test,y_test)]
    gaEstimator = GAEstimator(cross_rate=cross_rate,
                              mutation_rate=mutation_rate,
                              pop_size=pop_size)
    fit_param = {"neural_shape": neural_shape}
    gaEstimator.fit(X_train, y_train, **fit_param)
    fit_param["weights_matrix"] = gaEstimator.best_archive
    neuralNet = NeuralFlowRegressor()
    neuralNet.fit(X_train, y_train, **fit_param)
    y_pred = dataFeeder.convert(neuralNet.predict(X_test))
    print "done"
    print sqrt(mean_squared_error(y_pred, y_test))
    plot_figure(y_pred, y_test)