|
11 | 11 | "cell_type": "markdown", |
12 | 12 | "metadata": {}, |
13 | 13 | "source": [ |
14 | | - "# Change point detection" |
| 14 | + "# Anomaly detection" |
15 | 15 | ] |
16 | 16 | }, |
17 | 17 | { |
|
20 | 20 | "metadata": {}, |
21 | 21 | "outputs": [], |
22 | 22 | "source": [ |
23 | | - "!pip install ruptures" |
| 23 | + "!pip install alibi_detect" |
24 | 24 | ] |
25 | 25 | }, |
26 | 26 | { |
|
29 | 29 | "metadata": {}, |
30 | 30 | "outputs": [], |
31 | 31 | "source": [ |
32 | | - "import matplotlib.pyplot as plt\n", |
| 32 | + "from alibi_detect.datasets import fetch_kdd\n", |
33 | 33 | "\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()" |
37 | 35 | ] |
38 | 36 | }, |
39 | 37 | { |
|
42 | 40 | "metadata": {}, |
43 | 41 | "outputs": [], |
44 | 42 | "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\"])" |
51 | 44 | ] |
52 | 45 | }, |
53 | 46 | { |
|
56 | 49 | "metadata": {}, |
57 | 50 | "outputs": [], |
58 | 51 | "source": [ |
59 | | - "rpt.display(signal, bkps)" |
| 52 | + "intrusions[\"feature_names\"]" |
60 | 53 | ] |
61 | 54 | }, |
62 | 55 | { |
|
65 | 58 | "metadata": {}, |
66 | 59 | "outputs": [], |
67 | 60 | "source": [ |
68 | | - "signal.shape" |
| 61 | + "scores = od.score(intrusions[\"data\"][:, 0])" |
69 | 62 | ] |
70 | 63 | }, |
71 | 64 | { |
|
74 | 67 | "metadata": {}, |
75 | 68 | "outputs": [], |
76 | 69 | "source": [ |
77 | | - "bkps" |
| 70 | + "import pandas as pd\n", |
| 71 | + "\n", |
| 72 | + "pd.Series(intrusions[\"data\"][:, 0]).plot();" |
78 | 73 | ] |
79 | 74 | }, |
80 | 75 | { |
|
83 | 78 | "metadata": {}, |
84 | 79 | "outputs": [], |
85 | 80 | "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", |
89 | 82 | "\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])" |
99 | 87 | ] |
100 | 88 | }, |
101 | 89 | { |
|
104 | 92 | "metadata": {}, |
105 | 93 | "outputs": [], |
106 | 94 | "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\");" |
108 | 105 | ] |
109 | 106 | }, |
110 | 107 | { |
111 | | - "cell_type": "code", |
112 | | - "execution_count": null, |
| 108 | + "cell_type": "markdown", |
113 | 109 | "metadata": {}, |
114 | | - "outputs": [], |
115 | 110 | "source": [ |
116 | | - "from alibi_detect.datasets import fetch_kdd\n", |
117 | | - "\n", |
118 | | - "intrusions = fetch_kdd()" |
| 111 | + "# Change point detection" |
119 | 112 | ] |
120 | 113 | }, |
121 | 114 | { |
|
124 | 117 | "metadata": {}, |
125 | 118 | "outputs": [], |
126 | 119 | "source": [ |
127 | | - "intrusions[\"target\"].sum() / len(intrusions[\"target\"])" |
| 120 | + "!pip install ruptures" |
128 | 121 | ] |
129 | 122 | }, |
130 | 123 | { |
|
133 | 126 | "metadata": {}, |
134 | 127 | "outputs": [], |
135 | 128 | "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\"" |
137 | 136 | ] |
138 | 137 | }, |
139 | 138 | { |
|
142 | 141 | "metadata": {}, |
143 | 142 | "outputs": [], |
144 | 143 | "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 | + ")" |
151 | 147 | ] |
152 | 148 | }, |
153 | 149 | { |
|
156 | 152 | "metadata": {}, |
157 | 153 | "outputs": [], |
158 | 154 | "source": [ |
159 | | - "scores = od.score(intrusions[\"data\"][:, 0])" |
| 155 | + "rpt.display(signal, bkps)" |
160 | 156 | ] |
161 | 157 | }, |
162 | 158 | { |
|
165 | 161 | "metadata": {}, |
166 | 162 | "outputs": [], |
167 | 163 | "source": [ |
168 | | - "import pandas as pd\n", |
169 | | - "\n", |
170 | | - "pd.Series(intrusions[\"data\"][:, 0]).plot();" |
| 164 | + "signal.shape" |
171 | 165 | ] |
172 | 166 | }, |
173 | 167 | { |
|
176 | 170 | "metadata": {}, |
177 | 171 | "outputs": [], |
178 | 172 | "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" |
189 | 174 | ] |
190 | 175 | }, |
191 | 176 | { |
192 | 177 | "cell_type": "code", |
193 | 178 | "execution_count": null, |
194 | 179 | "metadata": {}, |
195 | 180 | "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 | + ] |
197 | 189 | } |
198 | 190 | ], |
199 | 191 | "metadata": { |
|
0 commit comments