-
Notifications
You must be signed in to change notification settings - Fork 4
/
sample.py
52 lines (44 loc) · 1.33 KB
/
sample.py
1
2
3
4
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
from changefinder import change_finder
from numpy.random import rand, multivariate_normal
import numpy as np
import matplotlib.pyplot as plt
## generating sample data
data_a = multivariate_normal([10.0], np.eye(1) * 0.1, 400)
data_b = multivariate_normal(rand(1) * 100, np.eye(1), 100)
X = np.r_[data_a, data_b][:, 0]
## scoring
c_cf = change_finder(term=50, window=5, order=(1, 1, 0))
result = c_cf.main(X)
## plot
fig = plt.figure()
axL = fig.add_subplot(111)
line1, = axL.plot(X, "b-", alpha=.7)
plt.ylabel("Values")
ax = plt.gca()
ax.yaxis.grid(False)
ax.xaxis.grid(True)
for tick in ax.yaxis.get_major_ticks():
tick.tick2On = False
tick.label2On = False
for tick in ax.xaxis.get_major_ticks():
tick.tick2On = False
tick.label2On = False
axR = fig.add_subplot(111, sharex=axL, frameon=False)
line2, = axR.plot(result, "r-", alpha=.7)
axR.yaxis.tick_right()
axR.yaxis.set_label_position("right")
plt.ylabel("Score")
plt.ylim(ymin=-5.0)
plt.xlabel("Sample data")
ax = plt.gca()
ax.yaxis.grid(False)
ax.xaxis.grid(False)
for tick in ax.yaxis.get_major_ticks():
tick.tick2On = True
tick.label2On = True
for tick in ax.xaxis.get_major_ticks():
tick.tick2On = False
tick.label2On = False
plt.title("Sample: Change Anomaly Detection")
plt.legend([line1, line2], ["Data", "Score"], loc=2)
plt.savefig("sample.png", dpi=144)