File size: 48,783 Bytes
6ba905b | 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 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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 | # Explainable IDS Full Pipeline β Code Walkthrough
This document explains the notebook `explainable_ids_full_pipeline.ipynb` in very detailed practical terms. The goal is to understand **what each line or block does**, **why it exists**, and **how it connects to the project deliverables**:
1. Train an IDS model.
2. Explain IDS predictions.
3. Evaluate explanation stability and faithfulness.
4. Analyze security/adversarial risks.
The notebook is organized into seven main parts:
- setup and imports,
- dataset loading and preprocessing,
- model definitions,
- model training and evaluation,
- SHAP explanations,
- LIME explanations,
- stability, faithfulness, and security analysis.
---
## Big Picture Before Reading the Code
The project is an **Explainable Intrusion Detection System (X-IDS)**.
The dataset is **NSL-KDD**, where each row is a network connection. Each connection has 41 features such as protocol, service, duration, bytes, login status, error rates, and host-level statistics. The target label is binary:
- `normal`
- `anomaly`
The notebook trains three neural models:
- **MLP**: a standard feed-forward network for tabular data.
- **LSTM**: treats the 41 features like a sequence.
- **1D-CNN**: treats the 41 features like a one-dimensional signal.
Then it explains predictions using:
- **SHAP**: feature contribution values based on Shapley values.
- **LIME**: local surrogate explanations based on perturbations.
Then it asks:
- Are explanations stable?
- Are explanations faithful?
- Are important features manipulable by attackers?
---
# Cell 2 β Install Dependencies
```python
!pip install -q torch numpy pandas scikit-learn datasets shap lime matplotlib scipy
```
### What it does
This line installs all Python packages needed in Google Colab.
- `torch`: PyTorch, used to build and train neural networks.
- `numpy`: numerical arrays and mathematical operations.
- `pandas`: table/dataframe manipulation.
- `scikit-learn`: preprocessing and metrics.
- `datasets`: Hugging Face library to load NSL-KDD.
- `shap`: SHAP explanations.
- `lime`: LIME explanations.
- `matplotlib`: plots and figures.
- `scipy`: statistics such as Pearson and Spearman correlations.
### Why it matters
This prepares the environment. Without these libraries, the rest of the notebook cannot run.
### Mapping to the project
This supports **all tasks** because it installs the tools for training, explaining, evaluating, and plotting.
---
# Cell 3 β Imports, Reproducibility, and Device Setup
```python
import os, sys, json, time, random, pickle
```
Imports standard Python utilities.
- `os`, `sys`: system/file utilities.
- `json`: could be used for saving structured results.
- `time`: used to measure training time.
- `random`: Python random generator.
- `pickle`: can save/load Python objects.
```python
import numpy as np
```
Imports NumPy as `np`. Almost all numerical arrays in preprocessing, SHAP, LIME, and metrics use NumPy.
```python
import pandas as pd
```
Imports pandas as `pd`. The NSL-KDD dataset is converted to pandas DataFrames so we can manipulate columns easily.
```python
import torch
```
Imports PyTorch main library.
```python
import torch.nn as nn
```
Imports PyTorch neural-network module as `nn`. This is used for layers like `Linear`, `LSTM`, `Conv1d`, `BatchNorm`, `Dropout`, and `CrossEntropyLoss`.
```python
from torch.utils.data import TensorDataset, DataLoader
```
Imports utilities to package arrays into datasets and mini-batches.
- `TensorDataset`: wraps tensors `(X, y)` together.
- `DataLoader`: creates batches for training and testing.
```python
from sklearn.preprocessing import LabelEncoder, MinMaxScaler
```
Imports preprocessing tools.
- `LabelEncoder`: converts categorical strings to integers.
- `MinMaxScaler`: scales numerical features into `[0, 1]`.
```python
from sklearn.metrics import classification_report, confusion_matrix, roc_auc_score, average_precision_score
```
Imports evaluation metrics.
- `classification_report`: precision, recall, F1-score.
- `confusion_matrix`: counts correct/incorrect predictions by class.
- `roc_auc_score`: ROC-AUC ranking metric.
- `average_precision_score`: PR-AUC / average precision.
```python
from datasets import load_dataset
```
Imports Hugging Face dataset loader. Used to download/load NSL-KDD.
```python
import shap
```
Imports SHAP explainability library.
```python
from lime import lime_tabular
```
Imports LIME tabular explainer.
```python
from scipy.stats import spearmanr, pearsonr
```
Imports statistical correlation functions.
- `spearmanr`: rank correlation. Used for comparing feature rankings and LIME stability.
- `pearsonr`: linear correlation. Used for SHAP perturbation stability.
```python
import matplotlib.pyplot as plt
```
Imports plotting interface.
```python
import warnings
warnings.filterwarnings('ignore')
```
Suppresses warning messages to keep the Colab output cleaner.
### Reproducibility block
```python
SEED = 42
```
Defines the random seed. A seed is a fixed starting point for randomness.
```python
random.seed(SEED)
```
Fixes Python's built-in random generator.
```python
np.random.seed(SEED)
```
Fixes NumPy randomness. This affects random sample selection for SHAP/LIME and stability tests.
```python
torch.manual_seed(SEED)
```
Fixes PyTorch randomness, such as weight initialization and training randomness.
```python
torch.backends.cudnn.deterministic = True
```
Forces deterministic CUDA operations where possible. This improves reproducibility.
```python
torch.backends.cudnn.benchmark = False
```
Disables CuDNN benchmarking. Benchmarking can choose different algorithms depending on runtime conditions, which hurts reproducibility.
### Device selection
```python
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
```
Checks if a GPU is available. If yes, training uses CUDA GPU; otherwise it uses CPU.
```python
print(f'Device: {DEVICE}')
```
Prints the selected device.
```python
if DEVICE.type == 'cuda':
print(f'GPU: {torch.cuda.get_device_name(0)}')
```
If running on GPU, prints the GPU name. In the final run it was Tesla T4.
### Mapping to the project
This cell establishes reproducibility and compute setup. In an academic report, reproducibility is important because results should be repeatable.
---
# Cell 5 β Feature Names, Dataset Loading, and Class Distribution
```python
FEATURE_NAMES = [ ... ]
```
This list contains the 41 NSL-KDD feature names in the exact order used by the dataset and model.
The list is not just cosmetic. It is needed for:
- selecting feature columns from the DataFrame,
- preserving consistent input order,
- labeling SHAP plots,
- labeling LIME explanations,
- interpreting security implications.
### Lines 1β16 β NSL-KDD features
The features include:
- Basic connection features: `duration`, `protocol_type`, `service`, `flag`, `src_bytes`, `dst_bytes`.
- Content features: `hot`, `num_failed_logins`, `logged_in`, `root_shell`, etc.
- Time-based traffic features: `count`, `srv_count`, `serror_rate`, `rerror_rate`, etc.
- Host-based traffic features: `dst_host_count`, `dst_host_srv_count`, `dst_host_*` rates.
Why this matters: later, when SHAP says `logged_in` is important, we know exactly which IDS feature influenced the model.
```python
CATEGORICAL_COLS = ['protocol_type', 'service', 'flag']
```
Defines the three categorical columns. These contain strings, not numbers, so they must be encoded before feeding them into neural networks.
```python
ds = load_dataset('Mireu-Lab/NSL-KDD')
```
Loads NSL-KDD from Hugging Face.
```python
df_train = ds['train'].to_pandas()
df_test = ds['test'].to_pandas()
```
Converts train and test splits into pandas DataFrames. Pandas makes column operations easier.
```python
print(f'Train: {len(df_train)} | Test: {len(df_test)}')
```
Prints dataset sizes.
Final output:
- Train: 151,165
- Test: 34,394
```python
print('\nTrain distribution:')
print(df_train['class'].value_counts())
```
Prints how many normal/anomaly samples exist in training.
```python
print('\nTest distribution:')
print(df_test['class'].value_counts())
```
Prints class distribution in the test set.
### Why class distribution matters
The train and test distributions are different:
- Train has more normal than anomaly.
- Test has more anomaly than normal.
This matters because the model must generalize under distribution shift.
### Mapping to project
This cell supports the **dataset understanding** part of the report. It proves what data we used and shows imbalance/distribution shift.
---
# Cell 6 β Target Encoding, Categorical Encoding, and Scaling
```python
# Encode target (binary: anomaly=0, normal=1)
```
Comment explaining the binary label setup.
```python
class_names = ['anomaly', 'normal']
```
Defines readable class names. This is used later in classification reports and LIME explanations.
```python
le_y = LabelEncoder()
```
Creates a label encoder for target labels.
```python
y_train = le_y.fit_transform(df_train['class'].values)
```
Fits the encoder on the training labels and transforms them into integers.
In this dataset, the final encoding is:
- anomaly = 0
- normal = 1
```python
y_test = le_y.transform(df_test['class'].values)
```
Transforms test labels using the same encoder learned from training.
Important: we do not fit on test labels, because the test set must remain unseen.
```python
df_tr, df_te = df_train.copy(), df_test.copy()
```
Creates copies of the train and test DataFrames so original data remains unchanged.
```python
label_encoders = {}
```
Creates a dictionary to store encoders for each categorical feature.
```python
for col in CATEGORICAL_COLS:
```
Loops over the categorical columns: protocol_type, service, flag.
```python
le = LabelEncoder()
```
Creates a new encoder for the current categorical column.
```python
le.fit(df_tr[col])
```
Fits the encoder only on training categories.
```python
known = set(le.classes_)
```
Stores categories seen during training.
```python
df_te[col] = df_te[col].apply(lambda x: x if x in known else le.classes_[0])
```
Handles possible unknown categories in test data. If a test category was not seen during training, it is replaced by the first known class.
Why: LabelEncoder cannot transform unseen labels. This prevents runtime errors.
```python
df_tr[col] = le.transform(df_tr[col])
```
Transforms training categorical values into integers.
```python
df_te[col] = le.transform(df_te[col])
```
Transforms test categorical values using the same encoder.
```python
label_encoders[col] = le
```
Stores the encoder for later inspection or inverse transformation.
```python
print(f'Encoded {col}: {len(le.classes_)} categories')
```
Prints how many categories each column has.
Final output:
- protocol_type: 3 categories
- service: 70 categories
- flag: 11 categories
### Scaling
```python
scaler = MinMaxScaler()
```
Creates a scaler that maps each feature to [0, 1].
```python
X_train = scaler.fit_transform(df_tr[FEATURE_NAMES].values.astype(np.float32))
```
Takes training features, converts them to float32, fits the scaler on training data, and transforms training features.
Important: fit only on training data.
```python
X_test = scaler.transform(df_te[FEATURE_NAMES].values.astype(np.float32))
```
Transforms test features using the training scaler.
Again, no fitting on test data to avoid data leakage.
```python
print(f'\nX_train: {X_train.shape} | X_test: {X_test.shape}')
```
Prints feature matrix shapes.
Final output:
- X_train: (151165, 41)
- X_test: (34394, 41)
```python
print(f'y_train: {np.bincount(y_train)} | y_test: {np.bincount(y_test)}')
```
Prints encoded class counts.
### Why this cell is essential
Neural networks cannot directly process strings or unscaled heterogeneous features. This cell converts the raw dataset into clean numerical tensors.
### Mapping to project
This is the **preprocessing pipeline** in the report.
---
# Cell 8 β Model Definitions
This cell defines the three deep learning models.
---
## MLP_IDS
```python
class MLP_IDS(nn.Module):
```
Defines a PyTorch class for the MLP model. It inherits from `nn.Module`, which is required for PyTorch models.
```python
def __init__(self, in_dim=41, num_classes=2):
```
Constructor. Input dimension is 41 because NSL-KDD has 41 features. Number of classes is 2: anomaly and normal.
```python
super().__init__()
```
Initializes the parent PyTorch module.
```python
self.net = nn.Sequential(
```
Creates a sequence of layers that will run one after another.
```python
nn.Linear(in_dim, 256), nn.BatchNorm1d(256), nn.ReLU(), nn.Dropout(0.3),
```
First hidden block:
- `Linear(41, 256)`: maps 41 input features to 256 hidden units.
- `BatchNorm1d(256)`: stabilizes hidden activations.
- `ReLU()`: adds non-linearity.
- `Dropout(0.3)`: randomly drops 30% of activations during training to reduce overfitting.
```python
nn.Linear(256, 128), nn.BatchNorm1d(128), nn.ReLU(), nn.Dropout(0.2),
```
Second hidden block. Reduces representation from 256 to 128.
```python
nn.Linear(128, 64), nn.ReLU(),
```
Third hidden block. Reduces from 128 to 64.
```python
nn.Linear(64, num_classes)
```
Output layer. Produces two logits: one for anomaly and one for normal.
```python
)
```
Ends the sequential model.
```python
for m in self.modules():
```
Loops through all modules/layers inside the model.
```python
if isinstance(m, nn.Linear):
```
Checks if the current module is a linear layer.
```python
nn.init.xavier_uniform_(m.weight)
```
Initializes weights using Xavier uniform initialization. This helps gradients flow well at the start of training.
```python
nn.init.zeros_(m.bias)
```
Initializes biases to zero.
```python
def forward(self, x): return self.net(x)
```
Defines the forward pass. Input `x` passes through `self.net`.
```python
def count_parameters(self): return sum(p.numel() for p in self.parameters() if p.requires_grad)
```
Counts trainable parameters. Used for reporting model size.
### Why MLP is used
MLP is the simplest strong baseline for tabular data. If a complex model beats the MLP, that suggests the extra architecture has value.
---
## LSTM_IDS
```python
class LSTM_IDS(nn.Module):
```
Defines the LSTM model class.
```python
def __init__(self, in_dim=41, hidden_dim=64, num_layers=2, num_classes=2):
```
Constructor. It uses 41 features, hidden size 64, 2 LSTM layers, and 2 output classes.
```python
super().__init__()
```
Initializes parent module.
```python
self.lstm = nn.LSTM(1, hidden_dim, num_layers, batch_first=True, dropout=0.2)
```
Creates an LSTM.
Important detail: each feature is treated as one timestep with one value. So input shape becomes:
```text
batch_size Γ 41 Γ 1
```
- `input_size=1`: each timestep contains one feature value.
- `hidden_dim=64`: LSTM hidden representation size.
- `num_layers=2`: stacked LSTM layers.
- `batch_first=True`: batch dimension comes first.
- `dropout=0.2`: dropout between LSTM layers.
```python
self.fc = nn.Sequential(nn.Linear(hidden_dim, 32), nn.ReLU(), nn.Linear(32, num_classes))
```
Creates a small classifier after the LSTM.
- 64 hidden state β 32 hidden units β 2 output classes.
```python
def forward(self, x):
```
Defines forward pass.
```python
out, (h_n, _) = self.lstm(x.unsqueeze(-1))
```
`x` originally has shape:
```text
batch_size Γ 41
```
`x.unsqueeze(-1)` changes it to:
```text
batch_size Γ 41 Γ 1
```
The LSTM returns:
- `out`: output at all timesteps.
- `h_n`: final hidden states.
- `_`: cell states, ignored.
```python
return self.fc(h_n[-1])
```
Uses the final hidden state from the last LSTM layer and feeds it into the classifier.
```python
def count_parameters(self): return sum(p.numel() for p in self.parameters() if p.requires_grad)
```
Counts trainable parameters.
### Why LSTM is used
Even though NSL-KDD is not a time series, the features have an order and groups. LSTM may learn dependencies across these feature groups.
---
## CNN1D_IDS
```python
class CNN1D_IDS(nn.Module):
```
Defines the 1D-CNN model.
```python
def __init__(self, in_dim=41, num_classes=2):
```
Constructor with 41 input features and 2 output classes.
```python
super().__init__()
```
Initializes parent module.
```python
self.conv = nn.Sequential(
```
Creates convolutional feature extractor.
```python
nn.Conv1d(1, 64, 3, padding=1), nn.BatchNorm1d(64), nn.ReLU(),
```
First convolution block:
- input channels = 1,
- output channels = 64,
- kernel size = 3,
- padding = 1 keeps length 41.
This learns local patterns across neighboring features.
```python
nn.Conv1d(64, 128, 3, padding=1), nn.BatchNorm1d(128), nn.ReLU(),
```
Second convolution block, increasing channels from 64 to 128.
```python
nn.AdaptiveAvgPool1d(8)
```
Compresses the sequence length to 8, regardless of input length.
```python
)
```
Ends convolution block.
```python
self.fc = nn.Sequential(nn.Linear(128*8, 64), nn.ReLU(), nn.Dropout(0.2), nn.Linear(64, num_classes))
```
Classifier after convolution:
- Flattened size = 128 channels Γ 8 pooled positions.
- Dense layer to 64.
- ReLU.
- Dropout.
- Output layer to 2 classes.
```python
def forward(self, x):
```
Defines forward pass.
```python
x = self.conv(x.unsqueeze(1))
```
Original `x` shape is:
```text
batch_size Γ 41
```
`x.unsqueeze(1)` gives:
```text
batch_size Γ 1 Γ 41
```
This is the format Conv1d expects.
```python
return self.fc(x.view(x.size(0), -1))
```
Flattens convolution output and feeds it to classifier.
```python
def count_parameters(self): return sum(p.numel() for p in self.parameters() if p.requires_grad)
```
Counts parameters.
### Final model loop
```python
for name, cls in [('MLP', MLP_IDS), ('LSTM', LSTM_IDS), ('CNN1D', CNN1D_IDS)]:
```
Loops over the three model classes.
```python
m = cls()
```
Instantiates each model.
```python
print(f'{name}: {m.count_parameters():,} parameters')
```
Prints model parameter counts.
### Mapping to project
This cell implements the **Train model** requirement and sets up model comparison.
---
# Cell 10 β Training All Models
This is the largest and most important training cell.
```python
EPOCHS = 50
BATCH_SIZE = 256
LR = 1e-3
```
Defines training hyperparameters:
- train for 50 epochs,
- use mini-batches of 256 samples,
- learning rate is 0.001.
```python
train_ds = TensorDataset(torch.FloatTensor(X_train), torch.LongTensor(y_train))
```
Converts training NumPy arrays into PyTorch tensors and bundles features/labels together.
- Features become float tensors.
- Labels become long integer tensors required by CrossEntropyLoss.
```python
test_ds = TensorDataset(torch.FloatTensor(X_test), torch.LongTensor(y_test))
```
Same for test data.
```python
train_loader = DataLoader(train_ds, batch_size=BATCH_SIZE, shuffle=True)
```
Creates mini-batches for training and shuffles data each epoch.
```python
test_loader = DataLoader(test_ds, batch_size=BATCH_SIZE)
```
Creates test batches. No shuffle is needed because evaluation order does not matter.
### Class weights
```python
counts = np.bincount(y_train)
```
Counts how many examples exist per class.
```python
weights = 1.0 / counts.astype(np.float32)
```
Creates inverse-frequency weights. Smaller classes get larger weight.
```python
weights = weights / weights.sum() * len(weights)
```
Normalizes weights so their average scale is reasonable.
```python
class_weights = torch.FloatTensor(weights).to(DEVICE)
```
Converts weights to PyTorch tensor and moves them to GPU/CPU.
### Why class weights?
Class imbalance can make the model favor the majority class. Weighted loss penalizes mistakes on underrepresented classes more.
---
## train_model function
```python
def train_model(model, model_name):
```
Defines a reusable function to train any of the three models.
```python
print(...)
```
Prints a header showing which model is being trained.
```python
model.to(DEVICE)
```
Moves model to GPU or CPU.
```python
criterion = nn.CrossEntropyLoss(weight=class_weights)
```
Defines classification loss with class weights.
CrossEntropyLoss expects raw logits, so the model does not need Softmax during training.
```python
optimizer = torch.optim.Adam(model.parameters(), lr=LR, weight_decay=1e-4)
```
Creates Adam optimizer.
- `lr=1e-3`: learning rate.
- `weight_decay=1e-4`: L2 regularization to reduce overfitting.
```python
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, patience=5, factor=0.5)
```
Creates learning-rate scheduler. If loss plateaus for 5 epochs, learning rate is halved.
```python
best_f1, history = 0, {'train_loss': [], 'test_acc': []}
```
Initializes best F1 and stores training history.
```python
best_state = None
```
Will store the best model weights.
```python
t0 = time.time()
```
Starts timing training.
```python
for epoch in range(EPOCHS):
```
Training loop over 50 epochs.
```python
model.train()
```
Sets model to training mode. Enables dropout and batchnorm training behavior.
```python
total_loss = 0
```
Initializes epoch loss accumulator.
```python
for xb, yb in train_loader:
```
Loops over training mini-batches.
```python
xb, yb = xb.to(DEVICE), yb.to(DEVICE)
```
Moves batch to GPU/CPU.
```python
optimizer.zero_grad()
```
Clears old gradients.
```python
loss = criterion(model(xb), yb)
```
Runs model forward pass and computes cross-entropy loss.
```python
loss.backward()
```
Backpropagates gradients.
```python
optimizer.step()
```
Updates model weights.
```python
total_loss += loss.item() * len(yb)
```
Adds weighted batch loss to epoch loss.
### Evaluation inside each epoch
```python
model.eval()
```
Sets model to evaluation mode. Dropout is disabled, batchnorm uses learned statistics.
```python
preds, probs, labels = [], [], []
```
Creates lists to collect predictions, probabilities, and labels.
```python
with torch.no_grad():
```
Disables gradient computation to save memory and speed up evaluation.
```python
for xb, yb in test_loader:
```
Loops through test batches.
```python
xb = xb.to(DEVICE)
```
Moves features to GPU/CPU.
```python
out = model(xb)
```
Gets raw logits.
```python
preds.append(out.argmax(1).cpu().numpy())
```
Predicted class is the index of the largest logit.
```python
probs.append(torch.softmax(out, 1).cpu().numpy())
```
Converts logits to class probabilities.
```python
labels.append(yb.numpy())
```
Stores true labels.
```python
preds = np.concatenate(preds)
probs = np.concatenate(probs)
labels = np.concatenate(labels)
```
Combines batch arrays into full test arrays.
```python
report = classification_report(labels, preds, output_dict=True)
```
Computes precision, recall, F1, etc.
```python
wf1 = report['weighted avg']['f1-score']
```
Extracts weighted F1-score.
```python
acc = report['accuracy']
```
Extracts accuracy.
```python
test_loss = total_loss / len(y_train)
```
Despite the variable name, this is actually average training loss for the epoch.
```python
scheduler.step(test_loss)
```
Updates scheduler based on loss.
```python
history['train_loss'].append(total_loss / len(y_train))
history['test_acc'].append(acc)
```
Stores loss and accuracy for plots.
```python
if wf1 > best_f1:
```
Checks if current model is best so far.
```python
best_f1 = wf1
```
Updates best F1.
```python
best_state = {k: v.cpu().clone() for k, v in model.state_dict().items()}
```
Saves a copy of best model weights on CPU.
```python
if (epoch+1) % 10 == 0 or epoch == 0:
```
Prints progress at epoch 1 and every 10 epochs.
```python
print(...)
```
Shows epoch, loss, accuracy, and F1.
### Final evaluation
```python
dt = time.time() - t0
```
Measures total training time.
```python
model.load_state_dict(best_state)
```
Restores best model weights.
```python
model.eval()
```
Sets evaluation mode.
The next block repeats final evaluation on the test set to compute final metrics.
```python
roc = roc_auc_score(labels, probs[:, 1])
```
Computes ROC-AUC using probability of class 1 (`normal`).
```python
pr = average_precision_score(labels, probs[:, 1])
```
Computes PR-AUC / average precision.
```python
print(...)
print(classification_report(...))
print(confusion_matrix(...))
```
Prints final metrics, per-class report, and confusion matrix.
```python
return model, {...}
```
Returns trained model and result dictionary.
### Training all models
```python
models = {}
results = {}
```
Creates dictionaries to store models and results.
```python
for name, cls in [('mlp', MLP_IDS), ('lstm', LSTM_IDS), ('cnn1d', CNN1D_IDS)]:
```
Loops over model classes.
```python
models[name], results[name] = train_model(cls(), name.upper())
```
Instantiates, trains, and stores each model.
### Mapping to project
This cell implements the **Train model** part and produces the model comparison results.
---
# Cells 11 and 12 β Model Summary and Training Curves
## Cell 11
```python
print(f'{"Model":<8} {"Params":>8} {"W-F1":>8} {"ROC-AUC":>9} {"PR-AUC":>8} {"Time":>8}')
```
Prints table header.
```python
print('-'*50)
```
Prints separator line.
```python
for name in ['mlp', 'lstm', 'cnn1d']:
```
Loops over the three trained models.
```python
r = results[name]
```
Gets metric dictionary.
```python
p = models[name].count_parameters()
```
Gets parameter count.
```python
print(...)
```
Prints model name, parameters, F1, ROC-AUC, PR-AUC, and time.
### Why this matters
This is the main quantitative result table in the report.
## Cell 12
```python
fig, axes = plt.subplots(1, 2, figsize=(14, 5))
```
Creates two side-by-side plots.
```python
for name in ['mlp', 'lstm', 'cnn1d']:
```
Loops over models.
```python
axes[0].plot(results[name]['history']['train_loss'], label=name.upper())
```
Plots training loss over epochs.
```python
axes[1].plot(results[name]['history']['test_acc'], label=name.upper())
```
Plots test accuracy over epochs.
```python
axes[0].set_xlabel(...); ...
```
Labels first plot.
```python
axes[1].set_xlabel(...); ...
```
Labels second plot.
```python
plt.tight_layout(); plt.show()
```
Adjusts spacing and displays plots.
### Mapping to project
These plots support training analysis and make the report/presentation visual.
---
# Cell 14 β SHAP Setup and SHAP Value Computation
```python
mlp_cpu = models['mlp'].cpu().eval()
```
Moves the trained MLP to CPU and sets evaluation mode.
Why MLP? The project uses MLP for SHAP explanation because it is a clean tabular baseline and easier to explain consistently.
```python
def predict_fn(X):
```
Defines a prediction wrapper for SHAP and LIME.
```python
with torch.no_grad():
```
No gradients are needed for explanation queries.
```python
return torch.softmax(mlp_cpu(torch.FloatTensor(X)), 1).numpy()
```
Converts NumPy input to PyTorch tensor, runs the MLP, applies Softmax, and returns probabilities.
SHAP and LIME need a function that takes NumPy arrays and returns prediction probabilities.
```python
bg_idx = np.random.choice(len(X_train), 100, replace=False)
```
Randomly selects 100 training samples as SHAP background data.
Background data represents the baseline distribution.
```python
exp_idx = np.random.choice(len(X_test), 150, replace=False)
```
Randomly selects 150 test samples to explain.
Why sample? Kernel SHAP is expensive; explaining the entire test set would take too long.
```python
explainer = shap.KernelExplainer(predict_fn, X_train[bg_idx])
```
Creates a model-agnostic SHAP explainer using prediction function and background data.
```python
print('Computing SHAP values...')
```
Progress message.
```python
shap_values_raw = explainer.shap_values(X_test[exp_idx], nsamples=200, silent=True)
```
Computes SHAP values for 150 test samples using 200 samples for approximation.
```python
if isinstance(shap_values_raw, list):
shap_vals_anomaly = shap_values_raw[0]
elif shap_values_raw.ndim == 3:
shap_vals_anomaly = shap_values_raw[:, :, 0]
else:
shap_vals_anomaly = shap_values_raw
```
Handles different SHAP library output formats.
- Older SHAP returns a list per class.
- Newer SHAP may return a 3D array.
- The code extracts class 0: anomaly.
```python
print(f'Done! Shape: {shap_vals_anomaly.shape}')
```
Prints SHAP array shape. Final shape is `(150, 41)`.
### Mapping to project
This is the core of **Explain predictions** using SHAP.
---
# Cells 15β18 β SHAP Global and Local Explanations
## Cell 15 β Feature Importance
```python
mean_abs_shap = np.abs(shap_vals_anomaly).mean(axis=0)
```
Takes absolute SHAP values and averages across samples.
Why absolute value? We care about magnitude of influence, regardless of direction.
```python
feature_importance = sorted(zip(FEATURE_NAMES, mean_abs_shap), key=lambda x: x[1], reverse=True)
```
Pairs each feature name with its importance and sorts descending.
```python
print('Top 15 features...')
```
Prints heading.
```python
for i, (f, v) in enumerate(feature_importance[:15]):
print(...)
```
Prints top 15 features.
## Cell 16 β SHAP Summary Plot
```python
shap.summary_plot(shap_vals_anomaly, X_test[exp_idx], feature_names=FEATURE_NAMES, max_display=15)
```
Creates SHAP summary plot.
It shows:
- feature importance,
- direction of feature effect,
- distribution of SHAP values,
- top 15 features.
## Cell 17 β SHAP Bar Plot
```python
plt.figure(figsize=(10, 6))
```
Creates figure.
```python
top15 = feature_importance[:15]
```
Selects top 15 SHAP features.
```python
plt.barh(range(15), [v for _, v in top15][::-1], color='steelblue')
```
Draws horizontal bar chart. `[::-1]` reverses order so most important appears at top visually.
```python
plt.yticks(range(15), [f for f, _ in top15][::-1])
```
Labels bars with feature names.
```python
plt.xlabel('Mean |SHAP value|')
plt.title('Top 15 Features β MLP (Anomaly Class)')
plt.tight_layout(); plt.show()
```
Adds labels, title, and displays plot.
## Cell 18 β Local SHAP Explanation
```python
idx = 0
```
Selects first explained test sample.
```python
pred = predict_fn(X_test[exp_idx[idx:idx+1]])
```
Gets prediction probabilities for that sample.
```python
print(f'Sample prediction: anomaly={pred[0][0]:.3f}, normal={pred[0][1]:.3f}')
```
Prints predicted probabilities.
```python
print(f'True label: {class_names[y_test[exp_idx[idx]]]}')
```
Prints true label.
```python
ev = explainer.expected_value
```
Gets SHAP baseline expected output.
```python
ev0 = ev[0] if isinstance(ev, (list, np.ndarray)) else ev
```
Handles expected value format.
```python
shap.force_plot(ev0, shap_vals_anomaly[idx], X_test[exp_idx[idx]], feature_names=FEATURE_NAMES, matplotlib=True)
```
Creates force plot for one prediction.
### Mapping to project
These cells produce the **explanation analysis deliverable**.
---
# Cell 20 β LIME Explanation Analysis
```python
lime_explainer = lime_tabular.LimeTabularExplainer(...)
```
Creates a LIME explainer for tabular data.
```python
X_train
```
Training data is used by LIME to understand feature distributions.
```python
feature_names=FEATURE_NAMES
```
Gives readable feature names.
```python
class_names=class_names
```
Gives readable class labels.
```python
discretize_continuous=True
```
LIME bins continuous features into intervals, making explanations more interpretable.
```python
random_state=SEED
```
Makes LIME sampling reproducible.
```python
n_lime = 30
```
Number of test samples to explain.
```python
lime_idx = np.random.choice(len(X_test), n_lime, replace=False)
```
Randomly selects 30 test samples.
```python
all_top_features = {}
```
Dictionary to count how often each feature appears in LIME top explanations.
```python
for i, idx in enumerate(lime_idx):
```
Loops over selected samples.
```python
exp = lime_explainer.explain_instance(X_test[idx], predict_fn, num_features=10, top_labels=1)
```
Generates a LIME explanation for one sample.
- `num_features=10`: keep top 10 features.
- `top_labels=1`: explain predicted class.
```python
pred_class = np.argmax(predict_fn(X_test[idx].reshape(1, -1)))
```
Gets predicted class for that sample.
```python
for fw in exp.as_list(label=pred_class):
```
Loops over feature-weight pairs in the LIME explanation.
```python
fname = fw[0].split(' ')[0]
```
Extracts feature name from LIME's text rule.
```python
all_top_features[fname] = all_top_features.get(fname, 0) + 1
```
Counts how often this feature appears.
```python
if (i+1) % 10 == 0:
print(...)
```
Progress every 10 samples.
```python
lime_sorted = sorted(all_top_features.items(), key=lambda x: x[1], reverse=True)
```
Sorts features by frequency.
```python
for f, c in lime_sorted[:10]:
print(...)
```
Prints top 10 LIME features.
### Mapping to project
This implements the **Apply explainability** task using LIME.
---
# Cell 21 β SHAP vs LIME Comparison
```python
fig, axes = plt.subplots(1, 2, figsize=(16, 6))
```
Creates two side-by-side plots.
```python
top10_shap = feature_importance[:10]
```
Gets top 10 SHAP features.
```python
axes[0].barh(...)
```
Plots SHAP top 10.
```python
top10_lime = lime_sorted[:10]
```
Gets top 10 LIME features.
```python
axes[1].barh(...)
```
Plots LIME top 10.
```python
plt.suptitle('SHAP vs LIME Feature Rankings', fontsize=14)
plt.tight_layout(); plt.show()
```
Displays comparison plot.
### Rank correlation
```python
shap_ranks = {f: i for i, (f, _) in enumerate(feature_importance[:20])}
```
Creates dictionary mapping SHAP feature to rank.
```python
lime_ranks = {f: i for i, (f, _) in enumerate(lime_sorted[:20])}
```
Creates dictionary mapping LIME feature to rank.
```python
common = set(shap_ranks.keys()) & set(lime_ranks.keys())
```
Finds features appearing in both top-20 lists.
```python
if len(common) >= 5:
```
Only compute correlation if enough overlap exists.
```python
rho, p = spearmanr([...], [...])
```
Computes Spearman rank correlation between SHAP and LIME rankings.
```python
print(...)
```
Prints result.
Final result:
```text
Spearman correlation = 0.0714
p = 0.8665
```
### Interpretation
SHAP and LIME disagree strongly. This is a key finding: explanations depend on method choice.
---
# Cell 23 β SHAP Stability Evaluation
```python
def compute_shap_stability(explainer, sample, epsilon, n_perturbs=10):
```
Defines function to evaluate how stable SHAP is under perturbations.
```python
rng = np.random.RandomState(SEED)
```
Creates deterministic random generator.
```python
base = np.array(explainer.shap_values(sample.reshape(1,-1), nsamples=100, silent=True))
```
Computes original SHAP explanation for the sample.
```python
base = base[0].flatten() if isinstance(base, list) else base.flatten()
```
Flattens SHAP values into one vector.
```python
max_delta, pccs = 0, []
```
Initializes maximum explanation change and list of correlations.
```python
for _ in range(n_perturbs):
```
Repeats perturbation several times.
```python
noise = rng.uniform(-epsilon, epsilon, sample.shape)
```
Creates random noise bounded by epsilon.
```python
perturbed = np.clip(sample + noise, 0, 1)
```
Adds noise and clips features to valid [0,1] range.
```python
p_shap = np.array(explainer.shap_values(perturbed.reshape(1,-1), nsamples=100, silent=True))
```
Computes SHAP explanation for perturbed sample.
```python
p_shap = p_shap[0].flatten() if isinstance(p_shap, list) else p_shap.flatten()
```
Flattens perturbed explanation.
```python
max_delta = max(max_delta, np.linalg.norm(p_shap - base))
```
Computes explanation shift magnitude and keeps maximum. This is SENS_MAX.
```python
if np.std(base) > 1e-8 and np.std(p_shap) > 1e-8:
```
Avoids correlation if vector has near-zero variance.
```python
pccs.append(pearsonr(base, p_shap)[0])
```
Computes Pearson correlation between original and perturbed SHAP values.
```python
return max_delta, np.mean(pccs) if pccs else 0.0
```
Returns SENS_MAX and average PCC.
### Running the test
```python
epsilons = [0.01, 0.03, 0.05]
```
Perturbation sizes.
```python
n_stability = 8
```
Number of samples used for stability test.
```python
stability_idx = np.random.choice(len(X_test), n_stability, replace=False)
```
Randomly selects test samples.
```python
stability_results = {}
```
Stores results.
```python
for eps in epsilons:
```
Loops over perturbation sizes.
```python
sens_list, pcc_list = [], []
```
Stores metrics per sample.
```python
for i, idx in enumerate(stability_idx):
```
Loops over selected samples.
```python
sm, pc = compute_shap_stability(...)
```
Computes SENS_MAX and PCC.
```python
sens_list.append(sm); pcc_list.append(pc)
```
Stores results.
```python
stability_results[eps] = {'sens_max': np.mean(sens_list), 'pcc': np.mean(pcc_list)}
```
Stores average metrics.
```python
status = 'STABLE' if np.mean(pcc_list) > 0.6 else 'UNSTABLE'
```
Classifies explanation stability using threshold 0.6.
### Mapping to project
This implements **Evaluate explanation stability**.
---
# Cell 24 β LIME Stochastic Stability
This evaluates whether LIME gives consistent explanations when run multiple times.
```python
lime_corrs = []
```
Stores average correlation per sample.
```python
for i, idx in enumerate(stability_idx[:6]):
```
Uses first 6 stability samples.
```python
weight_vecs = []
```
Stores LIME weight vectors from different seeds.
```python
for seed in range(10):
```
Runs LIME 10 times with different seeds.
```python
le_obj = lime_tabular.LimeTabularExplainer(..., random_state=seed)
```
Creates a new LIME explainer with a different random seed.
```python
exp = le_obj.explain_instance(..., num_features=len(FEATURE_NAMES))
```
Explains the sample using all features.
```python
w = np.zeros(len(FEATURE_NAMES))
```
Creates a zero vector of feature weights.
```python
for key, val in dict(exp.as_list()).items():
```
Loops over LIME explanation terms.
```python
for j, fn in enumerate(FEATURE_NAMES):
if fn in key: w[j] = val; break
```
Maps LIME text rules back to feature indices.
```python
weight_vecs.append(w)
```
Stores one explanation vector.
```python
corrs = []
```
Stores pairwise correlations.
```python
for a in range(10):
for b in range(a+1, 10):
```
Compares all pairs of the 10 runs.
```python
if np.std(weight_vecs[a]) > 1e-8 and np.std(weight_vecs[b]) > 1e-8:
```
Avoids invalid correlation.
```python
corrs.append(spearmanr(weight_vecs[a], weight_vecs[b])[0])
```
Computes Spearman correlation between two LIME runs.
```python
mc = np.mean(corrs) if corrs else 0
```
Mean correlation for this sample.
```python
lime_corrs.append(mc)
```
Stores it.
```python
lime_status = 'STABLE' if np.mean(lime_corrs) > 0.6 else 'UNSTABLE'
```
Classifies LIME stability.
### Mapping to project
This tests whether LIME explanations are reliable despite LIME randomness.
---
# Cell 25 β Faithfulness Evaluation
Faithfulness asks: do the important features actually matter to the model?
```python
def get_shap_for_class(shap_values, class_idx=0):
```
Helper function for SHAP output formats.
```python
if isinstance(shap_values, list):
return shap_values[class_idx]
```
Older SHAP format.
```python
elif isinstance(shap_values, np.ndarray) and shap_values.ndim == 3:
return shap_values[:, :, class_idx]
```
Newer SHAP 3D format.
```python
else:
return shap_values
```
Fallback.
```python
faith_results = {k: [] for k in [3, 5, 10]}
```
Creates result lists for top-3, top-5, and top-10 feature masking.
```python
for idx in stability_idx[:10]:
```
Loops over up to 10 samples.
```python
sample = X_test[idx]
```
Gets one test sample.
```python
sv_raw = explainer.shap_values(sample.reshape(1,-1), nsamples=100, silent=True)
```
Computes SHAP values.
```python
sv = get_shap_for_class(sv_raw, 0).flatten()
```
Extracts anomaly-class SHAP vector.
```python
base_conf = predict_fn(sample.reshape(1,-1))[0]
```
Gets original prediction probabilities.
```python
pred_cls = np.argmax(base_conf)
```
Gets predicted class.
```python
for k in faith_results:
```
Loops over k = 3, 5, 10.
```python
masked = sample.copy()
```
Copies sample.
```python
masked[np.argsort(np.abs(sv))[-k:]] = 0.0
```
Finds top-k absolute SHAP features and masks them by setting to 0.
```python
drop = base_conf[pred_cls] - predict_fn(masked.reshape(1,-1))[0][pred_cls]
```
Measures confidence drop after masking.
```python
faith_results[k].append(float(drop))
```
Stores confidence drop.
```python
for k, scores in faith_results.items():
print(...)
```
Prints average and standard deviation.
### Mapping to project
This implements **Evaluate explanation faithfulness**.
---
# Cell 26 β Stability Summary Plot
```python
fig, axes = plt.subplots(1, 3, figsize=(16, 5))
```
Creates three plots side by side.
```python
eps_list = list(stability_results.keys())
```
Gets epsilon values.
```python
axes[0].plot(eps_list, [stability_results[e]['sens_max'] for e in eps_list], ...)
```
Plots SENS_MAX vs epsilon.
```python
pcc_vals = [stability_results[e]['pcc'] for e in eps_list]
```
Gets PCC values.
```python
colors = ['green' if p > 0.6 else 'red' for p in pcc_vals]
```
Green bars for stable, red for unstable.
```python
axes[1].bar(...)
```
Plots PCC stability bars.
```python
axes[1].axhline(y=0.6, ...)
```
Draws stability threshold line.
```python
ks = list(faith_results.keys())
```
Gets masking sizes 3, 5, 10.
```python
axes[2].bar(...)
```
Plots faithfulness confidence drop with error bars.
```python
plt.suptitle(...)
plt.tight_layout(); plt.show()
```
Adds title and displays.
### Mapping to project
This creates the figure used to summarize explanation reliability.
---
# Cell 28 β Security Implications / Feature Manipulability
```python
manipulable = {...}
```
Defines features that attackers may directly influence.
Examples:
- `src_bytes`
- `dst_bytes`
- `hot`
- `num_failed_logins`
- `duration`
```python
partial = {...}
```
Defines partially manipulable features.
Examples:
- `count`
- `srv_count`
- `serror_rate`
- `rerror_rate`
- `protocol_type`
- `flag`
These can sometimes be influenced but not freely controlled.
```python
non_manip = {...}
```
Defines non-manipulable features such as host-level aggregated statistics.
Examples:
- `dst_host_count`
- `dst_host_srv_count`
- `dst_host_rerror_rate`
- `dst_host_serror_rate`
These are computed by IDS sensors or depend on broader traffic context.
```python
manip_count = {'Manipulable': 0, 'Partial': 0, 'Non-manipulable': 0}
```
Initializes counters.
```python
for i, (f, v) in enumerate(feature_importance[:15]):
```
Loops over top 15 SHAP features.
```python
if f in manipulable:
status = 'MANIPULABLE'
manip_count['Manipulable'] += 1
```
Classifies feature as manipulable.
```python
elif f in partial:
status = 'PARTIAL'
manip_count['Partial'] += 1
```
Classifies feature as partially manipulable.
```python
else:
status = 'NON-MANIPULABLE'
manip_count['Non-manipulable'] += 1
```
Otherwise classifies as non-manipulable.
```python
print(...)
```
Prints feature, SHAP value, and manipulability status.
```python
print(f'\nSummary: {manip_count}')
```
Prints count summary.
```python
if manip_count['Non-manipulable'] > manip_count['Manipulable']:
print('-> Model relies more on non-manipulable features -> MORE ROBUST against evasion')
else:
print('-> Model relies more on manipulable features -> LESS ROBUST against evasion')
```
Simple security conclusion.
### Mapping to project
This implements **Analyze security implications**.
---
# Cell 29 β Final Summary
```python
print('\n' + '='*60)
print('FINAL RESULTS SUMMARY')
print('='*60)
```
Prints final report-style summary header.
```python
print(f'\n1. MODEL COMPARISON:')
```
Starts model result section.
```python
for name in ['mlp', 'lstm', 'cnn1d']:
r = results[name]
print(...)
```
Prints F1, ROC-AUC, and PR-AUC for all models.
```python
print(f'\n2. EXPLANATION STABILITY (SAFARI):')
```
Starts stability section.
```python
for eps in epsilons:
sr = stability_results[eps]
status = 'STABLE' if sr['pcc'] > 0.6 else 'UNSTABLE'
print(...)
```
Prints SHAP stability for each epsilon.
```python
print(f' LIME: Spearman={np.mean(lime_corrs):.4f} ...')
```
Prints LIME stability.
```python
print(f'\n3. FAITHFULNESS:')
```
Starts faithfulness section.
```python
for k in [3, 5, 10]:
print(...)
```
Prints confidence drop after masking top-k SHAP features.
```python
print(f'\n4. SECURITY: Top features manipulability = {manip_count}')
```
Prints security summary.
```python
print('\nDone!')
```
End message.
### Mapping to project
This cell packages all deliverable results:
- model comparison,
- stability,
- faithfulness,
- security analysis.
---
# How the Whole Notebook Maps to the Project Requirements
| Teacher requirement | Notebook cells | What was done |
|---|---|---|
| Train model | Cells 5β12 | Load data, preprocess, train MLP/LSTM/CNN, evaluate metrics |
| Explain predictions | Cells 14β21 | SHAP and LIME explanations, feature rankings, local explanations |
| Evaluate stability | Cells 23β26 | SHAP perturbation stability, LIME stochastic stability |
| Analyze risks | Cell 28 | Feature manipulability and evasion risk analysis |
| Expected output | Cell 29 + figures | Final result summary, plots, explanation/security analysis |
---
# The Main Story You Should Understand
The code starts with raw NSL-KDD network connection records. It converts them into numerical normalized feature vectors. Then it trains three neural IDS models and compares them. The LSTM performs best.
After training, the notebook does not stop at accuracy. It asks: why did the model make its decisions? SHAP and LIME are used to identify important features. SHAP finds features like `logged_in` and error-rate statistics. LIME finds some overlapping but different features. Their low Spearman correlation shows that XAI methods can disagree.
Then the notebook asks whether the explanations are reliable. SHAP is stable only for very small perturbations. LIME is borderline stable. Feature masking shows SHAP explanations are reasonably faithful because removing top SHAP features reduces prediction confidence.
Finally, the code asks whether explanations are safe. If top features are manipulable by attackers, explanations can leak evasion strategies. The model relies on several non-manipulable or partially manipulable features, which is a positive sign, but explanation access should still be controlled.
---
# Key Things to Say if Asked About the Code
1. The preprocessing avoids data leakage by fitting encoders/scalers on training data and transforming test data.
2. The three models are compared fairly because they use the same dataset, preprocessing, and training setup.
3. Weighted F1 is important because class distributions are not perfectly balanced and train/test distributions differ.
4. SHAP gives global and local feature importance.
5. LIME gives local surrogate explanations.
6. SHAP and LIME disagree, which is an important result, not a failure.
7. Stability is evaluated because explanations must be consistent to be trusted.
8. Faithfulness is evaluated because important explanation features should actually affect predictions.
9. Security analysis checks whether important features can be manipulated by attackers.
10. The whole project is not just IDS accuracy; it is IDS + explanation + reliability + security.
|