Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,6 +33,55 @@ def prepare_data(input_data, n_samples, outliers_fraction=0.0):
|
|
| 33 |
labels[-len(outliers):] = "Anomaly"
|
| 34 |
return X, labels
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
# Function to detect anomalies and generate anomaly records
|
| 37 |
def detect_anomalies(input_data, n_samples, outliers_fraction, model_name):
|
| 38 |
X, labels = prepare_data(input_data, n_samples, outliers_fraction)
|
|
@@ -99,79 +148,25 @@ def get_anomaly_samples(input_data, n_samples, outliers_fraction, model_name):
|
|
| 99 |
|
| 100 |
return top_10, middle_10, bottom_10
|
| 101 |
|
| 102 |
-
# Function to generate feature scatter plots
|
| 103 |
-
def plot_interactive_feature_scatter(input_data, feature_x, feature_y, n_samples):
|
| 104 |
-
data, _ = prepare_data(input_data, n_samples)
|
| 105 |
-
x_data = data[:, 0] if feature_x == "Feature1" else data[:, 1]
|
| 106 |
-
y_data = data[:, 1] if feature_y == "Feature2" else data[:, 0]
|
| 107 |
-
|
| 108 |
-
# Generate scatter plot
|
| 109 |
-
plt.figure(figsize=(6, 6))
|
| 110 |
-
plt.scatter(x_data, y_data, alpha=0.8, c="blue", s=20, label="Features")
|
| 111 |
-
plt.title(f"Feature Interaction Scatter Plot - {feature_x} vs {feature_y}")
|
| 112 |
-
plt.xlabel(feature_x)
|
| 113 |
-
plt.ylabel(feature_y)
|
| 114 |
-
plt.legend()
|
| 115 |
-
return plt.gcf()
|
| 116 |
-
|
| 117 |
# Gradio Interface
|
| 118 |
with gr.Blocks() as demo:
|
| 119 |
-
# App Title and Description
|
| 120 |
gr.Markdown("## 🕵️♀️ Anomaly Detection App 🕵️♂️")
|
| 121 |
-
gr.Markdown("Explore anomaly detection models, feature interactions, and anomaly examples.")
|
| 122 |
-
|
| 123 |
-
# Interactive Feature Scatter Plot
|
| 124 |
-
gr.Markdown("### 1. Interactive Feature Scatter Plot")
|
| 125 |
input_data = gr.Radio(
|
| 126 |
choices=["Central Blob", "Two Blobs", "Blob with Noise", "Moons", "Noise"],
|
| 127 |
value="Moons",
|
| 128 |
label="Dataset"
|
| 129 |
)
|
| 130 |
-
feature_x = gr.Dropdown(choices=["Feature1", "Feature2"], value="Feature1", label="Feature 1")
|
| 131 |
-
feature_y = gr.Dropdown(choices=["Feature1", "Feature2"], value="Feature2", label="Feature 2")
|
| 132 |
n_samples = gr.Slider(minimum=10, maximum=10000, step=25, value=500, label="Number of Samples")
|
| 133 |
-
scatter_plot_button = gr.Button("Generate Scatter Plot")
|
| 134 |
-
scatter_plot = gr.Plot(label="Feature Scatter Plot")
|
| 135 |
-
|
| 136 |
-
scatter_plot_button.click(
|
| 137 |
-
fn=plot_interactive_feature_scatter,
|
| 138 |
-
inputs=[input_data, feature_x, feature_y, n_samples],
|
| 139 |
-
outputs=scatter_plot,
|
| 140 |
-
)
|
| 141 |
-
|
| 142 |
-
# Compare Anomaly Detection Algorithms
|
| 143 |
-
gr.Markdown("### 2. Compare Anomaly Detection Algorithms")
|
| 144 |
outliers_fraction = gr.Slider(minimum=0.001, maximum=0.999, step=0.1, value=0.2, label="Fraction of Outliers")
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
for model_name in input_models:
|
| 149 |
-
plot = gr.Plot(label=model_name)
|
| 150 |
-
plots.append((model_name, plot))
|
| 151 |
-
|
| 152 |
-
def update_anomaly_comparison(input_data, outliers_fraction, n_samples):
|
| 153 |
-
results = []
|
| 154 |
-
for clf_name, plot in plots:
|
| 155 |
-
fig = train_models(input_data, outliers_fraction, n_samples, clf_name)
|
| 156 |
-
results.append(fig)
|
| 157 |
-
return results
|
| 158 |
-
|
| 159 |
-
anomaly_inputs = [input_data, outliers_fraction, n_samples]
|
| 160 |
-
anomaly_outputs = [plot for _, plot in plots]
|
| 161 |
-
input_data.change(fn=update_anomaly_comparison, inputs=anomaly_inputs, outputs=anomaly_outputs)
|
| 162 |
-
n_samples.change(fn=update_anomaly_comparison, inputs=anomaly_inputs, outputs=anomaly_outputs)
|
| 163 |
-
outliers_fraction.change(fn=update_anomaly_comparison, inputs=anomaly_inputs, outputs=anomaly_outputs)
|
| 164 |
-
|
| 165 |
-
# Anomaly Samples Tab
|
| 166 |
-
gr.Markdown("### 3. Example Anomaly Records")
|
| 167 |
-
model_dropdown = gr.Dropdown(choices=input_models, value="Isolation Forest", label="Select Model")
|
| 168 |
top_table = gr.Dataframe(label="Top 10 Anomalies")
|
| 169 |
middle_table = gr.Dataframe(label="Middle 10 Records")
|
| 170 |
bottom_table = gr.Dataframe(label="Bottom 10 Normals")
|
| 171 |
anomaly_samples_button = gr.Button("Show Anomaly Samples")
|
| 172 |
-
|
| 173 |
anomaly_samples_button.click(
|
| 174 |
-
fn=get_anomaly_samples,
|
| 175 |
inputs=[input_data, n_samples, outliers_fraction, model_dropdown],
|
| 176 |
outputs=[top_table, middle_table, bottom_table],
|
| 177 |
)
|
|
|
|
| 33 |
labels[-len(outliers):] = "Anomaly"
|
| 34 |
return X, labels
|
| 35 |
|
| 36 |
+
# Function to train models and generate plots
|
| 37 |
+
def train_models(input_data, outliers_fraction, n_samples, clf_name):
|
| 38 |
+
X, _ = prepare_data(input_data, n_samples, outliers_fraction)
|
| 39 |
+
|
| 40 |
+
# Define classifiers
|
| 41 |
+
NAME_CLF_MAPPING = {
|
| 42 |
+
"Robust covariance": EllipticEnvelope(contamination=outliers_fraction),
|
| 43 |
+
"One-Class SVM": svm.OneClassSVM(nu=outliers_fraction, kernel="rbf", gamma=0.1),
|
| 44 |
+
"One-Class SVM (SGD)": make_pipeline(
|
| 45 |
+
Nystroem(gamma=0.1, random_state=42, n_components=150),
|
| 46 |
+
SGDOneClassSVM(
|
| 47 |
+
nu=outliers_fraction,
|
| 48 |
+
shuffle=True,
|
| 49 |
+
fit_intercept=True,
|
| 50 |
+
random_state=42,
|
| 51 |
+
tol=1e-6,
|
| 52 |
+
),
|
| 53 |
+
),
|
| 54 |
+
"Isolation Forest": IsolationForest(contamination=outliers_fraction, random_state=42),
|
| 55 |
+
"Local Outlier Factor": LocalOutlierFactor(n_neighbors=35, contamination=outliers_fraction),
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
clf = NAME_CLF_MAPPING[clf_name]
|
| 59 |
+
xx, yy = np.meshgrid(np.linspace(-7, 7, 150), np.linspace(-7, 7, 150))
|
| 60 |
+
|
| 61 |
+
t0 = time.time()
|
| 62 |
+
if clf_name == "Local Outlier Factor":
|
| 63 |
+
y_pred = clf.fit_predict(X)
|
| 64 |
+
else:
|
| 65 |
+
clf.fit(X)
|
| 66 |
+
y_pred = clf.predict(X)
|
| 67 |
+
t1 = time.time()
|
| 68 |
+
|
| 69 |
+
# Plotting
|
| 70 |
+
plt.figure(figsize=(5, 5))
|
| 71 |
+
if clf_name != "Local Outlier Factor":
|
| 72 |
+
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
|
| 73 |
+
Z = Z.reshape(xx.shape)
|
| 74 |
+
plt.contour(xx, yy, Z, levels=[0], linewidths=2, colors="black")
|
| 75 |
+
|
| 76 |
+
colors = np.array(["#377eb8", "#ff7f00"])
|
| 77 |
+
plt.scatter(X[:, 0], X[:, 1], s=30, color=colors[(y_pred + 1) // 2])
|
| 78 |
+
plt.title(f"{clf_name} ({t1 - t0:.2f}s)")
|
| 79 |
+
plt.xlim(-7, 7)
|
| 80 |
+
plt.ylim(-7, 7)
|
| 81 |
+
plt.xticks(())
|
| 82 |
+
plt.yticks(())
|
| 83 |
+
return plt.gcf()
|
| 84 |
+
|
| 85 |
# Function to detect anomalies and generate anomaly records
|
| 86 |
def detect_anomalies(input_data, n_samples, outliers_fraction, model_name):
|
| 87 |
X, labels = prepare_data(input_data, n_samples, outliers_fraction)
|
|
|
|
| 148 |
|
| 149 |
return top_10, middle_10, bottom_10
|
| 150 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
# Gradio Interface
|
| 152 |
with gr.Blocks() as demo:
|
|
|
|
| 153 |
gr.Markdown("## 🕵️♀️ Anomaly Detection App 🕵️♂️")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
input_data = gr.Radio(
|
| 155 |
choices=["Central Blob", "Two Blobs", "Blob with Noise", "Moons", "Noise"],
|
| 156 |
value="Moons",
|
| 157 |
label="Dataset"
|
| 158 |
)
|
|
|
|
|
|
|
| 159 |
n_samples = gr.Slider(minimum=10, maximum=10000, step=25, value=500, label="Number of Samples")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
outliers_fraction = gr.Slider(minimum=0.001, maximum=0.999, step=0.1, value=0.2, label="Fraction of Outliers")
|
| 161 |
+
model_dropdown = gr.Dropdown(choices=["Robust covariance", "One-Class SVM", "One-Class SVM (SGD)", "Isolation Forest", "Local Outlier Factor"], label="Select Model")
|
| 162 |
+
|
| 163 |
+
# Anomaly Samples Output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
top_table = gr.Dataframe(label="Top 10 Anomalies")
|
| 165 |
middle_table = gr.Dataframe(label="Middle 10 Records")
|
| 166 |
bottom_table = gr.Dataframe(label="Bottom 10 Normals")
|
| 167 |
anomaly_samples_button = gr.Button("Show Anomaly Samples")
|
|
|
|
| 168 |
anomaly_samples_button.click(
|
| 169 |
+
fn=get_anomaly_samples,
|
| 170 |
inputs=[input_data, n_samples, outliers_fraction, model_dropdown],
|
| 171 |
outputs=[top_table, middle_table, bottom_table],
|
| 172 |
)
|