Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,16 @@
|
|
| 1 |
import numpy as np
|
| 2 |
import pandas as pd
|
| 3 |
-
from sklearn.datasets import make_classification
|
| 4 |
from sklearn.ensemble import IsolationForest
|
| 5 |
from sklearn.metrics import roc_curve, auc
|
| 6 |
-
import shap
|
| 7 |
import matplotlib.pyplot as plt
|
| 8 |
import gradio as gr
|
| 9 |
-
from sklearn import svm
|
| 10 |
from sklearn.covariance import EllipticEnvelope
|
| 11 |
from sklearn.neighbors import LocalOutlierFactor
|
| 12 |
from sklearn.linear_model import SGDOneClassSVM
|
| 13 |
-
from sklearn.kernel_approximation import Nystroem
|
| 14 |
from sklearn.pipeline import make_pipeline
|
|
|
|
|
|
|
| 15 |
import time
|
| 16 |
from functools import partial
|
| 17 |
|
|
@@ -50,14 +49,6 @@ anomaly_labels = iso_forest.predict(df) # -1 for anomaly, 1 for normal
|
|
| 50 |
df["Anomaly_Score"] = anomaly_scores
|
| 51 |
df["Anomaly_Label"] = np.where(anomaly_labels == -1, "Anomaly", "Normal")
|
| 52 |
|
| 53 |
-
# Generate true labels (1 for anomaly, 0 for normal) for ROC curve
|
| 54 |
-
true_labels = np.where(df["Anomaly_Label"] == "Anomaly", 1, 0)
|
| 55 |
-
|
| 56 |
-
# SHAP Explainability
|
| 57 |
-
explainer = shap.Explainer(iso_forest, df[columns])
|
| 58 |
-
shap_values = explainer(df[columns])
|
| 59 |
-
|
| 60 |
-
|
| 61 |
# Functions for Anomaly Detection Algorithms tab
|
| 62 |
def train_models(input_data, outliers_fraction, n_samples, clf_name):
|
| 63 |
"""Train anomaly detection models and plot results."""
|
|
@@ -121,31 +112,28 @@ def train_models(input_data, outliers_fraction, n_samples, clf_name):
|
|
| 121 |
|
| 122 |
# Create Gradio interface
|
| 123 |
with gr.Blocks() as demo:
|
| 124 |
-
gr.Markdown("#
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
]
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
outliers_fraction.change(fn=fn, inputs=[input_data, outliers_fraction, n_samples], outputs=plot)
|
| 149 |
-
|
| 150 |
-
# Launch the Gradio app
|
| 151 |
demo.launch()
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
import pandas as pd
|
| 3 |
+
from sklearn.datasets import make_classification, make_blobs, make_moons
|
| 4 |
from sklearn.ensemble import IsolationForest
|
| 5 |
from sklearn.metrics import roc_curve, auc
|
|
|
|
| 6 |
import matplotlib.pyplot as plt
|
| 7 |
import gradio as gr
|
|
|
|
| 8 |
from sklearn.covariance import EllipticEnvelope
|
| 9 |
from sklearn.neighbors import LocalOutlierFactor
|
| 10 |
from sklearn.linear_model import SGDOneClassSVM
|
|
|
|
| 11 |
from sklearn.pipeline import make_pipeline
|
| 12 |
+
from sklearn.kernel_approximation import Nystroem
|
| 13 |
+
from sklearn import svm
|
| 14 |
import time
|
| 15 |
from functools import partial
|
| 16 |
|
|
|
|
| 49 |
df["Anomaly_Score"] = anomaly_scores
|
| 50 |
df["Anomaly_Label"] = np.where(anomaly_labels == -1, "Anomaly", "Normal")
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
# Functions for Anomaly Detection Algorithms tab
|
| 53 |
def train_models(input_data, outliers_fraction, n_samples, clf_name):
|
| 54 |
"""Train anomaly detection models and plot results."""
|
|
|
|
| 112 |
|
| 113 |
# Create Gradio interface
|
| 114 |
with gr.Blocks() as demo:
|
| 115 |
+
gr.Markdown("# Anomaly Detection Algorithms Comparison")
|
| 116 |
+
|
| 117 |
+
input_models = [
|
| 118 |
+
"Robust covariance", "One-Class SVM", "One-Class SVM (SGD)", "Isolation Forest", "Local Outlier Factor"
|
| 119 |
+
]
|
| 120 |
+
input_data = gr.Radio(
|
| 121 |
+
choices=["Central Blob", "Two Blobs", "Blob with Noise", "Moons", "Noise"],
|
| 122 |
+
value="Moons",
|
| 123 |
+
label="Dataset Type"
|
| 124 |
+
)
|
| 125 |
+
n_samples = gr.Slider(
|
| 126 |
+
minimum=100, maximum=500, step=25, value=300, label="Number of Samples"
|
| 127 |
+
)
|
| 128 |
+
outliers_fraction = gr.Slider(
|
| 129 |
+
minimum=0.1, maximum=0.9, step=0.1, value=0.2, label="Outlier Fraction"
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
for clf_name in input_models:
|
| 133 |
+
plot = gr.Plot(label=clf_name)
|
| 134 |
+
fn = partial(train_models, clf_name=clf_name)
|
| 135 |
+
input_data.change(fn=fn, inputs=[input_data, outliers_fraction, n_samples], outputs=plot)
|
| 136 |
+
n_samples.change(fn=fn, inputs=[input_data, outliers_fraction, n_samples], outputs=plot)
|
| 137 |
+
outliers_fraction.change(fn=fn, inputs=[input_data, outliers_fraction, n_samples], outputs=plot)
|
| 138 |
+
|
|
|
|
|
|
|
|
|
|
| 139 |
demo.launch()
|