文章目录
- 数据集介绍
- Python环境
- 随机森林训练结果
- SVM支持向量机训练结果
- DNN训练结果
- 所有代码下载
数据集介绍
下载:https://www.kaggle.com/datasets/tarundhamor/cicids-2019-dataset
数据个数:
# 删除label中是WebDDoS的数据
df = df[df['Label'] != 'WebDDoS']
Python环境
pandas
scipy
scikit-learn==1.3
torch
matplotlib
seaborn
numpy
随机森林训练结果
特征重要程度:
Max Packet Length: 0.0684
Packet Length Mean: 0.0637
Avg Fwd Segment Size: 0.0579
Fwd Packet Length Max: 0.0569
Average Packet Size: 0.0569
Subflow Fwd Bytes: 0.0522
Fwd Packet Length Min: 0.0505
Fwd Packet Length Mean: 0.0501
Min Packet Length: 0.0474
Total Length of Fwd Packets: 0.0401
act_data_pkt_fwd: 0.0301
ACK Flag Count: 0.0300
Flow Bytes/s: 0.0263
Init_Win_bytes_forward: 0.0232
Inbound: 0.0224
Flow IAT Max: 0.0210
Flow IAT Std: 0.0193
Flow IAT Min: 0.0182
Fwd IAT Max: 0.0169
Flow IAT Mean: 0.0162
Total Fwd Packets: 0.0162
Protocol: 0.0161
Flow Duration: 0.0156
Fwd IAT Min: 0.0155
Fwd IAT Total: 0.0146
Fwd IAT Std: 0.0144
Subflow Fwd Packets: 0.0125
Fwd IAT Mean: 0.0121
Fwd Packets/s: 0.0120
Flow Packets/s: 0.0115
URG Flag Count: 0.0086
Bwd Packets/s: 0.0083
Packet Length Variance: 0.0076
min_seg_size_forward: 0.0067
Init_Win_bytes_backward: 0.0058
Bwd Header Length: 0.0052
Total Backward Packets: 0.0044
Packet Length Std: 0.0044
Bwd IAT Total: 0.0036
Subflow Bwd Bytes: 0.0034
Down/Up Ratio: 0.0028
Bwd IAT Max: 0.0026
Avg Bwd Segment Size: 0.0023
Bwd IAT Mean: 0.0021
Bwd Packet Length Max: 0.0021
Fwd Packet Length Std: 0.0019
Total Length of Bwd Packets: 0.0019
Bwd Packet Length Mean: 0.0018
Fwd Header Length.1: 0.0017
CWE Flag Count: 0.0017
Fwd Header Length: 0.0017
Subflow Bwd Packets: 0.0016
Bwd IAT Min: 0.0015
Idle Std: 0.0012
Bwd Packet Length Min: 0.0011
Idle Max: 0.0011
Active Min: 0.0008
Bwd IAT Std: 0.0008
Active Mean: 0.0006
Idle Mean: 0.0006
Idle Min: 0.0004
Bwd Packet Length Std: 0.0004
Fwd PSH Flags: 0.0003
RST Flag Count: 0.0003
Active Max: 0.0001
Active Std: 0.0001
SYN Flag Count: 0.0001
Bwd PSH Flags: 0.0000
Fwd URG Flags: 0.0000
Bwd URG Flags: 0.0000
FIN Flag Count: 0.0000
PSH Flag Count: 0.0000
ECE Flag Count: 0.0000
Fwd Avg Bytes/Bulk: 0.0000
Fwd Avg Packets/Bulk: 0.0000
Fwd Avg Bulk Rate: 0.0000
Bwd Avg Bytes/Bulk: 0.0000
Bwd Avg Packets/Bulk: 0.0000
Bwd Avg Bulk Rate: 0.0000
SimillarHTTP: 0.0000
{'BENIGN': {'precision': 0.9965305156915313, 'recall': 0.9992093611638204, 'f1-score': 0.9978681405448085, 'support': 6324.0}, 'DrDoS_DNS': {'precision': 0.8426485397784491, 'recall': 0.52296875, 'f1-score': 0.6453914384882375, 'support': 6400.0}, 'DrDoS_LDAP': {'precision': 0.4552080170057698, 'recall': 0.70265625, 'f1-score': 0.5524909392468825, 'support': 6400.0}, 'DrDoS_MSSQL': {'precision': 0.43775933609958506, 'recall': 0.0659375, 'f1-score': 0.11461162411732753, 'support': 6400.0}, 'DrDoS_NTP': {'precision': 0.9717636167258139, 'recall': 0.9840625, 'f1-score': 0.9778743886344227, 'support': 6400.0}, 'DrDoS_NetBIOS': {'precision': 0.8055891441623001, 'recall': 0.936875, 'f1-score': 0.8662862096366395, 'support': 6400.0}, 'DrDoS_SNMP': {'precision': 0.9993749023284888, 'recall': 0.99921875, 'f1-score': 0.9992968200640675, 'support': 6400.0}, 'DrDoS_SSDP': {'precision': 0.6196612283071807, 'recall': 0.93171875, 'f1-score': 0.744305061474131, 'support': 6400.0}, 'Syn': {'precision': 0.9995313964386129, 'recall': 0.99984375, 'f1-score': 0.9996875488204968, 'support': 6400.0}, 'UDP-lag': {'precision': 0.6013483146067415, 'recall': 0.418125, 'f1-score': 0.4932718894009216, 'support': 6400.0},
'lDos': {'precision': 0.5524492234169653, 'recall': 0.7225, 'f1-score': 0.6261340555179418, 'support': 6400.0}, 'accuracy': 0.7527444400204767, 'macro avg': {'precision': 0.7528967485964944, 'recall': 0.7530105101058019, 'f1-score': 0.7288380105405342, 'support': 70324.0}, 'weighted avg': {'precision': 0.7526334506285287, 'recall': 0.7527444400204767, 'f1-score': 0.7285472664150532, 'support': 70324.0}}
整体准确率: 0.75
平均准确率: 0.75, 平均召回率: 0.75
Process finished with exit code 0
SVM支持向量机训练结果
{'BENIGN': {'precision': 0.9767441860465116, 'recall': 0.6402439024390244, 'f1-score': 0.7734806629834254, 'support': 656.0}, 'DrDoS_DNS': {'precision': 0.7, 'recall': 0.010869565217391304, 'f1-score': 0.021406727828746176, 'support': 644.0}, 'DrDoS_LDAP': {'precision': 0.31919406150583246, 'recall': 0.9525316455696202, 'f1-score': 0.4781572676727562, 'support': 632.0}, 'DrDoS_MSSQL': {'precision': 0.0, 'recall': 0.0, 'f1-score': 0.0, 'support': 652.0}, 'DrDoS_NTP': {'precision': 0.688212927756654, 'recall': 0.8418604651162791, 'f1-score': 0.7573221757322176, 'support': 645.0}, 'DrDoS_NetBIOS': {'precision': 0.7403100775193798, 'recall': 0.8995290423861853, 'f1-score': 0.81218993621545, 'support': 637.0}, 'DrDoS_SNMP': {'precision': 0.9861111111111112, 'recall': 1.0, 'f1-score': 0.993006993006993, 'support': 639.0}, 'DrDoS_SSDP': {'precision': 0.6170212765957447, 'recall': 0.23311897106109325, 'f1-score': 0.338389731621937, 'support': 622.0}, 'Syn': {'precision': 0.6777301927194861, 'recall': 1.0, 'f1-score': 0.8079132099553287, 'support': 633.0}, 'UDP-lag': {'precision': 0.4148148148148148, 'recall': 0.17582417582417584, 'f1-score': 0.24696802646086002, 'support': 637.0},
'lDos': {'precision': 0.4900662251655629, 'recall': 0.8144654088050315, 'f1-score': 0.61193148257531, 'support': 636.0}, 'accuracy': 0.5960472060287217, 'macro avg': {'precision': 0.6009277157486452, 'recall': 0.5971311978562547, 'f1-score': 0.5309787467320931, 'support': 7033.0}, 'weighted avg': {'precision': 0.6011068239694879, 'recall': 0.5960472060287217, 'f1-score': 0.5306623678089073, 'support': 7033.0}}
整体准确率: 0.60
平均准确率: 0.60, 平均召回率: 0.60
==============================
svm_classification
Macro-average Precision: 0.6014285714285714
Macro-average Recall: 0.5971428571428571
Process finished with exit code 0
DNN训练结果
C:\ProgramData\miniconda3\envs\dlib_align\python.exe C:\Users\Administrator\PycharmProjects\pythonProject3\cicids2019\x07_0DNN训练.py
训练数据的总数据形状 torch.Size([281292, 56])
训练数据的标签形状 torch.Size([281292, 11])
测试数据的总数据形状 torch.Size([70324, 56])
测试数据的标签形状 torch.Size([70324, 11])
Training Progress: 3%|▎ | 1/30 [00:23<11:09, 23.10s/it]Epoch 1/30, Loss: 0.7203, Accuracy: 0.70
Training Progress: 7%|▋ | 2/30 [00:45<10:29, 22.48s/it]Epoch 2/30, Loss: 0.6408, Accuracy: 0.70
Epoch 3/30, Loss: 0.6217, Accuracy: 0.71
Training Progress: 13%|█▎ | 4/30 [01:29<09:36, 22.18s/it]Epoch 4/30, Loss: 0.6075, Accuracy: 0.72
Epoch 5/30, Loss: 0.5966, Accuracy: 0.72
Training Progress: 20%|██ | 6/30 [02:13<08:49, 22.05s/it]Epoch 6/30, Loss: 0.5975, Accuracy: 0.72
Training Progress: 23%|██▎ | 7/30 [02:34<08:25, 22.00s/it]Epoch 7/30, Loss: 0.5947, Accuracy: 0.72
Epoch 8/30, Loss: 0.6020, Accuracy: 0.72
Training Progress: 30%|███ | 9/30 [03:18<07:41, 21.99s/it]Epoch 9/30, Loss: 0.6100, Accuracy: 0.72
Training Progress: 33%|███▎ | 10/30 [03:40<07:18, 21.90s/it]Epoch 10/30, Loss: 0.6152, Accuracy: 0.72
Training Progress: 37%|███▋ | 11/30 [04:03<06:59, 22.10s/it]Epoch 11/30, Loss: 0.6161, Accuracy: 0.71
Epoch 12/30, Loss: 0.6089, Accuracy: 0.72
Training Progress: 43%|████▎ | 13/30 [04:45<06:06, 21.54s/it]Epoch 13/30, Loss: 0.6007, Accuracy: 0.71
Epoch 14/30, Loss: 0.5893, Accuracy: 0.72
Training Progress: 50%|█████ | 15/30 [05:27<05:20, 21.34s/it]Epoch 15/30, Loss: 0.5800, Accuracy: 0.73
Training Progress: 53%|█████▎ | 16/30 [05:48<04:59, 21.38s/it]Epoch 16/30, Loss: 0.5779, Accuracy: 0.73
Training Progress: 57%|█████▋ | 17/30 [06:09<04:34, 21.15s/it]Epoch 17/30, Loss: 0.5786, Accuracy: 0.72
Training Progress: 60%|██████ | 18/30 [06:30<04:12, 21.02s/it]Epoch 18/30, Loss: 0.5861, Accuracy: 0.72
Training Progress: 63%|██████▎ | 19/30 [06:50<03:50, 20.92s/it]Epoch 19/30, Loss: 0.5963, Accuracy: 0.72
Epoch 20/30, Loss: 0.6042, Accuracy: 0.71
Training Progress: 70%|███████ | 21/30 [07:30<03:03, 20.43s/it]Epoch 21/30, Loss: 0.6054, Accuracy: 0.71
Epoch 22/30, Loss: 0.6027, Accuracy: 0.72
Training Progress: 77%|███████▋ | 23/30 [08:11<02:23, 20.44s/it]Epoch 23/30, Loss: 0.5938, Accuracy: 0.72
Training Progress: 80%|████████ | 24/30 [08:32<02:02, 20.42s/it]Epoch 24/30, Loss: 0.5836, Accuracy: 0.72
Epoch 25/30, Loss: 0.5760, Accuracy: 0.73
Training Progress: 87%|████████▋ | 26/30 [09:12<01:21, 20.39s/it]Epoch 26/30, Loss: 0.5753, Accuracy: 0.73
Epoch 27/30, Loss: 0.5745, Accuracy: 0.73
Training Progress: 93%|█████████▎| 28/30 [09:53<00:40, 20.43s/it]Epoch 28/30, Loss: 0.5813, Accuracy: 0.73
Epoch 29/30, Loss: 0.5922, Accuracy: 0.70
Training Progress: 100%|██████████| 30/30 [10:34<00:00, 21.15s/it]
Epoch 30/30, Loss: 0.5993, Accuracy: 0.71
{'0': {'precision': 0.988443881589362, 'recall': 0.9873497786211258, 'f1-score': 0.987896527173483, 'support': 6324.0}, '1': {'precision': 0.6740442655935613, 'recall': 0.41875, 'f1-score': 0.5165767154973014, 'support': 6400.0}, '2': {'precision': 0.42294757665677546, 'recall': 0.668125, 'f1-score': 0.5179890975166566, 'support': 6400.0}, '3': {'precision': 0.4377224199288256, 'recall': 0.0384375, 'f1-score': 0.07066934788853778, 'support': 6400.0}, '4': {'precision': 0.9512785072563925, 'recall': 0.8603125, 'f1-score': 0.9035116508040696, 'support': 6400.0}, '5': {'precision': 0.760898282694848, 'recall': 0.9, 'f1-score': 0.8246241947029349, 'support': 6400.0}, '6': {'precision': 0.9897706137631742, 'recall': 0.9978125, 'f1-score': 0.9937752878929349, 'support': 6400.0}, '7': {'precision': 0.5691368959748057, 'recall': 0.90359375, 'f1-score': 0.6983877785157901, 'support': 6400.0}, '8': {'precision': 0.9998436522826767, 'recall': 0.99921875, 'f1-score': 0.9995311034698343, 'support': 6400.0}, '9': {'precision': 0.5366459627329192, 'recall': 0.3375, 'f1-score': 0.41438848920863314, 'support': 6400.0},
'10': {'precision': 0.5114308018289283, 'recall': 0.7165625, 'f1-score': 0.5968634086028504, 'support': 6400.0},
'accuracy': 0.711307661680223, 'macro avg': {'precision': 0.7129238963911154, 'recall': 0.7116056616928296, 'f1-score': 0.6840194182975478, 'support': 70324.0}, 'weighted avg': {'precision': 0.7126261386003886, 'recall': 0.711307661680223, 'f1-score': 0.6836910146192221, 'support': 70324.0}}
整体准确率: 0.71
==============================
dnn_classification
Macro-average Precision: 0.7185714285714287
Macro-average Recall: 0.7121428571428573
Process finished with exit code 0
混淆矩阵
所有代码下载
https://docs.qq.com/sheet/DUEdqZ2lmbmR6UVdU?tab=BB08J2