Skip to content

Commit 549e8e3

Browse files
committed
fix chapter 6
1 parent d2d5d5a commit 549e8e3

File tree

1 file changed

+49
-57
lines changed

1 file changed

+49
-57
lines changed

chapter6/Change-Points_Anomalies.ipynb

Lines changed: 49 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"cell_type": "markdown",
1212
"metadata": {},
1313
"source": [
14-
"# Change point detection"
14+
"# Anomaly detection"
1515
]
1616
},
1717
{
@@ -20,7 +20,7 @@
2020
"metadata": {},
2121
"outputs": [],
2222
"source": [
23-
"!pip install ruptures"
23+
"!pip install alibi_detect"
2424
]
2525
},
2626
{
@@ -29,11 +29,9 @@
2929
"metadata": {},
3030
"outputs": [],
3131
"source": [
32-
"import matplotlib.pyplot as plt\n",
32+
"from alibi_detect.datasets import fetch_kdd\n",
3333
"\n",
34-
"plt.style.use(\"seaborn-whitegrid\")\n",
35-
"plt.rcParams[\"font.family\"] = \"Times New Roman\"\n",
36-
"plt.rcParams[\"font.size\"] = \"17\""
34+
"intrusions = fetch_kdd()"
3735
]
3836
},
3937
{
@@ -42,12 +40,7 @@
4240
"metadata": {},
4341
"outputs": [],
4442
"source": [
45-
"import numpy as np\n",
46-
"import ruptures as rpt\n",
47-
"\n",
48-
"signal, bkps = rpt.pw_constant(\n",
49-
" n_samples=500, n_features=3, n_bkps=2, noise_std=5.0, delta=(1, 20)\n",
50-
")"
43+
"intrusions[\"target\"].sum() / len(intrusions[\"target\"])"
5144
]
5245
},
5346
{
@@ -56,7 +49,7 @@
5649
"metadata": {},
5750
"outputs": [],
5851
"source": [
59-
"rpt.display(signal, bkps)"
52+
"intrusions[\"feature_names\"]"
6053
]
6154
},
6255
{
@@ -65,7 +58,7 @@
6558
"metadata": {},
6659
"outputs": [],
6760
"source": [
68-
"signal.shape"
61+
"scores = od.score(intrusions[\"data\"][:, 0])"
6962
]
7063
},
7164
{
@@ -74,7 +67,9 @@
7467
"metadata": {},
7568
"outputs": [],
7669
"source": [
77-
"bkps"
70+
"import pandas as pd\n",
71+
"\n",
72+
"pd.Series(intrusions[\"data\"][:, 0]).plot();"
7873
]
7974
},
8075
{
@@ -83,19 +78,12 @@
8378
"metadata": {},
8479
"outputs": [],
8580
"source": [
86-
"# \"l1\", \"rbf\", \"linear\", \"normal\", \"ar\"\n",
87-
"algo = rpt.Binseg(model=\"l1\").fit(signal)\n",
88-
"my_bkps = algo.predict(n_bkps=3)\n",
81+
"from alibi_detect.od import SpectralResidual\n",
8982
"\n",
90-
"# show results\n",
91-
"rpt.show.display(signal, bkps, my_bkps, figsize=(10, 6))"
92-
]
93-
},
94-
{
95-
"cell_type": "markdown",
96-
"metadata": {},
97-
"source": [
98-
"# Anomaly detection"
83+
"od = SpectralResidual(\n",
84+
" threshold=1.0, window_amp=20, window_local=20, n_est_points=10, n_grad_points=5\n",
85+
")\n",
86+
"intrusion_outliers = od.predict(intrusions[\"data\"][:,0])"
9987
]
10088
},
10189
{
@@ -104,18 +92,23 @@
10492
"metadata": {},
10593
"outputs": [],
10694
"source": [
107-
"!pip install alibi_detect"
95+
"import matplotlib\n",
96+
"\n",
97+
"ax = pd.Series(intrusions[\"data\"][:, 0], name=\"data\").plot(\n",
98+
" legend=False, figsize=(12, 6)\n",
99+
")\n",
100+
"ax2 = ax.twinx()\n",
101+
"ax = pd.Series(scores, name=\"scores\").plot(\n",
102+
" ax=ax2, legend=False, color=\"r\", marker=matplotlib.markers.CARETDOWNBASE\n",
103+
")\n",
104+
"ax.figure.legend(bbox_to_anchor=(1, 1), loc=\"upper left\");"
108105
]
109106
},
110107
{
111-
"cell_type": "code",
112-
"execution_count": null,
108+
"cell_type": "markdown",
113109
"metadata": {},
114-
"outputs": [],
115110
"source": [
116-
"from alibi_detect.datasets import fetch_kdd\n",
117-
"\n",
118-
"intrusions = fetch_kdd()"
111+
"# Change point detection"
119112
]
120113
},
121114
{
@@ -124,7 +117,7 @@
124117
"metadata": {},
125118
"outputs": [],
126119
"source": [
127-
"intrusions[\"target\"].sum() / len(intrusions[\"target\"])"
120+
"!pip install ruptures"
128121
]
129122
},
130123
{
@@ -133,7 +126,13 @@
133126
"metadata": {},
134127
"outputs": [],
135128
"source": [
136-
"intrusions[\"feature_names\"]"
129+
"import matplotlib.pyplot as plt\n",
130+
"import numpy as np\n",
131+
"import ruptures as rpt\n",
132+
"\n",
133+
"plt.style.use(\"seaborn-whitegrid\")\n",
134+
"plt.rcParams[\"font.family\"] = \"Times New Roman\"\n",
135+
"plt.rcParams[\"font.size\"] = \"17\""
137136
]
138137
},
139138
{
@@ -142,12 +141,9 @@
142141
"metadata": {},
143142
"outputs": [],
144143
"source": [
145-
"from alibi_detect.od import SpectralResidual\n",
146-
"\n",
147-
"od = SpectralResidual(\n",
148-
" threshold=1.0, window_amp=20, window_local=20, n_est_points=10, n_grad_points=5\n",
149-
")\n",
150-
"intrusion_outliers = od.predict(intrusions[\"data\"])"
144+
"signal, bkps = rpt.pw_constant(\n",
145+
" n_samples=500, n_features=3, n_bkps=2, noise_std=5.0, delta=(1, 20)\n",
146+
")"
151147
]
152148
},
153149
{
@@ -156,7 +152,7 @@
156152
"metadata": {},
157153
"outputs": [],
158154
"source": [
159-
"scores = od.score(intrusions[\"data\"][:, 0])"
155+
"rpt.display(signal, bkps)"
160156
]
161157
},
162158
{
@@ -165,9 +161,7 @@
165161
"metadata": {},
166162
"outputs": [],
167163
"source": [
168-
"import pandas as pd\n",
169-
"\n",
170-
"pd.Series(intrusions[\"data\"][:, 0]).plot();"
164+
"signal.shape"
171165
]
172166
},
173167
{
@@ -176,24 +170,22 @@
176170
"metadata": {},
177171
"outputs": [],
178172
"source": [
179-
"import matplotlib\n",
180-
"\n",
181-
"ax = pd.Series(intrusions[\"data\"][:, 0], name=\"data\").plot(\n",
182-
" legend=False, figsize=(12, 6)\n",
183-
")\n",
184-
"ax2 = ax.twinx()\n",
185-
"ax = pd.Series(scores, name=\"scores\").plot(\n",
186-
" ax=ax2, legend=False, color=\"r\", marker=matplotlib.markers.CARETDOWNBASE\n",
187-
")\n",
188-
"ax.figure.legend(bbox_to_anchor=(1, 1), loc=\"upper left\");"
173+
"bkps"
189174
]
190175
},
191176
{
192177
"cell_type": "code",
193178
"execution_count": null,
194179
"metadata": {},
195180
"outputs": [],
196-
"source": []
181+
"source": [
182+
"# \"l1\", \"rbf\", \"linear\", \"normal\", \"ar\"\n",
183+
"algo = rpt.Binseg(model=\"l1\").fit(signal)\n",
184+
"my_bkps = algo.predict(n_bkps=3)\n",
185+
"\n",
186+
"# show results\n",
187+
"rpt.show.display(signal, bkps, my_bkps, figsize=(10, 6))"
188+
]
197189
}
198190
],
199191
"metadata": {

0 commit comments

Comments
 (0)