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
//! KDD Cup 1999 network-intrusion dataset.
//!
//! The KDD Cup 1999 dataset is for network-intrusion detection. It is identical
//! to what scikit-learn exposes through `fetch_kddcup99`. Each sample is a single
//! network connection described by 41 features. The task is to classify the
//! connection as `normal.` traffic or as one of 22 attack types.
//!
//! Like scikit-learn, this loader has two partitions:
//! - [`Kddcup99::new`](crate::Kddcup99::new): the **10% subset** (494,021
//! samples), scikit-learn's default (`fetch_kddcup99(percent10=True)`).
//! - [`Kddcup99::new_full`](crate::Kddcup99::new_full): the **full set**
//! (4,898,431 samples), `fetch_kddcup99(percent10=False)`.
//!
//! Both partitions share the same 42 columns and the same 23 connection
//! classes. They differ only in sample count and in the upstream source file.
//!
//! **Columns (42):**
//!
//! | Name | Type | Description |
//! |------|------|-------------|
//! | `duration` | `Numeric` | length of the connection in seconds |
//! | `protocol_type` | `String` | `tcp`, `udp`, or `icmp` |
//! | `service` | `String` | destination service, such as `http` or `smtp` |
//! | `flag` | `String` | connection status flag, such as `SF`, `S0`, or `REJ` |
//! | `src_bytes` | `Numeric` | bytes sent from the source to the destination |
//! | `dst_bytes` | `Numeric` | bytes sent from the destination to the source |
//! | `land` | `Numeric` | `1` if the source and destination address and port are equal |
//! | `wrong_fragment` | `Numeric` | number of wrong fragments |
//! | `urgent` | `Numeric` | number of urgent packets |
//! | `hot` | `Numeric` | number of hot indicators |
//! | `num_failed_logins` | `Numeric` | number of failed login attempts |
//! | `logged_in` | `Numeric` | `1` if the login succeeded |
//! | `num_compromised` | `Numeric` | number of compromised conditions |
//! | `root_shell` | `Numeric` | `1` if the connection obtained a root shell |
//! | `su_attempted` | `Numeric` | `1` if the connection attempted the `su root` command |
//! | `num_root` | `Numeric` | number of root accesses |
//! | `num_file_creations` | `Numeric` | number of file creation operations |
//! | `num_shells` | `Numeric` | number of shell prompts |
//! | `num_access_files` | `Numeric` | number of operations on access control files |
//! | `num_outbound_cmds` | `Numeric` | number of outbound commands in an FTP session |
//! | `is_host_login` | `Numeric` | `1` if the login belongs to the host list |
//! | `is_guest_login` | `Numeric` | `1` if the login is a guest login |
//! | `count` | `Numeric` | connections to the same host in the past two seconds |
//! | `srv_count` | `Numeric` | connections to the same service in the past two seconds |
//! | `serror_rate` | `Numeric` | fraction of those connections with a SYN error |
//! | `srv_serror_rate` | `Numeric` | fraction of the same-service connections with a SYN error |
//! | `rerror_rate` | `Numeric` | fraction of those connections with a REJ error |
//! | `srv_rerror_rate` | `Numeric` | fraction of the same-service connections with a REJ error |
//! | `same_srv_rate` | `Numeric` | fraction of those connections to the same service |
//! | `diff_srv_rate` | `Numeric` | fraction of those connections to a different service |
//! | `srv_diff_host_rate` | `Numeric` | fraction of the same-service connections to a different host |
//! | `dst_host_count` | `Numeric` | connections to the same destination host |
//! | `dst_host_srv_count` | `Numeric` | connections to the same destination host and service |
//! | `dst_host_same_srv_rate` | `Numeric` | fraction of the destination-host connections to the same service |
//! | `dst_host_diff_srv_rate` | `Numeric` | fraction of the destination-host connections to a different service |
//! | `dst_host_same_src_port_rate` | `Numeric` | fraction of the destination-host connections from the same source port |
//! | `dst_host_srv_diff_host_rate` | `Numeric` | fraction of the same-service connections to a different host |
//! | `dst_host_serror_rate` | `Numeric` | fraction of the destination-host connections with a SYN error |
//! | `dst_host_srv_serror_rate` | `Numeric` | fraction of the same-service connections with a SYN error |
//! | `dst_host_rerror_rate` | `Numeric` | fraction of the destination-host connections with a REJ error |
//! | `dst_host_srv_rerror_rate` | `Numeric` | fraction of the same-service connections with a REJ error |
//! | `label` | `String` | connection class, with the trailing period, such as `normal.` |
//!
//! The source designates the 41 connection features as the inputs
//! ([`Kddcup99::FEATURE_NAMES`](crate::Kddcup99::FEATURE_NAMES)) and `label` as the label ([`Kddcup99::TARGET`](crate::Kddcup99::TARGET)).
//!
//! **Samples:** 494,021 (10% subset, default) or 4,898,431 (full set)
//! **Application:** Multi-class classification / network-intrusion detection
//!
//! **Missing values:** none.
//!
//! **Source:** UCI KDD Archive, via the gzip-compressed mirrors that
//! scikit-learn's `fetch_kddcup99` downloads (`kddcup.data_10_percent.gz` for the
//! subset, `kddcup.data.gz` for the full set).
//! <https://kdd.ics.uci.edu/databases/kddcup99/kddcup99.html>
//!
//! **Note on size:** the full set is large. Its decompressed source file is about
//! 743 MB. The parsed table takes several gigabytes of memory. Loading it with
//! [`Kddcup99::new_full`](crate::Kddcup99::new_full) takes noticeable time and
//! memory. The default 10% subset is about 10 times smaller.
use crateDOWNLOAD_RETRIES;
use crate;
use crateimpl_ml_dataset;
use ReaderBuilder;
use ;
use Array1;
use File;
/// Which KDD Cup 1999 partition to load.
///
/// This mirrors scikit-learn's `fetch_kddcup99(percent10=…)` switch: the default
/// is the 10% subset ([`Kddcup99::new`]), and the full set is opt-in
/// ([`Kddcup99::new_full`]). The two variants are distinct upstream files, each
/// with its own URL, sample count, and pinned SHA-256. The loader caches each
/// under a distinct filename, so both can exist in the same storage directory.
/// The name of the dataset.
const KDDCUP99_DATASET_NAME: &str = "kddcup99";
/// The number of categorical columns (`protocol_type`, `service`, `flag`).
const N_STRING_FEATURES: usize = 3;
/// The number of numeric columns.
const N_NUMERIC_FEATURES: usize = 38;
/// The number of columns per CSV record (41 features + 1 label).
const N_COLUMNS: usize = N_STRING_FEATURES + N_NUMERIC_FEATURES + 1;
/// The categorical columns, as `(source column index, name)`.
const STRING_COLUMNS: =
;
/// The number of feature columns (3 string features and 38 numeric features).
const N_FEATURES: usize = N_STRING_FEATURES + N_NUMERIC_FEATURES;
/// The 0-based source-column index of the label.
const LABEL_COLUMN: usize = 41;
/// The numeric columns, as `(source column index, name)`. These are every column
/// except the three categorical ones (indices 1, 2, 3) and the label (index 41).
const NUMERIC_COLUMNS: = ;
/// A struct that represents the KDD Cup 1999 dataset with lazy loading.
///
/// The dataset loads only when you call a data accessor method. After the first
/// load, the dataset caches the data for later accesses.
///
/// Construct it with [`Kddcup99::new`] for scikit-learn's default 10% subset
/// (494,021 samples), or with [`Kddcup99::new_full`] for the full set
/// (4,898,431 samples). Both share the columns below and differ only in sample
/// count.
///
/// # About Dataset
///
/// The KDD Cup 1999 dataset was built from the DARPA 1998 intrusion-detection
/// evaluation. Each sample is a network connection summarized by 41 features.
/// The source labels each sample `normal.` or as one of 22 attack types. The
/// attack types fall into four categories: DoS, R2L, U2R, and probing. This is
/// the same data scikit-learn exposes through `fetch_kddcup99`.
///
/// # Columns
///
/// | Name | Type | Description |
/// |------|------|-------------|
/// | `duration` | `Numeric` | length of the connection in seconds |
/// | `protocol_type` | `String` | `tcp`, `udp`, or `icmp` |
/// | `service` | `String` | destination service, one of about 70 values, such as `http` |
/// | `flag` | `String` | connection status flag, one of 11 values, such as `SF` |
/// | `src_bytes` | `Numeric` | bytes sent from the source to the destination |
/// | `dst_bytes` | `Numeric` | bytes sent from the destination to the source |
/// | `land` | `Numeric` | `1` if the source and destination address and port are equal |
/// | `wrong_fragment` | `Numeric` | number of wrong fragments |
/// | `urgent` | `Numeric` | number of urgent packets |
/// | `hot` | `Numeric` | number of hot indicators |
/// | `num_failed_logins` | `Numeric` | number of failed login attempts |
/// | `logged_in` | `Numeric` | `1` if the login succeeded |
/// | `num_compromised` | `Numeric` | number of compromised conditions |
/// | `root_shell` | `Numeric` | `1` if the connection obtained a root shell |
/// | `su_attempted` | `Numeric` | `1` if the connection attempted the `su root` command |
/// | `num_root` | `Numeric` | number of root accesses |
/// | `num_file_creations` | `Numeric` | number of file creation operations |
/// | `num_shells` | `Numeric` | number of shell prompts |
/// | `num_access_files` | `Numeric` | number of operations on access control files |
/// | `num_outbound_cmds` | `Numeric` | number of outbound commands in an FTP session |
/// | `is_host_login` | `Numeric` | `1` if the login belongs to the host list |
/// | `is_guest_login` | `Numeric` | `1` if the login is a guest login |
/// | `count` | `Numeric` | connections to the same host in the past two seconds |
/// | `srv_count` | `Numeric` | connections to the same service in the past two seconds |
/// | `serror_rate` | `Numeric` | fraction of those connections with a SYN error |
/// | `srv_serror_rate` | `Numeric` | fraction of the same-service connections with a SYN error |
/// | `rerror_rate` | `Numeric` | fraction of those connections with a REJ error |
/// | `srv_rerror_rate` | `Numeric` | fraction of the same-service connections with a REJ error |
/// | `same_srv_rate` | `Numeric` | fraction of those connections to the same service |
/// | `diff_srv_rate` | `Numeric` | fraction of those connections to a different service |
/// | `srv_diff_host_rate` | `Numeric` | fraction of the same-service connections to a different host |
/// | `dst_host_count` | `Numeric` | connections to the same destination host |
/// | `dst_host_srv_count` | `Numeric` | connections to the same destination host and service |
/// | `dst_host_same_srv_rate` | `Numeric` | fraction of the destination-host connections to the same service |
/// | `dst_host_diff_srv_rate` | `Numeric` | fraction of the destination-host connections to a different service |
/// | `dst_host_same_src_port_rate` | `Numeric` | fraction of the destination-host connections from the same source port |
/// | `dst_host_srv_diff_host_rate` | `Numeric` | fraction of the same-service connections to a different host |
/// | `dst_host_serror_rate` | `Numeric` | fraction of the destination-host connections with a SYN error |
/// | `dst_host_srv_serror_rate` | `Numeric` | fraction of the same-service connections with a SYN error |
/// | `dst_host_rerror_rate` | `Numeric` | fraction of the destination-host connections with a REJ error |
/// | `dst_host_srv_rerror_rate` | `Numeric` | fraction of the same-service connections with a REJ error |
/// | `label` | `String` | connection class, with the trailing period, such as `normal.` |
///
/// The source designates the 41 connection features as the inputs
/// ([`Kddcup99::FEATURE_NAMES`]) and `label` as the label ([`Kddcup99::TARGET`]).
///
/// The columns keep the source column order. The `label` column holds the class
/// exactly as the source distributes it, **including the trailing period** (for
/// example `"normal."`, `"smurf."`, `"neptune."`). This matches scikit-learn's
/// `fetch_kddcup99` target. There are 23 distinct values (`normal.` plus 22
/// attack types).
///
/// Missing values: none.
///
/// See more information at
/// <https://kdd.ics.uci.edu/databases/kddcup99/kddcup99.html>
///
/// # Citation
///
/// "KDD Cup 1999 Data," UCI KDD Archive, 1999. \[Online\].
/// Available: <https://kdd.ics.uci.edu/databases/kddcup99/kddcup99.html>
///
/// # Thread Safety
///
/// This struct implements `Send` and `Sync` automatically, because all fields
/// implement them. This makes the struct safe to share across threads. The
/// internal [`Dataset`] makes lazy initialization thread-safe.
///
/// # Example
/// ```no_run
/// use dataset_ml::Kddcup99;
///
/// let download_dir = "./kddcup99"; // the loader creates the directory if it does not exist
///
/// // `new` loads the 10% subset (494,021 samples). Use `new_full` for the
/// // full 4,898,431-sample set with the same columns.
/// let mut dataset = Kddcup99::new(download_dir);
/// let table = dataset.data().unwrap();
///
/// assert_eq!(table.n_samples(), 494021);
/// assert_eq!(table.n_columns(), 42);
///
/// // The 41 features mix types. Read a string feature by name.
/// let protocol_type = table.column("protocol_type").unwrap().as_string().unwrap();
/// assert_eq!(protocol_type.len(), 494021);
///
/// // Reach one column by name.
/// let duration = table.column("duration").unwrap().as_numeric().unwrap();
/// assert_eq!(duration.len(), 494021);
/// let label = table.column(Kddcup99::TARGET).unwrap().as_string().unwrap();
/// assert_eq!(label.len(), 494021);
///
/// // `get_data_mut()` edits the table in place. This needs no clone and no
/// // reload. The change stays cached.
/// if let Some(table) = dataset.get_data_mut() {
/// if let Some(column) = table.column_mut("duration") {
/// if let dataset_ml::ColumnData::Numeric(values) = column.data_mut() {
/// values[0] = 0.0;
/// }
/// }
/// }
/// assert!(dataset.get_data().is_some());
///
/// // `take_data()` moves the owned table out with no clone. This leaves the
/// // instance reusable.
/// let owned = dataset.take_data().unwrap();
/// assert_eq!(owned.n_samples(), 494021);
///
/// // `into_data()` also returns the owned table with no clone, but it consumes
/// // the instance.
/// let owned = dataset.into_data().unwrap();
/// assert_eq!(owned.n_samples(), 494021);
/// ```
impl_ml_dataset!;