Example #1
0
def test_case_3(data, Speed):
    # test case 3: non-recurrent morning congestion
    Fri, _, _, fri, _ = get_certain_dayofweek(data, Speed, 4)

    fig3 = plt.figure(figsize=(15, 8))
    fig3.suptitle(
        'test case 3 (non-recurrent morning congestion): 12-16-2016, Friday')
    ax = plt.subplot(2, 2, 1)
    ax.set_title('average traffic speed on Fridays')
    plt.pcolor(np.swapaxes(fri, 0, 1), cmap=my_cmap, vmin=20, vmax=70)
    ax = plt.subplot(2, 2, 2)
    ax.set_title('average traffic speed on the target Friday')
    plt.pcolor(np.swapaxes(Speed[330, :, :], 0, 1),
               cmap=my_cmap,
               vmin=20,
               vmax=70)  #12-16-2016 Friday 6:30AM - 8:30AM
    ax = plt.subplot(2, 2, 3)
    ax.set_title('average traffic speed of sensor0 on Fridays')
    plt.plot(fri[:, 0])
    ax.set_ylim([0, 70])
    ax = plt.subplot(2, 2, 4)
    ax.set_title('traffic speed of sensor0 on the target Friday')
    plt.plot(Speed[330, :, 0])  #12-16-2016 Friday 6:30AM - 8:30AM
    ax.set_ylim([0, 70])

    return fig3
Example #2
0
def test_case_1(data, Speed):
    # test case 1: recurrent morning congestion
    Mon, _, _, mon, _ = get_certain_dayofweek(data, Speed, 0)

    fig1 = plt.figure(figsize=(15, 8))
    fig1.suptitle(
        'test case 1 (recurrent morning congestion): 12-05-2016, Monday')
    ax = plt.subplot(2, 2, 1)
    plt.pcolor(np.swapaxes(mon, 0, 1), cmap=my_cmap, vmin=20, vmax=70)
    ax.set_title('average traffic speed on Mondays')
    ax = plt.subplot(2, 2, 2)
    plt.pcolor(np.swapaxes(Speed[319, :, :], 0, 1),
               cmap=my_cmap,
               vmin=20,
               vmax=70)  #12-05-2016 Monday 6:30AM - 8:30AM
    ax.set_title('traffic speed on the target Monday')
    ax = plt.subplot(2, 2, 3)
    ax.set_title('average traffic speed of sensor0 on Mondays')
    plt.plot(mon[:, 0])
    ax.set_ylim([0, 70])
    ax = plt.subplot(2, 2, 4)
    ax.set_title('traffic speed of sensor0 on the target Monday')
    plt.plot(Speed[319, :, 0])  #12-05-2016 Monday 6:30AM - 8:30AM sensor 1
    ax.set_ylim([0, 70])

    return fig1
Example #3
0
def Experiment3(epochs, data_train, data_test, Speed):

    test_speed = Speed[data_test.index, :, :]

    train_speed0, _, _, _, _ = get_certain_dayofweek(Speed, 0)
    train_speed1, _, _, _, _ = get_certain_dayofweek(Speed, 1)
    train_speed2, _, _, _, _ = get_certain_dayofweek(Speed, 2)
    train_speed3, _, _, _, _ = get_certain_dayofweek(Speed, 3)
    train_speed4, _, _, _, _ = get_certain_dayofweek(Speed, 4)
    train_speed5, _, _, _, _ = get_certain_dayofweek(Speed, 5)
    train_speed6, _, _, _, _ = get_certain_dayofweek(Speed, 6)

    print('train_speed0.shape = ', train_speed0.shape)
    print('test_speed.shape = ', test_speed.shape)

    #    construct model inputs
    look_back = 15
    mode = 'uni'
    train_speed_x, train_speed_y = create_dataset(train_speed, train_speed,
                                                  look_back, mode)
    test_speed_x, test_speed_y = create_dataset(test_speed, test_speed,
                                                look_back, mode)
    print('look_back = ', look_back)
    print('mode = ', mode)
    print('train_speed_x.shape = ', train_speed_x.shape)
    print('train_speed_y.shape = ', train_speed_y.shape)
    print('test_speed_x.shape = ', test_speed_x.shape)
    print('test_speed_y.shape = ', test_speed_y.shape)

    #    visualize test data
    #plt.plot(test_speed_y[0,390:510,:])  #12-19-2016 Monday 6:30AM - 8:30AM
    plt.plot(test_speed_y[0, :, :])  #12-16-2016 Friday

    #    one day is a batch
    batch_size = train_speed_x.shape[1]

    #    define model structure
    model = Sequential()
    model.add(
        LSTM(32,
             input_shape=(look_back, train_speed_x.shape[3]),
             stateful=False,
             return_sequences=True))
    # model.add(Dropout(0.3))
    model.add(
        LSTM(32,
             input_shape=(look_back, train_speed_x.shape[3]),
             stateful=False))
    # model.add(Dropout(0.3))
    model.add(Dense(1))
    model.compile(loss='mean_squared_error', optimizer='adam')

    #    fit model
    train_x = np.reshape(train_speed_x,
                         (train_speed_x.shape[0] * train_speed_x.shape[1],
                          train_speed_x.shape[2], train_speed_x.shape[3]))
    train_y = np.reshape(train_speed_y,
                         (train_speed_y.shape[0] * train_speed_y.shape[1],
                          train_speed_y.shape[2]))
    history = model.fit(train_x,
                        train_y,
                        epochs=epochs,
                        batch_size=batch_size,
                        verbose=1,
                        shuffle=True)

    return model, history
Example #4
0
import pandas as pd
import csv
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt
from matplotlib import gridspec
from pylab import *

from lib.Function_DOW_Normalizer import get_certain_dayofweek, MinMax_Normalization, transfer_scale
from lib.Function_CreateInput import create_dataset

(Traffic, Speed, data) = pickle.load(
    open(
        "/Users/Shuo/study/Project-predictive_study/Speed_Pred_Stage2/speed_short_term.p",
        "rb"))
train_speed, _, _, _, _ = get_certain_dayofweek(data, Speed[:334], dayofweek=0)
test_speed = train_speed[-1:]

look_back = 15
mode = 'uni'
test_speed_x, test_speed_y = create_dataset(test_speed, test_speed, look_back,
                                            mode)


def model_score(history_object, image1):
    trainScore = []
    for i in range(len(train_speed_x)):
        trainScore.append(
            model.evaluate(train_speed_x[i, :, :, :],
                           train_speed_y[i, :, :],
                           batch_size=batch_size,