Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -27,12 +27,15 @@ def prepare_data(input_data, n_samples, outliers_fraction=0.0):
|
|
| 27 |
}
|
| 28 |
X = DATA_MAPPING[input_data]
|
| 29 |
rng = np.random.RandomState(42)
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
# Function to train models and generate plots
|
| 34 |
def train_models(input_data, outliers_fraction, n_samples, clf_name):
|
| 35 |
-
X = prepare_data(input_data, n_samples, outliers_fraction)
|
| 36 |
|
| 37 |
# Define classifiers
|
| 38 |
NAME_CLF_MAPPING = {
|
|
@@ -81,9 +84,7 @@ def train_models(input_data, outliers_fraction, n_samples, clf_name):
|
|
| 81 |
|
| 82 |
# Function to generate feature scatter plots
|
| 83 |
def plot_interactive_feature_scatter(input_data, feature_x, feature_y, n_samples):
|
| 84 |
-
data = prepare_data(input_data, n_samples)
|
| 85 |
-
|
| 86 |
-
# Simulate feature selection by indexing
|
| 87 |
x_data = data[:, 0] if feature_x == "Feature1" else data[:, 1]
|
| 88 |
y_data = data[:, 1] if feature_y == "Feature2" else data[:, 0]
|
| 89 |
|
|
@@ -97,24 +98,35 @@ def plot_interactive_feature_scatter(input_data, feature_x, feature_y, n_samples
|
|
| 97 |
return plt.gcf()
|
| 98 |
|
| 99 |
# Function to simulate anomaly samples
|
| 100 |
-
def get_anomaly_samples():
|
| 101 |
-
#
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
# Top 10 anomalies
|
| 109 |
-
top_10 = df
|
| 110 |
|
| 111 |
-
# Middle 10
|
| 112 |
-
|
|
|
|
| 113 |
|
| 114 |
# Bottom 10 normals
|
| 115 |
-
bottom_10 = df[df["Anomaly_Label"] == "Normal"].
|
| 116 |
|
| 117 |
-
return top_10,
|
| 118 |
|
| 119 |
# Gradio Interface
|
| 120 |
with gr.Blocks() as demo:
|
|
@@ -122,7 +134,7 @@ with gr.Blocks() as demo:
|
|
| 122 |
gr.Markdown("## 🕵️♀️ Anomaly Detection App 🕵️♂️")
|
| 123 |
gr.Markdown("Explore anomaly detection models, feature interactions, and anomaly examples.")
|
| 124 |
|
| 125 |
-
# Anomaly Detection
|
| 126 |
gr.Markdown("### 1. Compare Anomaly Detection Algorithms")
|
| 127 |
input_data = gr.Radio(
|
| 128 |
choices=["Central Blob", "Two Blobs", "Blob with Noise", "Moons", "Noise"],
|
|
@@ -166,16 +178,16 @@ with gr.Blocks() as demo:
|
|
| 166 |
)
|
| 167 |
|
| 168 |
# Anomaly Samples Tab
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
|
| 181 |
demo.launch(debug=True)
|
|
|
|
| 27 |
}
|
| 28 |
X = DATA_MAPPING[input_data]
|
| 29 |
rng = np.random.RandomState(42)
|
| 30 |
+
outliers = rng.uniform(low=-6, high=6, size=(n_outliers, 2))
|
| 31 |
+
X = np.concatenate([X, outliers], axis=0)
|
| 32 |
+
labels = np.array(["Normal"] * len(X))
|
| 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 = {
|
|
|
|
| 84 |
|
| 85 |
# Function to generate feature scatter plots
|
| 86 |
def plot_interactive_feature_scatter(input_data, feature_x, feature_y, n_samples):
|
| 87 |
+
data, _ = prepare_data(input_data, n_samples)
|
|
|
|
|
|
|
| 88 |
x_data = data[:, 0] if feature_x == "Feature1" else data[:, 1]
|
| 89 |
y_data = data[:, 1] if feature_y == "Feature2" else data[:, 0]
|
| 90 |
|
|
|
|
| 98 |
return plt.gcf()
|
| 99 |
|
| 100 |
# Function to simulate anomaly samples
|
| 101 |
+
def get_anomaly_samples(input_data, n_samples, outliers_fraction):
|
| 102 |
+
# Prepare data with labels
|
| 103 |
+
X, labels = prepare_data(input_data, n_samples, outliers_fraction)
|
| 104 |
+
|
| 105 |
+
# Assign anomaly scores (simulated here)
|
| 106 |
+
scores = np.random.random(len(X))
|
| 107 |
+
|
| 108 |
+
# Create a DataFrame
|
| 109 |
+
df = pd.DataFrame({
|
| 110 |
+
"Feature1": X[:, 0],
|
| 111 |
+
"Feature2": X[:, 1],
|
| 112 |
+
"Anomaly_Score": scores,
|
| 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 |
|
| 131 |
# Gradio Interface
|
| 132 |
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"],
|
|
|
|
| 178 |
)
|
| 179 |
|
| 180 |
# Anomaly Samples Tab
|
| 181 |
+
gr.Markdown("### 3. Example Anomaly Records")
|
| 182 |
+
top_table = gr.Dataframe(label="Top 10 Anomalies")
|
| 183 |
+
middle_table = gr.Dataframe(label="Middle 10 Records")
|
| 184 |
+
bottom_table = gr.Dataframe(label="Bottom 10 Normals")
|
| 185 |
+
anomaly_samples_button = gr.Button("Show Anomaly Samples")
|
| 186 |
+
|
| 187 |
+
anomaly_samples_button.click(
|
| 188 |
+
fn=get_anomaly_samples,
|
| 189 |
+
inputs=[input_data, n_samples, outliers_fraction],
|
| 190 |
+
outputs=[top_table, middle_table, bottom_table],
|
| 191 |
+
)
|
| 192 |
|
| 193 |
demo.launch(debug=True)
|