In [110]:
import pandas as pd
import numpy as np
import elm_autoencoder as elmae
from datetime import datetime

In [111]:
dat = pd.read_csv("data/LSJA24U64GS058596.csv")

In [112]:
dat['starttime'] = dat['starttime'].map(lambda x:datetime.strptime(x,"%Y-%m-%d %H:%M:%S"))

In [113]:
dat = dat.sort('starttime')

In [123]:
def train(training):
    iternum = 5
    score_mean = []
    score_cov = []
    for i in range(0,iternum):
        model = elmae.ELMAutoEncoder(n_hidden=1000)
        model.fit(training)
        ## 计算likelihood参数
        diff_train = abs(model._get_predictions()-training)
        score_mean.append(np.apply_along_axis(np.mean,0,diff_train))
        score_cov.append(np.cov(diff_train.T))

    score_mean_sum = score_mean[0]
    score_cov_sum = score_cov[0]
    for i in range(1,iternum):
        score_mean_sum = score_mean_sum+score_mean[i]
        score_cov_sum = score_cov_sum+score_cov[i]

    score_mean = score_mean_sum/iternum
    score_cov = score_cov_sum/iternum
    np.savetxt("model/coef_hidden_.txt",model.coef_hidden_)
    np.savetxt("model/intercept_hidden_.txt",model.intercept_hidden_)
    np.savetxt("model/coef_output_.txt",model.coef_output_)
    np.savetxt("model/score_mean.txt",score_mean)
    np.savetxt("model/score_cov.txt",score_cov)
    return model

In [124]:
def detect(elm,testing):
    coef_hidden_ = np.loadtxt("model/coef_hidden_.txt")
    intercept_hidden_ = np.loadtxt("model/intercept_hidden_.txt")
    coef_output_ = np.loadtxt("model/coef_output_.txt")
    #score_mean = np.loadtxt("model/score_mean.txt")
    score_cov = np.loadtxt("model/score_cov.txt")
    #anomaly_score = []

    def predict(testing,coef_hidden_,intercept_hidden,coef_output_):
        A = np.dot(testing,coef_hidden_)
        A += intercept_hidden_
        hidden_activations_ = np.tanh(A)
        preds = np.dot(hidden_activations_, coef_output_)
        return preds
    
    #from sklearn.metrics import mean_squared_error
    def anomaly_s(x,coef_hidden_,intercept_hidden_,coef_output_):
        pred = predict(x,coef_hidden_,intercept_hidden_,coef_output_)
        actual = x
        d = np.array(abs(pred-actual))
        a = np.dot(d.T,np.linalg.inv(score_cov))
        b = np.dot(a,d)
        #b = mean_squared_error(actual,pred)
        return b
    
        
    return np.apply_along_axis(anomaly_s,1,testing,coef_hidden_,intercept_hidden_,coef_output_)

In [125]:
axy = np.array(dat[['tboxaccelx','tboxaccely']])
#,'tboxaccelz','vehspeed','vehrpm', 'vehaccelpos', 'vehbrakepos'
axy = np.apply_along_axis(lambda x: (x-np.average(x))/np.sqrt(np.var(x)), 0, axy)
model = train(axy)
#n = int(len(axy)/1000)
#for i in range(0,n):
#    model.add_fit(axy[i*1000:(i+1)*1000])

In [118]:
anomaly_score = detect(model,axy)

In [119]:
import matplotlib.pylab as plt
%matplotlib inline
plt.plot(np.log(anomaly_score))


Out[119]:
[<matplotlib.lines.Line2D at 0x150cad3d0>]

In [120]:
plt.hist(np.log(anomaly_score))


Out[120]:
(array([  1.65300000e+03,   3.00500000e+03,   6.55810000e+04,
          9.71050000e+04,   8.61520000e+04,   1.32560000e+04,
          2.74000000e+03,   1.01200000e+03,   8.70000000e+01,
          3.00000000e+01]),
 array([ -9.84497289,  -7.85364786,  -5.86232282,  -3.87099778,
         -1.87967274,   0.1116523 ,   2.10297734,   4.09430238,
          6.08562741,   8.07695245,  10.06827749]),
 <a list of 10 Patch objects>)

In [121]:
dat['anomaly_score'] = np.log(anomaly_score)

In [122]:
dat.sort('anomaly_score',ascending=False)


Out[122]:
vin starttime tboxaccelx tboxaccely tboxaccelz vehspeed vehrpm vehaccelpos vehbrakepos vehsteeringangle anomaly_score
86375 LSJA24U64GS058596 2016-09-05 09:14:29 -0.583 1.007 -1.286 48.0 1006 0.000 81.536 -48.5 10.068277
9762 LSJA24U64GS058596 2016-09-24 10:43:06 -0.875 0.449 -0.430 30.2 1087 0.000 28.616 1.5 9.641436
126055 LSJA24U64GS058596 2016-09-25 14:53:47 -0.758 0.660 -0.860 35.4 1312 0.000 16.464 210.1 9.265391
154757 LSJA24U64GS058596 2016-09-25 14:51:09 -0.145 1.183 -1.149 15.5 2553 0.000 81.536 -6.0 9.264810
21729 LSJA24U64GS058596 2016-09-25 16:19:22 -0.868 0.218 -1.122 29.1 1844 83.496 0.000 277.1 9.077803
154115 LSJA24U64GS058596 2016-09-25 14:52:05 -0.840 0.332 -1.165 37.3 1986 0.000 55.272 94.0 8.821532
154437 LSJA24U64GS058596 2016-09-25 14:52:06 -0.840 0.332 -1.165 32.7 1206 17.640 0.000 263.3 8.821532
97161 LSJA24U64GS058596 2016-09-10 14:03:49 -0.840 0.328 -1.032 55.5 1279 0.000 0.000 82.6 8.811585
96044 LSJA24U64GS058596 2016-09-10 14:03:50 -0.840 0.328 -1.032 54.0 1386 0.000 0.000 67.8 8.811585
133795 LSJA24U64GS058596 2016-09-13 06:26:26 -0.844 0.167 -0.680 20.5 1798 6.272 0.000 2.6 8.694068
11163 LSJA24U64GS058596 2016-09-25 14:50:26 -0.836 0.273 -1.239 33.0 1959 99.960 0.000 123.1 8.634919
121956 LSJA24U64GS058596 2016-09-25 14:50:25 -0.836 0.273 -1.239 29.8 1661 99.960 0.000 206.1 8.634919
114398 LSJA24U64GS058596 2016-09-04 10:56:14 -0.032 1.148 -0.915 0.1 954 0.000 99.960 -1.0 8.607348
91844 LSJA24U64GS058596 2016-09-04 10:56:13 -0.032 1.148 -0.915 0.8 938 0.000 82.320 -0.9 8.607348
17360 LSJA24U64GS058596 2016-09-25 16:19:43 0.792 0.234 -1.036 35.0 3183 99.960 0.000 138.3 8.537800
12142 LSJA24U64GS058596 2016-09-25 15:46:06 -0.821 0.082 -1.012 42.1 2439 40.376 0.000 182.3 8.383017
184042 LSJA24U64GS058596 2016-09-25 15:46:07 -0.821 0.082 -1.012 44.6 3497 8.624 0.000 23.8 8.383017
15540 LSJA24U64GS058596 2016-09-25 16:19:40 0.628 0.648 -1.051 44.8 2827 0.000 52.920 -150.9 8.294260
243591 LSJA24U64GS058596 2016-09-11 15:37:48 -0.032 1.125 -1.086 10.4 1089 0.000 83.104 1.0 8.270267
66303 LSJA24U64GS058596 2016-09-11 09:49:03 0.156 1.101 -0.950 29.8 926 0.000 89.376 -0.4 8.238309
243273 LSJA24U64GS058596 2016-09-11 09:49:05 -0.040 1.117 -0.895 0.1 934 0.000 72.128 1.3 8.152174
62724 LSJA24U64GS058596 2016-09-11 09:49:04 -0.040 1.117 -0.895 0.8 1003 0.000 90.552 1.3 8.152174
28214 LSJA24U64GS058596 2016-09-28 14:55:39 0.453 0.871 -1.047 29.2 1023 0.000 79.184 -66.5 8.138618
243861 LSJA24U64GS058596 2016-09-11 11:15:17 0.070 1.113 -0.922 7.5 1126 0.000 83.496 -10.9 8.121943
123032 LSJA24U64GS058596 2016-09-30 10:59:03 0.062 1.113 -0.915 18.3 965 0.000 81.144 -1.2 8.108595
26767 LSJA24U64GS058596 2016-09-29 12:34:51 -0.051 1.113 -1.016 0.1 1135 0.000 99.960 -0.4 8.101635
27137 LSJA24U64GS058596 2016-09-29 12:34:52 -0.051 1.113 -1.016 0.0 1016 0.000 0.000 -0.2 8.101635
124705 LSJA24U64GS058596 2016-09-25 14:51:00 0.667 0.539 -1.098 36.0 3315 10.192 0.000 96.3 8.089964
17986 LSJA24U64GS058596 2016-09-25 14:50:59 0.667 0.539 -1.098 34.9 2344 99.960 0.000 -243.7 8.089964
114519 LSJA24U64GS058596 2016-09-04 11:28:14 0.566 0.722 -1.051 27.4 1316 0.000 77.224 -2.2 8.082402
... ... ... ... ... ... ... ... ... ... ... ...
263966 LSJA24U64GS058596 2016-09-10 10:44:25 0.000 -0.004 -1.012 0.0 947 0.000 49.784 -7.2 -9.015006
124558 LSJA24U64GS058596 2016-09-25 09:51:13 0.000 -0.004 -1.012 0.0 943 0.000 29.400 -13.2 -9.015006
124878 LSJA24U64GS058596 2016-09-25 09:51:14 0.000 -0.004 -1.012 0.0 953 0.000 29.792 -13.2 -9.015006
26472 LSJA24U64GS058596 2016-09-29 12:17:24 0.000 -0.004 -1.008 0.0 940 0.000 49.784 -2.4 -9.015006
131275 LSJA24U64GS058596 2016-09-29 12:17:23 0.000 -0.004 -1.008 0.0 951 0.000 49.784 -2.4 -9.015006
27178 LSJA24U64GS058596 2016-09-29 12:27:22 0.000 -0.004 -1.012 0.0 941 0.000 49.784 -0.9 -9.015006
99653 LSJA24U64GS058596 2016-09-10 14:06:54 0.000 -0.004 -1.012 0.0 938 0.000 30.576 7.3 -9.015006
268112 LSJA24U64GS058596 2016-09-10 14:06:55 0.000 -0.004 -1.012 0.0 942 0.000 30.968 7.5 -9.015006
178333 LSJA24U64GS058596 2016-09-13 06:24:29 0.000 -0.004 -1.012 0.0 770 0.000 0.000 -0.9 -9.015006
224916 LSJA24U64GS058596 2016-09-13 06:24:28 0.000 -0.004 -1.012 0.0 747 0.000 0.000 -0.9 -9.015006
43301 LSJA24U64GS058596 2016-09-13 06:24:27 0.000 -0.004 -1.008 0.0 747 0.000 0.000 -0.9 -9.015006
210124 LSJA24U64GS058596 2016-09-13 06:24:26 0.000 -0.004 -1.008 0.0 769 0.000 0.000 -0.9 -9.015006
75563 LSJA24U64GS058596 2016-09-13 13:25:00 0.000 -0.004 -1.028 56.5 1467 5.880 0.000 -1.5 -9.015006
112533 LSJA24U64GS058596 2016-09-04 09:46:27 0.000 -0.004 -1.004 0.0 960 0.000 25.088 3.1 -9.015006
237659 LSJA24U64GS058596 2016-09-04 09:46:28 0.000 -0.004 -1.004 0.0 947 0.000 25.088 3.1 -9.015006
177303 LSJA24U64GS058596 2016-09-13 10:45:25 0.000 -0.004 -1.012 0.0 947 0.000 0.000 1.0 -9.015006
14463 LSJA24U64GS058596 2016-09-25 15:47:27 0.000 -0.004 -1.012 0.0 945 0.000 0.000 5.3 -9.015006
210263 LSJA24U64GS058596 2016-09-13 11:13:43 0.000 -0.004 -1.008 0.0 748 0.000 49.784 -7.2 -9.015006
75497 LSJA24U64GS058596 2016-09-13 11:13:42 0.000 -0.004 -1.008 0.0 750 0.000 49.784 -7.4 -9.015006
97509 LSJA24U64GS058596 2016-09-10 14:07:04 0.000 -0.004 -0.997 0.0 931 0.000 14.112 7.3 -9.015006
96477 LSJA24U64GS058596 2016-09-10 14:07:05 0.000 -0.004 -0.997 0.0 947 0.000 14.896 7.3 -9.015006
145314 LSJA24U64GS058596 2016-09-10 10:44:32 0.000 -0.004 -1.016 0.0 947 0.000 49.784 -7.2 -9.015006
98729 LSJA24U64GS058596 2016-09-22 16:19:40 0.000 -0.004 -1.000 0.0 936 0.000 30.576 -102.2 -9.015006
25123 LSJA24U64GS058596 2016-09-22 16:19:39 0.000 -0.004 -1.000 0.0 951 0.000 30.968 -102.2 -9.015006
47896 LSJA24U64GS058596 2016-09-13 06:24:21 0.000 -0.004 -1.008 0.0 747 0.000 0.000 -0.9 -9.015006
42071 LSJA24U64GS058596 2016-09-13 06:24:20 0.000 -0.004 -1.008 0.0 750 0.000 0.000 -0.9 -9.015006
95551 LSJA24U64GS058596 2016-09-10 10:44:31 0.000 -0.004 -1.016 0.0 956 0.000 49.784 -7.2 -9.015006
107597 LSJA24U64GS058596 2016-09-02 15:39:40 0.000 -0.004 -1.063 41.4 1291 5.488 0.000 -0.4 -9.015006
111259 LSJA24U64GS058596 2016-09-04 10:55:16 0.000 -0.004 -1.004 0.0 952 0.000 49.784 7.0 -9.015006
145063 LSJA24U64GS058596 2016-09-10 12:34:39 -0.227 0.039 -0.973 23.3 1435 0.000 0.000 117.5 -9.844973

270621 rows × 11 columns


In [109]:
len(axy)


Out[109]:
90889

In [ ]: