Пример #1
0
def main(method, transform, temporal, layout, hybrid, threshold, output):
    variables = getattr(layouts, layout)

    K = list(variables.keys())

    df = utils.prep_dataframe(keep=K)

    if temporal:
        df_shifted = utils.create_shifted_features(df)
        df = df.join(df_shifted, how="outer")
        df = df.dropna()

    if transform:
        print("* Tranform data")
        X = tr.to_normal(df.values)
        df = pd.DataFrame(X, index=df.index.values, columns=df.columns.values)

    X = df.values

    if hybrid:
        model = HRF(k=5, k_star=10, variables_names=df.columns.values)
    else:
        model = GMRF(method=method[0])

    model.fit(X)

    if not hybrid:
        print("* Selected alpha = {}".format(model.alpha_))
    else:
        print("* Selected k = {}, k star = {}".format(model.k, model.k_star))

    if threshold:
        Q = model.precision_.copy()
        ts = np.arange(0., 1., 0.001)

        bics = np.empty(len(ts))
        connectivity = np.empty(len(ts))

        n = Q.shape[0]
        gmrf_test = GMRF()
        gmrf_test.mean_ = np.mean(X, axis=0)
        for i, t in enumerate(ts):
            Q[Q < t] = 0
            gmrf_test.precision_ = Q
            bics[i], _ = gmrf_test.bic(X)
            connectivity[i] = 1 - np.size(np.where(Q == 0)[0]) / (n * n)

        fig, (ax, ax1) = plt.subplots(2, 1)
        ax.plot(connectivity)
        ax1.plot(bics)

    if output:
        results_name = os.path.join(os.path.dirname(__file__), "../results/")
        if hybrid:
            BNs = np.empty(len(model.variables_names), dtype=object)
            for i in range(len(BNs)):
                BNs[i] = (model.bns[i].variables_names, model.bns[i].nodes, model.bns[i].edges)
            np.save(results_name + output + "_bns", BNs)
        else:
            np.save(results_name + output + "_prec", model.precision_)
            np.save(results_name + output + "_mean", model.mean_)
            np.save(results_name + output + "_bic_scores", model.bic_scores)

    if not hybrid:
        plt.figure()
        plt.plot(model.bic_scores)

        fig, ax = plt.subplots(1, 1)
        pl.bin_precision_matrix(model.precision_, df.columns.values, ax)

        plt.show()
Пример #2
0
import os
import sys
import utils
import layouts
import numpy as np
import pandas as pd

K = list(layouts.datacenter_layout.keys())

df = utils.prep_dataframe(keep=K)
df_shifted = utils.create_shifted_features(df)
df = df.join(df_shifted, how="outer")
df = df.dropna()

kf_scores = np.load("../results/gmrfNone_kf_scores.npy")
r2 = np.load("../results/gmrfNone_r2.npy")
kf_scores_hybrid = np.load("../results/hybridNone_kf_scores.npy")
r2_hybrid = np.load("../results/hybridNone_r2.npy")

def tables(data, r2, name):
    print(name.upper())
    columns = ["\textbf{Variables}", "\textbf{MAD t = 1}", "\textbf{MAD t = 4}", "\textbf{MAD t = 8}", "\textbf{$R^2$ = 0}"]
    table = pd.DataFrame(columns=columns)

    j = 0
    mean = np.mean(data, axis=0)
    std = np.std(data, axis=0)

    for i, n in enumerate(df.columns.values):
        if 'l1_' not in n:
            v = ((n.replace("_", " ")).upper())[:5]
Пример #3
0
def main(alpha, transform, temporal, layout, steps, output, hybrid):
    variables = getattr(layouts, layout)

    K = list(variables.keys())

    df = utils.prep_dataframe(keep=K)

    if temporal:
        df_shifted = utils.create_shifted_features(df)
        df = df.join(df_shifted, how="outer")
        df = df.dropna()

        names = list(filter(lambda x: 'l1_' not in x, df.columns.values))

    if transform:
        print("* Tranform data")
        X = tr.to_normal(df.values)
        df = pd.DataFrame(X, index=df.index.values, columns=df.columns.values)

    if hybrid:
        model = HRF(variables_names=df.columns.values, k=5, k_star=10)
    else:
        model = GMRF(variables_names=df.columns.values, alpha=alpha)

    kf = KFold(df.shape[0], n_folds=5, shuffle=False)

    pool = mp.Pool(processes=5)

    print("* Scoring")
    kf_scores = [pool.apply_async(scoring,
                 args=(df, model, names, train, test, steps, id))
                 for id, (train, test) in enumerate(kf)]

    results = [p.get() for p in kf_scores]
    results = [np.array(t) for t in zip(*results)]

    r2 = results[0]
    kf_scores = results[1]
    #variances = results[2]

    r2 = np.sum(r2, axis=0) / len(kf)
    scores = np.sum(kf_scores, axis=0) / len(kf)
    #var = np.sum(variances, axis=0) / len(kf)

    if output:
        results_name = os.path.join(os.path.dirname(__file__),
                                    "../results/")
        np.save(results_name + output + str(steps) + "_kf_scores", kf_scores)
        np.save(results_name + output + str(steps) + "_scores", scores)
        np.save(results_name + output + str(steps) + "_r2", r2)
       # np.save(results_name + output + str(steps) + "_var", var)

    labels = df.columns.values
    labels = list(filter(lambda x: 'ahu' not in x, labels))

    if steps == 1:
        plt.figure()
        plt.boxplot(scores)
        plt.xticks(np.arange(1, 40), labels, rotation=90)
    else:
        plt.figure()
        plt.plot(scores)
        plt.figure()
        plt.plot(r2)

    plt.show()
Пример #4
0
def main():
    K = list(layouts.datacenter_layout.keys())

    df = utils.prep_dataframe(keep=K)
    df_shifted = utils.create_shifted_features(df)
    df = df.join(df_shifted, how="outer")
    df = df.dropna()
    names = list(filter(lambda x: 'l1_' not in x, df.columns.values))

    # a = np.random.normal(5, 2, 3000)
    # d = np.random.normal(-2, 3, 3000)
    # b = a * 3 + 9 + np.random.normal(0, 0.2, 3000)
    # c = 4 * d + (-5) * b + 11 + np.random.normal(0, 0.01, 3000)
    # X = np.array([a, b, c, d]).T
    # names = ['c', 'a']

    # df = pd.DataFrame.from_records(columns=np.array(['a', 'b', 'c', 'd']), data=X)

    X = df.values

    gmrf = GMRF(variables_names=df.columns.values, alpha=0.1)
    hrf = HRF(k=5, k_star=10, variables_names=df.columns.values)
    gbn = GBN(variables_names=df.columns.values)

    X_train, X_test = train_test_split(X, test_size=0.25)

    cv_scores = []
    train_scores = []

    pool = mp.Pool(processes=8)
    results = [pool.apply_async(scoring,
               args=(df, hrf, names, X_train[:i, :], X_test, i))
               for i in range(100, X_train.shape[0], 100)]

    output = [p.get() for p in results]
    output.sort()
    output = [np.array(t) for t in zip(*output)]

    cv_scores = output[2]
    train_scores = output[1]

    # hl, = plt.plot([], [])
    # for i in range(100, X_train.shape[0], 100):
    #     print("* Round {}".format(int(i / 100)))
    #     train_score, cv_score = scoring(df, gmrf, names, X_train[:i, :], X_test, i)

    #     cv_scores.append(cv_score)
    #     train_scores.append(train_score)

    #     hl.set_xdata(numpy.append(hl.get_xdata(), i))
    #     hl.set_ydata(numpy.append(hl.get_ydata(), train_score))
    #     plt.draw()

    #     # plt.plot(cv_scores, 'bo-')
    #     # plt.plot(train_scores, 'ro-')
    #     # plt.draw()

    # plt.ioff()
    plt.plot(range(100, X_train.shape[0], 100), cv_scores, 'bo-')
    plt.plot(range(100, X_train.shape[0], 100), train_scores, 'ro-')
    plt.show()
Пример #5
0
def main(layout, model, transform, output):
    variables = getattr(layouts, layout)

    K = list(variables.keys())

    df = utils.prep_dataframe(keep=K)

    df_shifted = utils.create_shifted_features(df)
    df = df.join(df_shifted, how="outer")
    df = df.dropna()

    # print(list(enumerate(df.columns.values)))
    # assert False

    if transform:
        print("* Tranform data")
        X = tr.to_normal(df.values)
        df = pd.DataFrame(X, index=df.index.values, columns=df.columns.values)

    X = df.values

    if model[0] == 'gmrf':
        model = GMRF(variables_names=df.columns.values, alpha=0.1)
    elif model[0] == 'hybrid':
        model = HRF(k=5, k_star=10, variables_names=df.columns.values)

    lim = int(X.shape[0] * 0.75)
    X_train = X[:lim]
    X_test = X[lim:]

    model.fit(X_train)
    print("* Model Fitted")

    # controls_vars = ['ahu_1_outlet', 'ahu_2_outlet', 'ahu_3_outlet', 'ahu_4_outlet']
    controls_vars = ['ahu_3_outlet']
    controller = Controller(6, 15, 30)
    mdp = MDP(model, 1000, reward, 0.8, feature_creator, controller,
              controls_vars, n_jobs=3)
    mdp.learn()

    # plt.figure()
    # plt.hist(test[:, 38:42].ravel(), bins=5, range=(5, 30))

    # plt.figure()
    # plt.plot(test[:, 38:42])

    actions, states = run_simulation(X_test, controls_vars, mdp, model, controller)

    print(actions)
    actions_values_one = [None] * len(controls_vars)
    actions_values_two = [None] * len(controls_vars)
    for i in range(len(controls_vars)):
        actions_values_one[i] = [(j, a[i][1]) for j, a in enumerate(actions)
                                if a[i][0] == 0]
        actions_values_two[i] = [(j, a[i][1]) for j, a in enumerate(actions)
                                if a[i][0] == 1]

        actions_values_one[i] = list(zip(*actions_values_one[i]))
        actions_values_two[i] = list(zip(*actions_values_two[i]))

    for i in range(len(controls_vars)):
        plt.figure()
        if len(actions_values_one[i]) != 0:
            plt.plot(list(actions_values_one[i][0]), list(actions_values_one[i][1]), 'b')
        if len(actions_values_two[i]) != 0:
            plt.plot(list(actions_values_two[i][0]), list(actions_values_two[i][1]), 'g')
        plt.title(controls_vars[i])

    max_states = np.amax(states, axis=1)
    mean_states = np.mean(states, axis=1)
    min_states = np.amin(states, axis=1)

    plt.figure()
    plt.plot(max_states, 'r')
    plt.plot(mean_states, 'g')
    plt.plot(min_states, 'b')

    # plt.figure()
    # plt.plot(actions[:, 0], label="1")
    # plt.plot(actions[:, 1], label="2")
    # plt.plot(actions[:, 2], label="3")
    # plt.plot(actions[:, 3], label="4")
    # plt.legend(loc=0)

    # print(np.mean(actions, axis=0))

    plt.show()