Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,109 +1,133 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
from sklearn.
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import seaborn as sns
|
| 6 |
+
from sklearn.preprocessing import StandardScaler
|
| 7 |
+
from sklearn.ensemble import IsolationForest
|
| 8 |
+
from sklearn.svm import OneClassSVM
|
| 9 |
+
from sklearn.linear_model import LogisticRegression
|
| 10 |
+
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix
|
| 11 |
+
|
| 12 |
+
# ----------------------------
|
| 13 |
+
# STREAMLIT APP TITLE
|
| 14 |
+
# ----------------------------
|
| 15 |
+
st.title("🧠 NSL-KDD Anomaly Detection")
|
| 16 |
+
st.markdown("""
|
| 17 |
+
Compare **Isolation Forest**, **One-Class SVM**, and **Logistic Regression**
|
| 18 |
+
for detecting network intrusions using the **NSL-KDD dataset**.
|
| 19 |
+
""")
|
| 20 |
+
|
| 21 |
+
# ----------------------------
|
| 22 |
+
# LOAD DATA
|
| 23 |
+
# ----------------------------
|
| 24 |
+
@st.cache_data
|
| 25 |
+
def load_data():
|
| 26 |
+
url = "https://raw.githubusercontent.com/defcom17/NSL_KDD/master/KDDTrain+.txt"
|
| 27 |
+
col_names = [
|
| 28 |
+
"duration","protocol_type","service","flag","src_bytes","dst_bytes","land","wrong_fragment","urgent",
|
| 29 |
+
"hot","num_failed_logins","logged_in","num_compromised","root_shell","su_attempted","num_root",
|
| 30 |
+
"num_file_creations","num_shells","num_access_files","num_outbound_cmds","is_host_login",
|
| 31 |
+
"is_guest_login","count","srv_count","serror_rate","srv_serror_rate","rerror_rate","srv_rerror_rate",
|
| 32 |
+
"same_srv_rate","diff_srv_rate","srv_diff_host_rate","dst_host_count","dst_host_srv_count",
|
| 33 |
+
"dst_host_same_srv_rate","dst_host_diff_srv_rate","dst_host_same_src_port_rate","dst_host_srv_diff_host_rate",
|
| 34 |
+
"dst_host_serror_rate","dst_host_srv_serror_rate","dst_host_rerror_rate","dst_host_srv_rerror_rate","label","difficulty"
|
| 35 |
+
]
|
| 36 |
+
df = pd.read_csv(url, names=col_names)
|
| 37 |
+
return df
|
| 38 |
+
|
| 39 |
+
df = load_data()
|
| 40 |
+
st.write("### Dataset Preview", df.head())
|
| 41 |
+
|
| 42 |
+
# ----------------------------
|
| 43 |
+
# DATA PREPROCESSING
|
| 44 |
+
# ----------------------------
|
| 45 |
+
# Drop unnecessary column
|
| 46 |
+
df = df.drop(columns=['difficulty'])
|
| 47 |
+
|
| 48 |
+
# One-hot encode categorical features
|
| 49 |
+
categorical_cols = ['protocol_type', 'service', 'flag']
|
| 50 |
+
df = pd.get_dummies(df, columns=categorical_cols)
|
| 51 |
+
|
| 52 |
+
# Encode labels (normal = 0, attack = 1)
|
| 53 |
+
df['label'] = df['label'].apply(lambda x: 0 if x == 'normal' else 1)
|
| 54 |
+
|
| 55 |
+
# Split features and labels
|
| 56 |
+
X = df.drop(columns=['label'])
|
| 57 |
+
y = df['label']
|
| 58 |
+
|
| 59 |
+
# Scale numeric features
|
| 60 |
+
scaler = StandardScaler()
|
| 61 |
+
X_scaled = scaler.fit_transform(X)
|
| 62 |
+
|
| 63 |
+
st.write("✅ Data successfully preprocessed and scaled.")
|
| 64 |
+
st.write("Feature shape:", X_scaled.shape)
|
| 65 |
+
|
| 66 |
+
# ----------------------------
|
| 67 |
+
# MODEL TRAINING
|
| 68 |
+
# ----------------------------
|
| 69 |
+
st.subheader("⚙️ Model Training and Evaluation")
|
| 70 |
+
|
| 71 |
+
models = {
|
| 72 |
+
"Isolation Forest": IsolationForest(contamination=0.1, random_state=42),
|
| 73 |
+
"One-Class SVM": OneClassSVM(nu=0.1, kernel='rbf', gamma='scale'),
|
| 74 |
+
"Logistic Regression": LogisticRegression(max_iter=1000)
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
results = {}
|
| 78 |
+
|
| 79 |
+
for name, model in models.items():
|
| 80 |
+
if name in ["Isolation Forest", "One-Class SVM"]:
|
| 81 |
+
model.fit(X_scaled)
|
| 82 |
+
preds = model.predict(X_scaled)
|
| 83 |
+
# Convert -1 (anomaly) to 1 (attack), +1 to 0 (normal)
|
| 84 |
+
preds = np.where(preds == -1, 1, 0)
|
| 85 |
+
else:
|
| 86 |
+
model.fit(X_scaled, y)
|
| 87 |
+
preds = model.predict(X_scaled)
|
| 88 |
+
|
| 89 |
+
acc = accuracy_score(y, preds)
|
| 90 |
+
prec = precision_score(y, preds)
|
| 91 |
+
rec = recall_score(y, preds)
|
| 92 |
+
f1 = f1_score(y, preds)
|
| 93 |
+
results[name] = [acc, prec, rec, f1]
|
| 94 |
+
|
| 95 |
+
# ----------------------------
|
| 96 |
+
# DISPLAY RESULTS
|
| 97 |
+
# ----------------------------
|
| 98 |
+
st.write("### 📊 Model Performance Comparison")
|
| 99 |
+
|
| 100 |
+
results_df = pd.DataFrame(results, index=['Accuracy', 'Precision', 'Recall', 'F1-Score']).T
|
| 101 |
+
st.dataframe(results_df.style.highlight_max(axis=0, color='lightgreen'))
|
| 102 |
+
|
| 103 |
+
# ----------------------------
|
| 104 |
+
# CONFUSION MATRICES
|
| 105 |
+
# ----------------------------
|
| 106 |
+
st.write("### 🔍 Confusion Matrices")
|
| 107 |
+
|
| 108 |
+
fig, axes = plt.subplots(1, 3, figsize=(18, 5))
|
| 109 |
+
for ax, (name, model) in zip(axes, models.items()):
|
| 110 |
+
if name in ["Isolation Forest", "One-Class SVM"]:
|
| 111 |
+
model.fit(X_scaled)
|
| 112 |
+
preds = model.predict(X_scaled)
|
| 113 |
+
preds = np.where(preds == -1, 1, 0)
|
| 114 |
+
else:
|
| 115 |
+
preds = model.predict(X_scaled)
|
| 116 |
+
cm = confusion_matrix(y, preds)
|
| 117 |
+
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', ax=ax)
|
| 118 |
+
ax.set_title(name)
|
| 119 |
+
ax.set_xlabel('Predicted')
|
| 120 |
+
ax.set_ylabel('Actual')
|
| 121 |
+
st.pyplot(fig)
|
| 122 |
+
|
| 123 |
+
# ----------------------------
|
| 124 |
+
# CONCLUSION
|
| 125 |
+
# ----------------------------
|
| 126 |
+
st.markdown("""
|
| 127 |
+
### 🧾 Summary
|
| 128 |
+
- **Isolation Forest** and **One-Class SVM** are *unsupervised* models — useful when labels are unknown.
|
| 129 |
+
- **Logistic Regression** is *supervised* — it learns from labeled data.
|
| 130 |
+
- Typically, Isolation Forest performs better for anomaly detection on high-dimensional data.
|
| 131 |
+
|
| 132 |
+
📘 Dataset Source: [NSL-KDD Dataset](https://www.unb.ca/cic/datasets/nsl.html)
|
| 133 |
+
""")
|