In [10]:

    import warnings
    warnings.filterwarnings("ignore")
    from IPython.core.display import display, HTML
    display(HTML("<style>.container { width:100% !important; }</style>"))

    import sys
    sys.path.append('..\\..\\pyclf\\models')
    sys.path.append('..\\..\\pyclf\\model')
    sys.path.append('..\\..\\pyclf\\validation')
    sys.path.append('..\\..\\pyclf\\pyutils')
    sys.path.append('..\\..\\bokeh')

    import os.path
    import json
    import copy
    import time
    import pickle
    import numpy as np
    import pandas as pd

    from sklearn.neighbors import KNeighborsClassifier
    from sklearn.pipeline import Pipeline
    from sklearn.preprocessing import MinMaxScaler
    from sklearn.decomposition import PCA
    from sklearn import cross_validation
    from sklearn import metrics
    from sklearn.cluster import KMeans
    from sklearn.preprocessing import PolynomialFeatures
    from sklearn.grid_search import GridSearchCV
    from sklearn.tree import DecisionTreeClassifier
    from sklearn.cross_validation import StratifiedKFold, cross_val_score

    import matplotlib.pyplot as plt
    import matplotlib as mpl
    %matplotlib inline
    %config InlineBackend.figure_format='retina'
    import seaborn as sns

    import experimental
    import validation
    import weight
    import multiclass_metrics
    import optimization
    from sklearn.linear_model import LogisticRegression
    from local_model import LocalModel

    from experimental import KNNWeightedFeatures

    import bokeh_confmat_v2
    from bokeh.io import output_notebook, show
    output_notebook()

    #load configs
    dataset = "..//..//training_data/trainset_v3_profile_clusters.csv"

    with open('..//..//pyclf//configs/feature_headers.json', 'r') as f:
        feature_headers = json.load(f)

    with open('..//..//pyclf//configs//folds.json', 'r') as f:
        folds = json.load(f)

    with open('..//..//pyclf//configs//class_mapping.json', 'r') as f:
        mapper = json.load(f)

    mapper = mapper["loop_table"]

    #prepare data
    df = pd.read_csv(dataset, sep=';')
    df['class_name_pcluster'] = df.class_name + ' PCluster: ' + df.PCluster.map(str)
    df.loc[:,'class_name'] = df.loc[:,'class_name'].map( lambda x: mapper[x] if x in mapper.keys() else x)

    classes = df.class_name.unique()
    X = df[feature_headers['feature_names']].as_matrix()
    y = df.class_name.as_matrix()

    folds_ind = [ (f["train_ind"], f["test_ind"]) for f in folds["folds"]]

    #weight from coordinate descent
    cd_weights = np.array([3.1, 0.9,  1.0,  1.0,  1.0,  1.1,  0.0,  0.5,  0.7,  1.0,  0.9,  1.0,  0.9,  1.0,  1.0,  1.0,  1.0,  0.9,  0.9,  1.0,  1.1,  1.2,  1.0,  1.0,  1.0, 1.0, 1.0])

Loading BokehJS ...
In [11]:

    sns.reset_orig()
    plt.figure(figsize=(6,4))
    sns.boxplot(x="class_name", y='Length',  saturation=1,  data=df, color='gray')
    plt.title("Vehicle length across vehicle classes")
    plt.xlabel("vehicle class code")
    plt.ylabel("Length [m]")
    plt.grid()
    # plt.savefig("length_across_vehicle_classes.pdf")

In [20]:

    df = pd.read_csv("..//..//training_data//trainset_v3_resampled.csv", ';')

    # df.loc[:,'class_name'] = df.loc[:,'class_name'].map( lambda x: mapper[x] if x in mapper.keys() else x)
    X = df.iloc[:, 1:-2].as_matrix()
    y = df.class_name.as_matrix()

    CLASS_TO_PLOT = '8 -semi-trailer truck'

    MAX_SAMPLES_TO_PLOT = 1500
    X = X[y==CLASS_TO_PLOT]
    X = X[np.random.choice(np.arange(len(X)), MAX_SAMPLES_TO_PLOT)]

    est = KMeans(n_clusters=2)
    est.fit(X)
    clusters = est.predict(X)

    plt.figure(figsize=(10,4))

    for cl in range(est.n_clusters):
        plt.subplot(1, est.n_clusters, cl + 1)
        Xcl = X[clusters == cl]
        for i in range(len(Xcl)):
            plt.plot(Xcl[i], color = 'gray', lw='0.1')
        plt.xticks([])
        plt.ylim([0.0, 1.04])
        plt.grid()
    plt.suptitle('vehicle class: ' + CLASS_TO_PLOT)
    plt.savefig("vehicle_class_8_profiles.png")

In [60]:

    pL = experimental.ProfilesLoader(dataset,
                                     frq_folder="E:\\Camea\\Loops\\data")

    pL.load_data()

    0\9909 samples read
    1000\9909 samples read
    2000\9909 samples read
    3000\9909 samples read
    4000\9909 samples read
    5000\9909 samples read
    6000\9909 samples read
    7000\9909 samples read
    8000\9909 samples read
    9000\9909 samples read
    9909 samples read
    0 samples failed to read

In [69]:

    P_LENGTHS = [i for i in range(5, 100, 1)]

    knn = KNeighborsClassifier(n_neighbors=1, weights = 'uniform',
                               algorithm = 'brute',
                               metric = 'minkowski', p=2)

    clf = Pipeline([       ("Normalization", MinMaxScaler()),
                           ("KNN", knn)])

    scores = []
    for plength in P_LENGTHS:
        print plength,
        X, y = pL.getXy(plength)
        y = np.array(map( lambda x: mapper[x] if x in mapper.keys() else x, y))
        scores.append(cross_val_score(clf, X, y, scoring='f1_macro', cv=folds_ind).mean())

     5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

In [72]:

    pickle.dump((P_LENGTHS, scores), open("macro_f_score_vs_profile_length_1nn", "wb"))

In [139]:

    sns.reset_orig()
    plt.rcParams["font.family"] = "Roman"
    plt.figure(figsize=(8,4))
    plt.plot(P_LENGTHS, scores, '+',  markersize=6, markeredgewidth=2, color='black', ); plt.grid();
    plt.ylabel("$ \mathrm{F1_{macro}}$ score", fontsize=12)
    plt.xlabel("Number of loop signature data points")
    plt.savefig("macro_f_score_vs_profile_length_1nn.pdf")

In [132]:

    plt.plot?

In [ ]: