Najma Nour
← All articles

Case Study: Nature-Inspired Feature Selection for Intrusion Detection

Genetic Algorithm, Particle Swarm and Ant Colony Optimisation compared under one fair protocol on two intrusion detection benchmarks — up to 80% fewer features with the same or better detection.

  • cybersecurity
  • optimisation
  • feature selection
  • swarm intelligence
80% fewer featuresCASE STUDY /Intrusiondetection.NAJMA NOUR

An intrusion detection system watches network traffic and flags attacks. Every connection is described by dozens or hundreds of features, and many of them are redundant or noisy — they slow the model down and can even hurt detection. This project asks a simple question: which nature-inspired search algorithm is best at picking the features that matter? I built it for the Nature-Inspired Computing course in my M.Sc. at Canadian University Dubai.

3
Algorithms written from scratch: GA, PSO, ACO
2
Benchmarks: NSL-KDD and CIC-IDS-2017
80%
Fewer features with ACO, on both datasets
+9.5 pts
Detection-rate gain from PSO on NSL-KDD
99.9%
Accuracy on CIC-IDS-2017 (GA)
2¹²²
Possible feature subsets on NSL-KDD

The problem

Choosing a subset of features is a search problem with an enormous space: with 122 features there are 2¹²² possible subsets, far too many to try. Nature-inspired algorithms explore that space the way evolution, bird flocks or ant colonies solve problems — keeping what works and gradually improving it. Many papers compare these methods, but often with different data, classifiers or settings, which makes the comparison unfair. The goal here was a clean, like-for-like test.

A fair comparison

All three algorithms share exactly the same data, preprocessing, classifier, fitness function and metrics. The only thing that changes is the search strategy, so any difference in the results comes from the algorithm alone.

  1. 1

    Pre-process the traffic data

    Impute missing values, one-hot encode categorical fields and min-max scale everything; labels become binary — normal or attack.

  2. 2

    Search for a feature subset

    GA, PSO or ACO proposes subsets; each is scored by the same wrapper fitness function using a Random Forest and k-fold cross-validation.

  3. 3

    Stop when progress stalls

    Each algorithm stops early if its best fitness has not improved for a set number of generations.

  4. 4

    Evaluate on the full test set

    Train a 100-tree Random Forest on the chosen features and measure accuracy, detection rate, F1, false-positive rate, feature reduction and runtime.

  5. 5

    Test for significance

    Repeat the runs and compare the algorithms with Wilcoxon and Friedman tests.

Fitness(C) = 0.9 · F1(C) − 0.1 · |C| / n

C is a candidate feature subset, |C| the number of features it keeps and n the total. The score rewards detection quality and gently penalises large subsets.

How each algorithm searches

AlgorithmInspired byHow a subset is representedHow the search moves
Genetic Algorithm (GA)EvolutionBinary chromosome — 1 keeps a featureRoulette-wheel selection, single-point crossover (p = 0.8), bit-flip mutation (p = 0.02)
Particle Swarm (PSO)Bird flocksContinuous position, thresholded with a sigmoidInertia decays from 0.9 to 0.4, c₁ = c₂ = 2.0
Ant Colony (ACO)Foraging antsPheromone level per featureSampling by pheromone and heuristic (α = 1, β = 2), evaporation ρ = 0.1

Results

Both benchmarks come from the Canadian Institute for Cybersecurity. NSL-KDD is the harder one, with 122 features after encoding; CIC-IDS-2017 has 78 features of modern traffic.

Features kept on NSL-KDD

Out of 122 features. Fewer features means a smaller, faster model.

  • All features122
  • PSO52
  • GA43
  • ACO25

Bar length = features

Detection rate on NSL-KDD

Share of attacks correctly detected on the full test set (%).

  • PSO70.3
  • ACO62.2
  • GA60.8
  • All features60.8

Bar length = % of attacks detected

The trade-off: features kept vs attacks detected (NSL-KDD)

Every algorithm moved left of the baseline (smaller model); PSO also moved clearly up (more attacks caught).

55%60%65%70%75%0265278104130Features keptDetection rateAll featuresGAPSOACO

Up and to the left is better: more attacks detected with fewer features. Hover a point for details.

Features kept on CIC-IDS-2017

Out of 78 features — every subset matched or beat the baseline's 99.8% accuracy.

  • All features78
  • PSO44
  • GA29
  • ACO16

Bar length = features

Full results — NSL-KDD (122 features)

100-tree Random Forest on the full held-out test set.

MethodFeaturesAccuracyDetection rateF1False-positive rate
Baseline (all)12276.5%60.8%0.7462.74%
GA43 (65% fewer)76.4%60.8%0.7462.85%
PSO52 (57% fewer)81.9%70.3%0.8162.63%
ACO25 (80% fewer)77.3%62.2%0.7572.86%

Full results — CIC-IDS-2017 (78 features)

MethodFeaturesAccuracyDetection rateF1False-positive rate
Baseline (all)7899.81%99.27%0.9950.06%
GA29 (63% fewer)99.90%99.64%0.9970.04%
PSO44 (44% fewer)99.82%99.29%0.9950.05%
ACO16 (80% fewer)99.85%99.48%0.9960.05%

Every algorithm matched or beat the all-features baseline while using far fewer features. On CIC-IDS-2017, ACO kept just 16 of 78 features and still detected more attacks than the full model.

Which algorithm to choose

If you need…ChooseWhy
The smallest modelACOAbout 80% of features removed on both datasets
The highest detection ratePSO+9.5 points on the harder NSL-KDD benchmark
The best all-round scoresGATop accuracy, F1 and lowest false-positive rate on CIC-IDS-2017

How the searches converged

The convergence curves track the best fitness found so far at each generation. ACO starts strong and settles within a few iterations; PSO begins lowest but climbs steadily as the swarm closes in; GA improves slowly and evenly.

Convergence of GA, PSO and ACO

Best-so-far fitness per generation on each dataset, from the pilot-scale run in the repository. Original figure from the project's code.

Two line charts of best-so-far fitness against generation for GA, PSO and ACO on NSL-KDD and CIC-IDS-2017. ACO reaches the highest fitness quickly, GA rises slowly, PSO starts lowest and climbs steadily.

Swipe sideways to see the full figure.

Are the differences real?

Good results in one run can be luck, so the runs were repeated and tested statistically.

Statistical tests on F1 scores

p < 0.05 would indicate a significant difference.

TestNSL-KDDCIC-IDS-2017
Friedman (all three algorithms)p = 0.25p = 0.55
Wilcoxon GA vs PSOp = 0.63p = 0.44
Wilcoxon GA vs ACOp = 0.81p = 0.81
Wilcoxon PSO vs ACOp = 0.13p = 0.63

Built to be reproduced

The algorithms are written in plain NumPy, following their published pseudocode, with no metaheuristic libraries. The project is packaged as a Python library with a command-line tool: one command re-runs every experiment and rebuilds every table and figure.

Engineering

AreaDetails
AlgorithmsGA, PSO and ACO from scratch in NumPy
Modellingscikit-learn Random Forest, stratified k-fold cross-validation
StatisticsSciPy Wilcoxon and Friedman tests
ReproducibilityFixed seeds, YAML configs (fast and paper-scale), CLI: nifs run / nifs stats
Qualitypytest suite on synthetic data, Ruff linting, GitHub Actions CI on Python 3.10–3.12

Limitations

  • Results come from a pilot-scale search (populations of 12, 8 generations, 6,000 training rows for fitness); the paper-scale configuration (50 × 100, all rows, 30 runs) takes several hours and would give stronger statistics.
  • NSL-KDD is hard for every method — detection stays around 60–70% — while CIC-IDS-2017 is close to the ceiling, which leaves little room to separate the algorithms.
  • Both datasets are binary (normal vs attack); multi-class attack types are a natural next step.

What I learned

A fair experiment is worth more than a flashy number. Holding everything constant except the search strategy, and then testing the results statistically, showed that the real gain came from selecting features at all — not from which algorithm did it. Writing the three algorithms from scratch also made their trade-offs concrete: ants prune aggressively, swarms explore widely, and evolution balances the two.