Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -74,6 +74,28 @@ def train_models(input_data, outliers_fraction, n_samples, clf_name):
|
|
| 74 |
plt.yticks(())
|
| 75 |
return plt.gcf()
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
# Gradio Interface
|
| 78 |
description = "Compare how different anomaly detection algorithms perform on various datasets."
|
| 79 |
title = "🕵️♀️ Compare Anomaly Detection Algorithms 🕵️♂️"
|
|
@@ -88,8 +110,10 @@ with gr.Blocks() as demo:
|
|
| 88 |
value="Moons",
|
| 89 |
label="Dataset"
|
| 90 |
)
|
| 91 |
-
n_samples = gr.Slider(minimum=
|
| 92 |
-
outliers_fraction = gr.Slider(
|
|
|
|
|
|
|
| 93 |
|
| 94 |
# Models and their plots in a row
|
| 95 |
input_models = ["Robust covariance", "One-Class SVM", "One-Class SVM (SGD)", "Isolation Forest", "Local Outlier Factor"]
|
|
@@ -100,17 +124,24 @@ with gr.Blocks() as demo:
|
|
| 100 |
plot = gr.Plot(label=model_name)
|
| 101 |
plots.append((model_name, plot))
|
| 102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
# Update function
|
| 104 |
def update(input_data, outliers_fraction, n_samples):
|
| 105 |
-
|
| 106 |
for clf_name, plot in plots:
|
| 107 |
fig = train_models(input_data, outliers_fraction, n_samples, clf_name)
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
| 110 |
|
| 111 |
# Set change triggers
|
| 112 |
inputs = [input_data, outliers_fraction, n_samples]
|
| 113 |
-
demo_outputs = [plot for _, plot in plots]
|
| 114 |
input_data.change(fn=update, inputs=inputs, outputs=demo_outputs)
|
| 115 |
n_samples.change(fn=update, inputs=inputs, outputs=demo_outputs)
|
| 116 |
outliers_fraction.change(fn=update, inputs=inputs, outputs=demo_outputs)
|
|
|
|
| 74 |
plt.yticks(())
|
| 75 |
return plt.gcf()
|
| 76 |
|
| 77 |
+
# Function for Feature Scatter Plots
|
| 78 |
+
def plot_feature_scatter(input_data, n_samples):
|
| 79 |
+
data, _ = make_moons(n_samples=n_samples, noise=0.05) if input_data == "Moons" else make_blobs(n_samples=n_samples, random_state=0)
|
| 80 |
+
plt.figure(figsize=(5, 5))
|
| 81 |
+
plt.scatter(data[:, 0], data[:, 1], alpha=0.8, c="blue", s=20, label="Features")
|
| 82 |
+
plt.title(f"Feature Scatter Plot - {input_data}")
|
| 83 |
+
plt.legend()
|
| 84 |
+
return plt.gcf()
|
| 85 |
+
|
| 86 |
+
# Function for Anomaly Examples
|
| 87 |
+
def plot_anomaly_examples(input_data, n_samples, outliers_fraction):
|
| 88 |
+
n_outliers = int(outliers_fraction * n_samples)
|
| 89 |
+
rng = np.random.RandomState(42)
|
| 90 |
+
data, _ = make_blobs(n_samples=n_samples, random_state=0)
|
| 91 |
+
outliers = rng.uniform(low=-6, high=6, size=(n_outliers, 2))
|
| 92 |
+
plt.figure(figsize=(5, 5))
|
| 93 |
+
plt.scatter(data[:, 0], data[:, 1], alpha=0.8, c="blue", s=20, label="Inliers")
|
| 94 |
+
plt.scatter(outliers[:, 0], outliers[:, 1], alpha=0.8, c="red", s=20, label="Outliers")
|
| 95 |
+
plt.title("Anomaly Examples")
|
| 96 |
+
plt.legend()
|
| 97 |
+
return plt.gcf()
|
| 98 |
+
|
| 99 |
# Gradio Interface
|
| 100 |
description = "Compare how different anomaly detection algorithms perform on various datasets."
|
| 101 |
title = "🕵️♀️ Compare Anomaly Detection Algorithms 🕵️♂️"
|
|
|
|
| 110 |
value="Moons",
|
| 111 |
label="Dataset"
|
| 112 |
)
|
| 113 |
+
n_samples = gr.Slider(minimum=100, maximum=500, step=25, value=300, label="Number of Samples")
|
| 114 |
+
outliers_fraction = gr.Slider(
|
| 115 |
+
minimum=0.01, maximum=0.99, step=0.01, value=0.2, label="Fraction of Outliers"
|
| 116 |
+
)
|
| 117 |
|
| 118 |
# Models and their plots in a row
|
| 119 |
input_models = ["Robust covariance", "One-Class SVM", "One-Class SVM (SGD)", "Isolation Forest", "Local Outlier Factor"]
|
|
|
|
| 124 |
plot = gr.Plot(label=model_name)
|
| 125 |
plots.append((model_name, plot))
|
| 126 |
|
| 127 |
+
# Additional Rows for Scatter and Anomaly Examples
|
| 128 |
+
with gr.Row():
|
| 129 |
+
feature_plot = gr.Plot(label="Feature Scatter Plot")
|
| 130 |
+
anomaly_plot = gr.Plot(label="Anomaly Examples")
|
| 131 |
+
|
| 132 |
# Update function
|
| 133 |
def update(input_data, outliers_fraction, n_samples):
|
| 134 |
+
model_results = []
|
| 135 |
for clf_name, plot in plots:
|
| 136 |
fig = train_models(input_data, outliers_fraction, n_samples, clf_name)
|
| 137 |
+
model_results.append(fig)
|
| 138 |
+
feature_fig = plot_feature_scatter(input_data, n_samples)
|
| 139 |
+
anomaly_fig = plot_anomaly_examples(input_data, n_samples, outliers_fraction)
|
| 140 |
+
return model_results + [feature_fig, anomaly_fig]
|
| 141 |
|
| 142 |
# Set change triggers
|
| 143 |
inputs = [input_data, outliers_fraction, n_samples]
|
| 144 |
+
demo_outputs = [plot for _, plot in plots] + [feature_plot, anomaly_plot]
|
| 145 |
input_data.change(fn=update, inputs=inputs, outputs=demo_outputs)
|
| 146 |
n_samples.change(fn=update, inputs=inputs, outputs=demo_outputs)
|
| 147 |
outliers_fraction.change(fn=update, inputs=inputs, outputs=demo_outputs)
|