Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,20 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
def train_models(input_data, outliers_fraction, n_samples, clf_name):
|
| 3 |
"""Train anomaly detection models and plot results."""
|
| 4 |
n_outliers = int(outliers_fraction * n_samples)
|
|
@@ -38,7 +54,7 @@ def train_models(input_data, outliers_fraction, n_samples, clf_name):
|
|
| 38 |
# Convert X to DataFrame if using IsolationForest to ensure feature names
|
| 39 |
if clf_name == "Isolation Forest":
|
| 40 |
X = pd.DataFrame(X, columns=["Feature1", "Feature2"])
|
| 41 |
-
|
| 42 |
t0 = time.time()
|
| 43 |
clf.fit(X)
|
| 44 |
t1 = time.time()
|
|
@@ -62,3 +78,33 @@ def train_models(input_data, outliers_fraction, n_samples, clf_name):
|
|
| 62 |
plt.yticks(())
|
| 63 |
plt.title(f"{clf_name} (time: {t1 - t0:.2f}s)")
|
| 64 |
return plt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.covariance import EllipticEnvelope
|
| 6 |
+
from sklearn.neighbors import LocalOutlierFactor
|
| 7 |
+
from sklearn.linear_model import SGDOneClassSVM
|
| 8 |
+
from sklearn.pipeline import make_pipeline
|
| 9 |
+
from sklearn.kernel_approximation import Nystroem
|
| 10 |
+
from sklearn import svm
|
| 11 |
+
import matplotlib.pyplot as plt
|
| 12 |
+
import gradio as gr
|
| 13 |
+
import time
|
| 14 |
+
from functools import partial
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
# Function to train and visualize anomaly detection models
|
| 18 |
def train_models(input_data, outliers_fraction, n_samples, clf_name):
|
| 19 |
"""Train anomaly detection models and plot results."""
|
| 20 |
n_outliers = int(outliers_fraction * n_samples)
|
|
|
|
| 54 |
# Convert X to DataFrame if using IsolationForest to ensure feature names
|
| 55 |
if clf_name == "Isolation Forest":
|
| 56 |
X = pd.DataFrame(X, columns=["Feature1", "Feature2"])
|
| 57 |
+
|
| 58 |
t0 = time.time()
|
| 59 |
clf.fit(X)
|
| 60 |
t1 = time.time()
|
|
|
|
| 78 |
plt.yticks(())
|
| 79 |
plt.title(f"{clf_name} (time: {t1 - t0:.2f}s)")
|
| 80 |
return plt
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
# Gradio interface setup
|
| 84 |
+
with gr.Blocks() as demo:
|
| 85 |
+
gr.Markdown("# Anomaly Detection Algorithms Comparison")
|
| 86 |
+
|
| 87 |
+
input_models = [
|
| 88 |
+
"Robust covariance", "One-Class SVM", "One-Class SVM (SGD)", "Isolation Forest", "Local Outlier Factor"
|
| 89 |
+
]
|
| 90 |
+
input_data = gr.Radio(
|
| 91 |
+
choices=["Central Blob", "Two Blobs", "Blob with Noise", "Moons", "Noise"],
|
| 92 |
+
value="Moons",
|
| 93 |
+
label="Dataset Type"
|
| 94 |
+
)
|
| 95 |
+
n_samples = gr.Slider(
|
| 96 |
+
minimum=100, maximum=500, step=25, value=300, label="Number of Samples"
|
| 97 |
+
)
|
| 98 |
+
outliers_fraction = gr.Slider(
|
| 99 |
+
minimum=0.1, maximum=0.9, step=0.1, value=0.2, label="Outlier Fraction"
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
for clf_name in input_models:
|
| 103 |
+
plot = gr.Plot(label=clf_name)
|
| 104 |
+
fn = partial(train_models, clf_name=clf_name)
|
| 105 |
+
input_data.change(fn=fn, inputs=[input_data, outliers_fraction, n_samples], outputs=plot)
|
| 106 |
+
n_samples.change(fn=fn, inputs=[input_data, outliers_fraction, n_samples], outputs=plot)
|
| 107 |
+
outliers_fraction.change(fn=fn, inputs=[input_data, outliers_fraction, n_samples], outputs=plot)
|
| 108 |
+
|
| 109 |
+
# Launch the app
|
| 110 |
+
demo.launch()
|