How-to articleMethodology & analyticsComputer science

Choosing and preparing a dataset for a machine-learning intrusion detection study

The dataset decides what a machine-learning intrusion detection study can claim. This guide shows how to choose a public dataset, decide on labels, split the data without leakage, handle scaling and class imbalance, and report it all so that examiners can trust your results.

What you will learn

  • Compare widely used intrusion detection datasets and match one to your research question
  • Choose between binary and multiclass labels and check how labels were produced
  • Split data into training, validation and test sets without leakage, including time-based splits
  • Distinguish feature scaling, class imbalance and overfitting, and handle each correctly
  • Report data preparation and evaluation in a way examiners can check

What you will unlearn

  • KDD Cup 1999 is a good default dataset because it is widely used.It is decades old and contains many duplicate records, so it suits historical comparison, not claims about current networks.
  • Removing rows from over-represented attacks fixes overfitting.Undersampling addresses class imbalance; overfitting is tackled by regularisation, simpler models and honest validation.
  • Between 100,000 and 200,000 records is enough for a neural network.No fixed number exists; what matters is examples per class and whether a learning curve is still rising.
  • A high accuracy on a random test split shows the model will work in practice.Random splits of the same traffic hide drift and leakage; test on later traffic or a second dataset.

Skills you will practise

  • Dataset selection
  • Label design
  • Leakage-free data splitting
  • Class imbalance handling
  • Precision-recall evaluation
  • Reproducible data pipelines

Why the dataset decides what your IDS study can claim

An intrusion detection system (IDS) monitors network or host activity and flags behaviour that suggests an attack or policy violation. Machine learning approaches learn to separate malicious from benign activity from labelled examples, usually records of network flows or packets described by features such as duration, protocol, ports, byte and packet counts and TCP flags. Whatever model you build, it can only learn the patterns present in its training data, so the choice and preparation of the dataset shape every result.

This is also where many studies go wrong. Sommer and Paxson (2010) argued that machine learning is harder to apply to intrusion detection than to other domains, partly because realistic, well-labelled data are scarce and evaluation is difficult. A later review of security papers found pitfalls such as sampling bias, label errors, data snooping and lab-only evaluation to be common (Arp et al., 2022). A doctoral study that treats dataset selection as a design decision, rather than a download, avoids most of them.

Chart of the most frequent attack types reported in 2018
Figure 1. Top attack types (source: McAfee Labs Threats Report, December 2018).

Step 1: choose a dataset that matches your research question

Surveys of network intrusion datasets compare them on properties such as how the traffic was generated, whether it is labelled, which attacks it contains, its format (packets or flows), its age and how much metadata describes the setting (Ring et al., 2019). Start from your research question and threat model, then ask which dataset contains that kind of traffic and those attacks.

Table 1. Widely used public intrusion detection datasets and what to check before using them
DatasetOriginLabelsPoints to check
KDD Cup 1999 (and the cleaned NSL-KDD)Derived from the 1998 DARPA evaluation trafficNormal plus four attack categoriesDecades old, with many duplicate records in the original; use only for historical comparison
UNSW-NB15Generated in a cyber range at UNSW Canberra (2015)Binary label and nine attack categoriesSynthetic traffic mix; check how the official training and test splits were built
CIC-IDS2017Testbed with profiled benign users over five days (Sharafaldin et al., 2018)Benign and several attack familiesStrong class imbalance; attacks grouped by day; check for published corrections
UGR'16NetFlow from a Spanish internet service provider over several monthsBackground traffic with labelled attacksReal background traffic but synthetic attacks; flow data only
Bot-IoTIoT testbed with botnet scenarios (Koroniotis et al., 2019)Attack categories and subcategoriesDominated by attack traffic, the reverse of real networks
ToN_IoTIoT and industrial IoT testbed (Booij et al., 2022)Normal and several attack typesHeterogeneous sources; feature sets differ from other datasets

Two consequences follow. First, results on different datasets are rarely comparable, because feature sets and attack labels differ; Booij et al. (2022) argued for standardised features and attack types for exactly this reason. Second, attacks and benign behaviour change over time, so a model that performs well on an old dataset may fail on current traffic. Testing on at least two datasets, or on data collected later than the training data, makes your conclusions much stronger.

Step 2: decide on binary or multiclass labels

Some datasets label each record only as normal or attack; others name the attack category. Binary labels suit studies of detection, where the operational question is whether to raise an alert. Multiclass labels suit studies of attack identification or response, but rare classes will have few examples and may need to be merged or reported separately.

Check how labels were produced. Labels in testbed datasets come from the attack schedule, which can mislabel benign flows that occur during an attack window, and labels in real traffic often come from other detection tools, which carries their errors into your ground truth (Arp et al., 2022).

Step 3: split the data and judge how much you need

A common rule of thumb suggests 100,000 to 200,000 records for neural networks and recommended keeping 20% "for the actual classification". There is no fixed number. What matters is the number of examples in each class, especially the rarest attacks, and whether performance is still improving as you add data, which a learning curve shows. The 20% is the test set: data that the model never sees during training or tuning.

  • Three partitions. Use a training set to fit the model, a validation set (or cross-validation within the training data) to tune it, and a test set touched only once, at the end.
  • Split before any preprocessing. Fitting scalers, feature selection or resampling on the whole dataset leaks information from the test set into training (Kapoor & Narayanan, 2023).
  • Remove duplicates across partitions. Identical records in training and test sets inflate accuracy.
  • Respect time. Where records carry timestamps, train on earlier traffic and test on later traffic. Random splits let a model learn from the future and overstate how it would perform after deployment (Pendlebury et al., 2019; Arp et al., 2022).

Step 4: handle scaling and class imbalance correctly

The original section on "data scaling" actually described deleting rows and reducing over-represented attack classes, and claimed that this would cure overfitting. These are separate issues.

  • Scaling means transforming feature values, for example standardising each feature to zero mean and unit variance, so that features with large ranges such as byte counts do not dominate distance-based models or slow neural network training. Fit the scaler on the training data only.
  • Class imbalance means some classes vastly outnumber others. Undersampling the majority class, oversampling the minority class or weighting classes in the loss function can help the model learn rare attacks. Resample only the training data, never the test set.
  • Overfitting means learning noise in the training data. It is addressed by regularisation, simpler models, more data and honest validation, not by deleting rows.

Imbalance also changes how results should be reported. When attacks are rare, accuracy can be high for a model that never detects them, and ROC curves can look reassuring. Precision-recall curves are more informative for imbalanced data (Saito & Rehmsmeier, 2015), and a false positive rate that sounds small can still flood analysts with alerts when benign traffic vastly outnumbers attacks, the base rate problem (Arp et al., 2022).

Step 5: choose a model, and a baseline, to suit the data

Deep neural networks are popular for intrusion detection, but they are not automatically the best choice. Most deep IDS classifiers are trained with supervised learning on labelled records. The input layer receives the features; hidden layers learn intermediate representations from them; and the output layer gives either two classes (normal or attack) or one class per attack type. Recurrent networks, which feed information forward across time steps, suit sequences of packets or flows; autoencoders trained only on benign traffic are used for anomaly detection.

Diagram of input, hidden and output layers of a deep neural network
Figure 2. Layers of a deep neural network (from a companion post in the original series).

Always include simpler baselines, such as logistic regression, random forests or gradient boosting on the same features and splits, so that any gain from a deep model is measured rather than assumed (Arp et al., 2022). Deep models usually need more labelled data and more tuning, and their advantage is clearest on raw or sequential inputs. The claim that real-time learning is not feasible is also outdated: incremental (online) learning methods update models as data arrive. The practical limit is that new data need trustworthy labels.

Worked example: preparing data for a flow-based IDS study (illustrative)

Illustrative case. A doctoral researcher asks whether a gradient-boosting model trained on flow features detects brute-force and denial-of-service attacks better than a deep network, and whether either holds up on traffic from a different network.

  1. Datasets. CIC-IDS2017 for development (Sharafaldin et al., 2018), UNSW-NB15 as a second dataset; KDD Cup 1999 is cited only for historical context.
  2. Labels. Binary labels for the main question, with a secondary multiclass analysis for the two attack families.
  3. Splits. Within CIC-IDS2017, earlier days for training and validation, a later day for testing; duplicates removed across partitions; scaler and resampling fitted on training data only (Kapoor & Narayanan, 2023).
  4. Cross-dataset test. Features mapped to a shared subset so the trained models can be tested on UNSW-NB15, with the mapping documented (Booij et al., 2022).
  5. Evaluation. Precision, recall and precision-recall curves per class, false positives per day of benign traffic, and results for a random forest baseline (Saito & Rehmsmeier, 2015; Arp et al., 2022).
  6. Reproducibility. Code, random seeds, dataset versions and preprocessing scripts archived (Sandve et al., 2013).

What to report in your methods chapter

Table 2. What to report about the data in a machine-learning IDS study
ElementWhat to state
DatasetName, version, source, year and how the traffic was generated
LabelsBinary or multiclass, how labels were produced and any relabelling or merging
CleaningDuplicates, missing values and records removed, with counts
SplitsPartition sizes, time or random basis, and how leakage was prevented
PreprocessingScaling, encoding, feature selection and resampling, fitted on training data only
Class balanceClass counts in every partition
ModelsAll models including baselines, with hyperparameters and tuning procedure
MetricsPer-class precision and recall, precision-recall curves, false positive rates
GeneralisationResults on a second dataset or later traffic

Common mistakes

  • Using KDD Cup 1999 as the only dataset for claims about current networks.
  • Fitting scalers, feature selection or resampling before splitting the data (Kapoor & Narayanan, 2023).
  • Resampling the test set so that it no longer reflects real class proportions.
  • Reporting accuracy alone on imbalanced data (Saito & Rehmsmeier, 2015).
  • Testing on a random split of the same traffic and calling the result generalisation (Pendlebury et al., 2019).
  • Comparing a deep model with no simpler baseline (Arp et al., 2022).

For more on evaluation, see why lab accuracy does not survive real networks, adversarial attacks on machine-learning IDS, flaws in cybersecurity PhD methodology and the annotated LSTM intrusion detection example.

Key takeaways

Your next step

Want to apply this to your own study?

  1. 1Tell us your topic, data and deadline
  2. 2A subject mentor reviews your work with you
  3. 3You get a plan and a quote
Primary data collection

References

  1. Arp, D., Quiring, E., Pendlebury, F., Warnecke, A., Pierazzi, F., Wressnegger, C., Cavallaro, L., & Rieck, K. (2022). Dos and don'ts of machine learning in computer security. In Proceedings of the 31st USENIX Security Symposium (pp. 3971–3988). USENIX Association. https://www.usenix.org/conference/usenixsecurity22/presentation/arp
  2. Booij, T. M., Chiscop, I., Meeuwissen, E., Moustafa, N., & den Hartog, F. T. H. (2022). ToN_IoT: The role of heterogeneity and the need for standardization of features and attack types in IoT network intrusion data sets. IEEE Internet of Things Journal, 9(1), 485–496. https://doi.org/10.1109/JIOT.2021.3085194
  3. Kapoor, S., & Narayanan, A. (2023). Leakage and the reproducibility crisis in machine-learning-based science. Patterns, 4(9), 100804. https://doi.org/10.1016/j.patter.2023.100804
  4. Koroniotis, N., Moustafa, N., Sitnikova, E., & Turnbull, B. (2019). Towards the development of realistic botnet dataset in the Internet of Things for network forensic analytics: Bot-IoT dataset. Future Generation Computer Systems, 100, 779–796. https://doi.org/10.1016/j.future.2019.05.041
  5. Pendlebury, F., Pierazzi, F., Jordaney, R., Kinder, J., & Cavallaro, L. (2019). TESSERACT: Eliminating experimental bias in malware classification across space and time. In Proceedings of the 28th USENIX Security Symposium (pp. 729–746). USENIX Association. https://www.usenix.org/conference/usenixsecurity19/presentation/pendlebury
  6. Ring, M., Wunderlich, S., Scheuring, D., Landes, D., & Hotho, A. (2019). A survey of network-based intrusion detection data sets. Computers & Security, 86, 147–167. https://doi.org/10.1016/j.cose.2019.06.005
  7. Saito, T., & Rehmsmeier, M. (2015). The precision-recall plot is more informative than the ROC plot when evaluating binary classifiers on imbalanced datasets. PLoS ONE, 10(3), e0118432. https://doi.org/10.1371/journal.pone.0118432
  8. Sandve, G. K., Nekrutenko, A., Taylor, J., & Hovig, E. (2013). Ten simple rules for reproducible computational research. PLoS Computational Biology, 9(10), e1003285. https://doi.org/10.1371/journal.pcbi.1003285
  9. Sharafaldin, I., Lashkari, A. H., & Ghorbani, A. A. (2018). Toward generating a new intrusion detection dataset and intrusion traffic characterization. In Proceedings of the 4th International Conference on Information Systems Security and Privacy (ICISSP 2018) (pp. 108–116). SciTePress. https://doi.org/10.5220/0006639801080116
  10. Sommer, R., & Paxson, V. (2010). Outside the closed world: On using machine learning for network intrusion detection. In 2010 IEEE Symposium on Security and Privacy (pp. 305–316). IEEE. https://www.icsi.berkeley.edu/icsi/publication_details?n=2818

How to cite this article

Choose your referencing style, then copy. Check your university's or journal's guide for local variations.

PhD Research Lab Academy. (2019, October 16). Choosing and preparing a dataset for a machine-learning intrusion detection study. PhD Research Lab. https://www.phdassistance.com/blog/dissertation-algorithm-development-tips-for-developing-an-effective-machine-learning-algorithm-and-select-best-dataset-for-building-ids-models/

PhD Research Lab Academy. “Choosing and Preparing a Dataset for a Machine-learning Intrusion Detection Study.” PhD Research Lab, 16 Oct. 2019, www.phdassistance.com/blog/dissertation-algorithm-development-tips-for-developing-an-effective-machine-learning-algorithm-and-select-best-dataset-for-building-ids-models. Accessed [access date].

PhD Research Lab Academy (2019) Choosing and preparing a dataset for a machine-learning intrusion detection study. PhD Research Lab. Available at: https://www.phdassistance.com/blog/dissertation-algorithm-development-tips-for-developing-an-effective-machine-learning-algorithm-and-select-best-dataset-for-building-ids-models/ (Accessed: [access date]).

PhD Research Lab Academy. “Choosing and Preparing a Dataset for a Machine-learning Intrusion Detection Study.” PhD Research Lab. October 16, 2019. https://www.phdassistance.com/blog/dissertation-algorithm-development-tips-for-developing-an-effective-machine-learning-algorithm-and-select-best-dataset-for-building-ids-models.

PhD Research Lab Academy, ‘Choosing and Preparing a Dataset for a Machine-learning Intrusion Detection Study’ (PhD Research Lab, 16 October 2019) <https://www.phdassistance.com/blog/dissertation-algorithm-development-tips-for-developing-an-effective-machine-learning-algorithm-and-select-best-dataset-for-building-ids-models> accessed [access date].

PhD Research Lab Academy. Choosing and preparing a dataset for a machine-learning intrusion detection study [Internet]. PhD Research Lab; 2019 Oct 16 [cited [access date]]. Available from: https://www.phdassistance.com/blog/dissertation-algorithm-development-tips-for-developing-an-effective-machine-learning-algorithm-and-select-best-dataset-for-building-ids-models/

PhD Research Lab Academy, “Choosing and preparing a dataset for a machine-learning intrusion detection study,” PhD Research Lab, Oct. 16, 2019. [Online]. Available: https://www.phdassistance.com/blog/dissertation-algorithm-development-tips-for-developing-an-effective-machine-learning-algorithm-and-select-best-dataset-for-building-ids-models. [Accessed: [access date]].

@online{dissertation_algorithm_development_tips_for_developing_an_effective_machine_learning_algorithm_and_select_best_dataset_for_building_ids_models_2019,
  author = {{PhD Research Lab Academy}},
  title = {{Choosing and preparing a dataset for a machine-learning intrusion detection study}},
  organization = {PhD Research Lab},
  date = {2019-10-16},
  url = {https://www.phdassistance.com/blog/dissertation-algorithm-development-tips-for-developing-an-effective-machine-learning-algorithm-and-select-best-dataset-for-building-ids-models},
  urldate = {[access date]}
}

This article is for learning. Use it to understand the method and cite the original sources above in your own work; do not copy or submit it as your own.

Choosing and preparing a dataset for a machine-learning intrusion detection studNeed help applying it to your study?
Primary data collection