-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmulticlass_classification.py
More file actions
295 lines (244 loc) · 9.91 KB
/
multiclass_classification.py
File metadata and controls
295 lines (244 loc) · 9.91 KB
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
"""
Multi-class Classification Script for Intrusion Detection System
Trains a Random Forest classifier to identify specific attack types.
Attack Categories (10 classes):
1. Normal
2. Fuzzers
3. Analysis
4. Backdoors
5. DoS
6. Exploits
7. Generic
8. Reconnaissance
9. Shellcode
10. Worms
"""
import pandas as pd
import numpy as np
import joblib
import time
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import LabelEncoder, StandardScaler
from sklearn.metrics import (
accuracy_score, precision_score, recall_score, f1_score,
classification_report, confusion_matrix
)
import warnings
warnings.filterwarnings('ignore')
print("="*70)
print("MULTI-CLASS INTRUSION DETECTION SYSTEM")
print("="*70)
print()
# ============================================================================
# 1. LOAD ORIGINAL DATA
# ============================================================================
print("Step 1: Loading UNSW-NB15 dataset...")
print("-" * 70)
# Try to load the data from common locations
data_paths = [
r"C:\Users\User\Documents\Cybersecurity\IDS\IDS\Data\UNSW_NB15_training-set.csv",
"UNSW_NB15_training-set.csv",
"Data/UNSW_NB15_training-set.csv",
"../Data/UNSW_NB15_training-set.csv"
]
train_data = None
test_data = None
for path in data_paths:
try:
train_data = pd.read_csv(path)
test_path = path.replace("training-set", "testing-set")
test_data = pd.read_csv(test_path)
print(f"✓ Data loaded from: {path}")
break
except FileNotFoundError:
continue
if train_data is None:
print("✗ Error: Could not find UNSW-NB15 dataset files.")
print("\nPlease ensure the dataset files are in one of these locations:")
for path in data_paths:
print(f" - {path}")
print("\nOr update the path in this script.")
exit(1)
print(f"✓ Training data shape: {train_data.shape}")
print(f"✓ Test data shape: {test_data.shape}")
print()
# ============================================================================
# 2. DATA PREPROCESSING
# ============================================================================
print("Step 2: Preprocessing data for multi-class classification...")
print("-" * 70)
# Keep attack_cat column (don't drop it this time!)
# Drop only the id column
train_data = train_data.drop(columns=["id"])
test_data = test_data.drop(columns=["id"])
# Also drop the binary label column since we're using attack_cat
if 'label' in train_data.columns:
train_data = train_data.drop(columns=["label"])
test_data = test_data.drop(columns=["label"])
print(f"✓ Columns after dropping id and label: {train_data.shape[1]}")
# Check attack categories
attack_categories = train_data['attack_cat'].value_counts()
print(f"\nAttack categories found: {len(attack_categories)}")
for category, count in attack_categories.items():
print(f" {category:20s}: {count:6d}")
print()
# Identify categorical columns (excluding attack_cat which is our target)
categorical_cols = train_data.select_dtypes(include=['object']).columns.tolist()
categorical_cols.remove('attack_cat') # Remove target from features
print(f"Categorical feature columns: {len(categorical_cols)}")
print(f" {categorical_cols}")
print()
# Encode categorical features
print("Encoding categorical features...")
label_encoders = {}
for col in categorical_cols:
label_encoders[col] = LabelEncoder()
train_data[col] = label_encoders[col].fit_transform(train_data[col])
# Map test data using the same encoder, replacing unknown labels with -1
test_data[col] = test_data[col].apply(
lambda x: label_encoders[col].classes_.tolist().index(x)
if x in label_encoders[col].classes_ else -1
)
print("✓ Categorical features encoded")
# Encode target variable (attack_cat)
print("\nEncoding target variable (attack_cat)...")
attack_encoder = LabelEncoder()
train_data['attack_cat_encoded'] = attack_encoder.fit_transform(train_data['attack_cat'])
test_data['attack_cat_encoded'] = test_data['attack_cat'].apply(
lambda x: attack_encoder.classes_.tolist().index(x)
if x in attack_encoder.classes_ else -1
)
# Save the encoder for later use
joblib.dump(attack_encoder, 'attack_category_encoder.pkl')
print(f"✓ Target variable encoded into {len(attack_encoder.classes_)} classes")
print(f"✓ Class mapping saved to 'attack_category_encoder.pkl'")
print()
# Display class mapping
print("Class Mapping:")
for idx, category in enumerate(attack_encoder.classes_):
print(f" {idx}: {category}")
print()
# Normalize numerical features
print("Normalizing numerical features...")
numerical_cols = train_data.columns.difference(categorical_cols).difference(['attack_cat', 'attack_cat_encoded'])
scaler = StandardScaler()
train_data[numerical_cols] = scaler.fit_transform(train_data[numerical_cols])
test_data[numerical_cols] = scaler.transform(test_data[numerical_cols])
print(f"✓ {len(numerical_cols)} numerical features normalized")
print()
# Save preprocessed data
train_data.to_csv('train_multiclass_processed.csv', index=False)
test_data.to_csv('test_multiclass_processed.csv', index=False)
print("✓ Preprocessed data saved")
print()
# ============================================================================
# 3. PREPARE FEATURES AND LABELS
# ============================================================================
print("Step 3: Preparing features and labels...")
print("-" * 70)
X_train = train_data.drop(['attack_cat', 'attack_cat_encoded'], axis=1)
y_train = train_data['attack_cat_encoded']
X_test = test_data.drop(['attack_cat', 'attack_cat_encoded'], axis=1)
y_test = test_data['attack_cat_encoded']
print(f"✓ Training features: {X_train.shape}")
print(f"✓ Training labels: {y_train.shape}")
print(f"✓ Test features: {X_test.shape}")
print(f"✓ Test labels: {y_test.shape}")
print()
# Check class distribution
print("Class distribution in training set:")
class_dist = y_train.value_counts().sort_index()
for class_idx, count in class_dist.items():
class_name = attack_encoder.classes_[class_idx]
percentage = (count / len(y_train)) * 100
print(f" {class_idx} ({class_name:20s}): {count:6d} ({percentage:5.2f}%)")
print()
# ============================================================================
# 4. TRAIN MULTI-CLASS MODEL
# ============================================================================
print("Step 4: Training multi-class Random Forest classifier...")
print("-" * 70)
# Use class_weight='balanced' to handle class imbalance
print("Using class_weight='balanced' to handle class imbalance...")
print("Training model (this may take several minutes)...")
print()
start_time = time.time()
model = RandomForestClassifier(
n_estimators=200,
max_depth=30,
min_samples_split=2,
min_samples_leaf=1,
max_features='sqrt',
class_weight='balanced', # Handle class imbalance
random_state=42,
n_jobs=-1,
verbose=1
)
model.fit(X_train, y_train)
elapsed_time = time.time() - start_time
print(f"\n✓ Model trained in {elapsed_time/60:.2f} minutes")
print()
# Save the model
joblib.dump(model, 'intrusion_detection_model_multiclass.pkl')
print("✓ Model saved as 'intrusion_detection_model_multiclass.pkl'")
print()
# ============================================================================
# 5. EVALUATE MODEL
# ============================================================================
print("Step 5: Evaluating multi-class model...")
print("-" * 70)
# Make predictions
y_pred = model.predict(X_test)
# Overall accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f"\nOverall Accuracy: {accuracy:.4f} ({accuracy*100:.2f}%)")
print()
# Per-class metrics
print("Per-Class Metrics:")
print("=" * 70)
print(f"{'Class':<20s} {'Precision':>10s} {'Recall':>10s} {'F1-Score':>10s} {'Support':>10s}")
print("-" * 70)
# Calculate per-class metrics
for class_idx in range(len(attack_encoder.classes_)):
class_name = attack_encoder.classes_[class_idx]
# Binary classification for this class vs all others
y_test_binary = (y_test == class_idx).astype(int)
y_pred_binary = (y_pred == class_idx).astype(int)
if y_test_binary.sum() > 0: # Only if class exists in test set
precision = precision_score(y_test_binary, y_pred_binary, zero_division=0)
recall = recall_score(y_test_binary, y_pred_binary, zero_division=0)
f1 = f1_score(y_test_binary, y_pred_binary, zero_division=0)
support = y_test_binary.sum()
print(f"{class_name:<20s} {precision:>10.4f} {recall:>10.4f} {f1:>10.4f} {support:>10d}")
print()
# Detailed classification report
print("Detailed Classification Report:")
print("=" * 70)
print(classification_report(y_test, y_pred, target_names=attack_encoder.classes_, zero_division=0))
# Save classification report
report_dict = classification_report(y_test, y_pred, target_names=attack_encoder.classes_,
output_dict=True, zero_division=0)
report_df = pd.DataFrame(report_dict).transpose()
report_df.to_csv('multiclass_classification_report.csv')
print("✓ Classification report saved to 'multiclass_classification_report.csv'")
print()
# ============================================================================
# 6. SAVE RESULTS
# ============================================================================
print("Step 6: Saving results...")
print("-" * 70)
# Save confusion matrix
cm = confusion_matrix(y_test, y_pred)
cm_df = pd.DataFrame(cm, index=attack_encoder.classes_, columns=attack_encoder.classes_)
cm_df.to_csv('multiclass_confusion_matrix.csv')
print("✓ Confusion matrix saved to 'multiclass_confusion_matrix.csv'")
# Save feature names for later use
feature_names = X_train.columns.tolist()
pd.DataFrame({'feature': feature_names}).to_csv('multiclass_feature_names.csv', index=False)
print("✓ Feature names saved to 'multiclass_feature_names.csv'")
print()
print("="*70)
print("MULTI-CLASS CLASSIFICATION COMPLETE!")
print("="*70)
print()
print("Next: Run 'python multiclass_visualizations.py' to generate visualizations")