rtik007 commited on
Commit
289b779
·
verified ·
1 Parent(s): f7681d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -18
app.py CHANGED
@@ -113,18 +113,21 @@ def get_anomaly_samples(input_data, n_samples, outliers_fraction):
113
  "Anomaly_Label": labels,
114
  })
115
 
 
 
 
116
  # Round values to 3 decimal places
117
  df = df.round({"Feature1": 3, "Feature2": 3, "Anomaly_Score": 3})
118
 
119
  # Top 10 anomalies
120
- top_10 = df[df["Anomaly_Label"] == "Anomaly"].nlargest(10, "Anomaly_Score")
121
 
122
  # Middle 10 (mixed)
123
  mid_start = len(df) // 2 - 5
124
  middle_10 = df.iloc[mid_start: mid_start + 10]
125
 
126
  # Bottom 10 normals
127
- bottom_10 = df[df["Anomaly_Label"] == "Normal"].nsmallest(10, "Anomaly_Score")
128
 
129
  return top_10, middle_10, bottom_10
130
 
@@ -134,16 +137,28 @@ with gr.Blocks() as demo:
134
  gr.Markdown("## 🕵️‍♀️ Anomaly Detection App 🕵️‍♂️")
135
  gr.Markdown("Explore anomaly detection models, feature interactions, and anomaly examples.")
136
 
137
- # Compare Anomaly Detection Algorithms
138
- gr.Markdown("### 1. Compare Anomaly Detection Algorithms")
139
  input_data = gr.Radio(
140
  choices=["Central Blob", "Two Blobs", "Blob with Noise", "Moons", "Noise"],
141
  value="Moons",
142
  label="Dataset"
143
  )
 
 
144
  n_samples = gr.Slider(minimum=10, maximum=10000, step=25, value=500, label="Number of Samples")
145
- outliers_fraction = gr.Slider(minimum=0.001, maximum=0.999, step=0.1, value=0.2, label="Fraction of Outliers")
 
 
 
 
 
 
 
146
 
 
 
 
147
  input_models = ["Robust covariance", "One-Class SVM", "One-Class SVM (SGD)", "Isolation Forest", "Local Outlier Factor"]
148
  plots = []
149
  with gr.Row():
@@ -164,19 +179,6 @@ with gr.Blocks() as demo:
164
  n_samples.change(fn=update_anomaly_comparison, inputs=anomaly_inputs, outputs=anomaly_outputs)
165
  outliers_fraction.change(fn=update_anomaly_comparison, inputs=anomaly_inputs, outputs=anomaly_outputs)
166
 
167
- # Interactive Feature Scatter Plot
168
- gr.Markdown("### 2. Interactive Feature Scatter Plot")
169
- feature_x = gr.Dropdown(choices=["Feature1", "Feature2"], value="Feature1", label="Feature 1")
170
- feature_y = gr.Dropdown(choices=["Feature1", "Feature2"], value="Feature2", label="Feature 2")
171
- scatter_plot_button = gr.Button("Generate Scatter Plot")
172
- scatter_plot = gr.Plot(label="Feature Scatter Plot")
173
-
174
- scatter_plot_button.click(
175
- fn=plot_interactive_feature_scatter,
176
- inputs=[input_data, feature_x, feature_y, n_samples],
177
- outputs=scatter_plot,
178
- )
179
-
180
  # Anomaly Samples Tab
181
  gr.Markdown("### 3. Example Anomaly Records")
182
  top_table = gr.Dataframe(label="Top 10 Anomalies")
 
113
  "Anomaly_Label": labels,
114
  })
115
 
116
+ # Sort by Anomaly Score in descending order
117
+ df = df.sort_values("Anomaly_Score", ascending=False)
118
+
119
  # Round values to 3 decimal places
120
  df = df.round({"Feature1": 3, "Feature2": 3, "Anomaly_Score": 3})
121
 
122
  # Top 10 anomalies
123
+ top_10 = df[df["Anomaly_Label"] == "Anomaly"].head(10)
124
 
125
  # Middle 10 (mixed)
126
  mid_start = len(df) // 2 - 5
127
  middle_10 = df.iloc[mid_start: mid_start + 10]
128
 
129
  # Bottom 10 normals
130
+ bottom_10 = df[df["Anomaly_Label"] == "Normal"].tail(10)
131
 
132
  return top_10, middle_10, bottom_10
133
 
 
137
  gr.Markdown("## 🕵️‍♀️ Anomaly Detection App 🕵️‍♂️")
138
  gr.Markdown("Explore anomaly detection models, feature interactions, and anomaly examples.")
139
 
140
+ # Interactive Feature Scatter Plot
141
+ gr.Markdown("### 1. Interactive Feature Scatter Plot")
142
  input_data = gr.Radio(
143
  choices=["Central Blob", "Two Blobs", "Blob with Noise", "Moons", "Noise"],
144
  value="Moons",
145
  label="Dataset"
146
  )
147
+ feature_x = gr.Dropdown(choices=["Feature1", "Feature2"], value="Feature1", label="Feature 1")
148
+ feature_y = gr.Dropdown(choices=["Feature1", "Feature2"], value="Feature2", label="Feature 2")
149
  n_samples = gr.Slider(minimum=10, maximum=10000, step=25, value=500, label="Number of Samples")
150
+ scatter_plot_button = gr.Button("Generate Scatter Plot")
151
+ scatter_plot = gr.Plot(label="Feature Scatter Plot")
152
+
153
+ scatter_plot_button.click(
154
+ fn=plot_interactive_feature_scatter,
155
+ inputs=[input_data, feature_x, feature_y, n_samples],
156
+ outputs=scatter_plot,
157
+ )
158
 
159
+ # Compare Anomaly Detection Algorithms
160
+ gr.Markdown("### 2. Compare Anomaly Detection Algorithms")
161
+ outliers_fraction = gr.Slider(minimum=0.001, maximum=0.999, step=0.1, value=0.2, label="Fraction of Outliers")
162
  input_models = ["Robust covariance", "One-Class SVM", "One-Class SVM (SGD)", "Isolation Forest", "Local Outlier Factor"]
163
  plots = []
164
  with gr.Row():
 
179
  n_samples.change(fn=update_anomaly_comparison, inputs=anomaly_inputs, outputs=anomaly_outputs)
180
  outliers_fraction.change(fn=update_anomaly_comparison, inputs=anomaly_inputs, outputs=anomaly_outputs)
181
 
 
 
 
 
 
 
 
 
 
 
 
 
 
182
  # Anomaly Samples Tab
183
  gr.Markdown("### 3. Example Anomaly Records")
184
  top_table = gr.Dataframe(label="Top 10 Anomalies")