Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,12 +9,8 @@ from pyod.models.combination import aom, moa, average
|
|
9 |
from pyod.utils.utility import standardizer
|
10 |
from sklearn.decomposition import PCA
|
11 |
from sklearn.metrics import precision_score, recall_score
|
12 |
-
import base64
|
13 |
from datetime import datetime
|
14 |
|
15 |
-
# Configuration
|
16 |
-
st.set_option('deprecation.showPyplotGlobalUse', False)
|
17 |
-
|
18 |
def generate_report(data, predictions, model_names, metrics):
|
19 |
report = f"""
|
20 |
Network Anomaly Detection Report
|
@@ -37,11 +33,10 @@ def generate_report(data, predictions, model_names, metrics):
|
|
37 |
return report
|
38 |
|
39 |
def plot_3d_projections(data, predictions):
|
40 |
-
pca = PCA(n_components=3)
|
41 |
-
projections = pca.fit_transform(data)
|
42 |
-
|
43 |
fig = plt.figure(figsize=(10, 7))
|
44 |
ax = fig.add_subplot(111, projection='3d')
|
|
|
|
|
45 |
|
46 |
normal = projections[predictions == 0]
|
47 |
anomalies = projections[predictions == 1]
|
@@ -121,18 +116,22 @@ def main():
|
|
121 |
progress_bar.progress((i+1)/len(selected_models))
|
122 |
|
123 |
# Ensemble prediction
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
|
|
|
|
|
|
|
|
130 |
|
131 |
threshold = np.percentile(combined_scores, 100*(1-contamination))
|
132 |
predictions = (combined_scores > threshold).astype(int)
|
133 |
|
134 |
# Performance metrics
|
135 |
-
if uploaded_file is None:
|
136 |
y_true = np.zeros(n_samples)
|
137 |
y_true[anomaly_idx] = 1
|
138 |
precision = precision_score(y_true, predictions)
|
@@ -161,18 +160,18 @@ def main():
|
|
161 |
|
162 |
tab1, tab2 = st.tabs(["2D Projection", "3D Projection"])
|
163 |
with tab1:
|
|
|
164 |
pca = PCA(n_components=2)
|
165 |
viz_data = pca.fit_transform(X_norm)
|
166 |
-
|
167 |
-
plt.scatter(viz_data[predictions==0, 0], viz_data[predictions==0, 1],
|
168 |
c='blue', label='Normal', alpha=0.6)
|
169 |
-
|
170 |
c='red', marker='x', label='Anomaly')
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
st.pyplot()
|
176 |
|
177 |
with tab2:
|
178 |
st.pyplot(plot_3d_projections(X_norm, predictions))
|
|
|
9 |
from pyod.utils.utility import standardizer
|
10 |
from sklearn.decomposition import PCA
|
11 |
from sklearn.metrics import precision_score, recall_score
|
|
|
12 |
from datetime import datetime
|
13 |
|
|
|
|
|
|
|
14 |
def generate_report(data, predictions, model_names, metrics):
|
15 |
report = f"""
|
16 |
Network Anomaly Detection Report
|
|
|
33 |
return report
|
34 |
|
35 |
def plot_3d_projections(data, predictions):
|
|
|
|
|
|
|
36 |
fig = plt.figure(figsize=(10, 7))
|
37 |
ax = fig.add_subplot(111, projection='3d')
|
38 |
+
pca = PCA(n_components=3)
|
39 |
+
projections = pca.fit_transform(data)
|
40 |
|
41 |
normal = projections[predictions == 0]
|
42 |
anomalies = projections[predictions == 1]
|
|
|
116 |
progress_bar.progress((i+1)/len(selected_models))
|
117 |
|
118 |
# Ensemble prediction
|
119 |
+
try:
|
120 |
+
if ensemble_method == "Average":
|
121 |
+
combined_scores = average(train_scores)
|
122 |
+
elif ensemble_method == "MOA":
|
123 |
+
combined_scores = moa(train_scores)
|
124 |
+
else:
|
125 |
+
combined_scores = aom(train_scores)
|
126 |
+
except Exception as e:
|
127 |
+
st.error(f"Ensemble method failed: {str(e)}")
|
128 |
+
return
|
129 |
|
130 |
threshold = np.percentile(combined_scores, 100*(1-contamination))
|
131 |
predictions = (combined_scores > threshold).astype(int)
|
132 |
|
133 |
# Performance metrics
|
134 |
+
if uploaded_file is None:
|
135 |
y_true = np.zeros(n_samples)
|
136 |
y_true[anomaly_idx] = 1
|
137 |
precision = precision_score(y_true, predictions)
|
|
|
160 |
|
161 |
tab1, tab2 = st.tabs(["2D Projection", "3D Projection"])
|
162 |
with tab1:
|
163 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
164 |
pca = PCA(n_components=2)
|
165 |
viz_data = pca.fit_transform(X_norm)
|
166 |
+
ax.scatter(viz_data[predictions==0, 0], viz_data[predictions==0, 1],
|
|
|
167 |
c='blue', label='Normal', alpha=0.6)
|
168 |
+
ax.scatter(viz_data[predictions==1, 0], viz_data[predictions==1, 1],
|
169 |
c='red', marker='x', label='Anomaly')
|
170 |
+
ax.set_xlabel("Principal Component 1")
|
171 |
+
ax.set_ylabel("Principal Component 2")
|
172 |
+
ax.set_title("PCA Projection of Network Data")
|
173 |
+
ax.legend()
|
174 |
+
st.pyplot(fig)
|
175 |
|
176 |
with tab2:
|
177 |
st.pyplot(plot_3d_projections(X_norm, predictions))
|