Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,11 +9,11 @@ from sklearn.kernel_approximation import Nystroem
|
|
| 9 |
from sklearn.pipeline import make_pipeline
|
| 10 |
from sklearn.datasets import make_blobs, make_moons
|
| 11 |
import gradio as gr
|
| 12 |
-
import pandas as pd
|
| 13 |
import time
|
| 14 |
|
| 15 |
# Helper function to prepare data
|
| 16 |
-
def prepare_data(input_data, n_samples, outliers_fraction):
|
| 17 |
n_outliers = int(outliers_fraction * n_samples)
|
| 18 |
n_inliers = n_samples - n_outliers
|
| 19 |
blobs_params = dict(random_state=0, n_samples=n_inliers, n_features=2)
|
|
@@ -25,7 +25,6 @@ def prepare_data(input_data, n_samples, outliers_fraction):
|
|
| 25 |
"Moons": 4.0 * (make_moons(n_samples=n_samples, noise=0.05, random_state=0)[0] - np.array([0.5, 0.25])),
|
| 26 |
"Noise": 14.0 * (np.random.RandomState(42).rand(n_samples, 2) - 0.5),
|
| 27 |
}
|
| 28 |
-
|
| 29 |
X = DATA_MAPPING[input_data]
|
| 30 |
rng = np.random.RandomState(42)
|
| 31 |
X = np.concatenate([X, rng.uniform(low=-6, high=6, size=(n_outliers, 2))], axis=0)
|
|
@@ -80,6 +79,23 @@ def train_models(input_data, outliers_fraction, n_samples, clf_name):
|
|
| 80 |
plt.yticks(())
|
| 81 |
return plt.gcf()
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
# Function to simulate anomaly samples
|
| 84 |
def get_anomaly_samples():
|
| 85 |
# Simulated dataframe
|
|
@@ -136,6 +152,19 @@ with gr.Blocks() as demo:
|
|
| 136 |
n_samples.change(fn=update_anomaly_comparison, inputs=anomaly_inputs, outputs=anomaly_outputs)
|
| 137 |
outliers_fraction.change(fn=update_anomaly_comparison, inputs=anomaly_inputs, outputs=anomaly_outputs)
|
| 138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
# Anomaly Samples Tab
|
| 140 |
with gr.Tab("Anomaly Samples"):
|
| 141 |
gr.Markdown("### Example Anomaly Records")
|
|
|
|
| 9 |
from sklearn.pipeline import make_pipeline
|
| 10 |
from sklearn.datasets import make_blobs, make_moons
|
| 11 |
import gradio as gr
|
| 12 |
+
import pandas as pd
|
| 13 |
import time
|
| 14 |
|
| 15 |
# Helper function to prepare data
|
| 16 |
+
def prepare_data(input_data, n_samples, outliers_fraction=0.0):
|
| 17 |
n_outliers = int(outliers_fraction * n_samples)
|
| 18 |
n_inliers = n_samples - n_outliers
|
| 19 |
blobs_params = dict(random_state=0, n_samples=n_inliers, n_features=2)
|
|
|
|
| 25 |
"Moons": 4.0 * (make_moons(n_samples=n_samples, noise=0.05, random_state=0)[0] - np.array([0.5, 0.25])),
|
| 26 |
"Noise": 14.0 * (np.random.RandomState(42).rand(n_samples, 2) - 0.5),
|
| 27 |
}
|
|
|
|
| 28 |
X = DATA_MAPPING[input_data]
|
| 29 |
rng = np.random.RandomState(42)
|
| 30 |
X = np.concatenate([X, rng.uniform(low=-6, high=6, size=(n_outliers, 2))], axis=0)
|
|
|
|
| 79 |
plt.yticks(())
|
| 80 |
return plt.gcf()
|
| 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 |
+
|
| 90 |
+
# Generate scatter plot
|
| 91 |
+
plt.figure(figsize=(6, 6))
|
| 92 |
+
plt.scatter(x_data, y_data, alpha=0.8, c="blue", s=20, label="Features")
|
| 93 |
+
plt.title(f"Feature Interaction Scatter Plot - {feature_x} vs {feature_y}")
|
| 94 |
+
plt.xlabel(feature_x)
|
| 95 |
+
plt.ylabel(feature_y)
|
| 96 |
+
plt.legend()
|
| 97 |
+
return plt.gcf()
|
| 98 |
+
|
| 99 |
# Function to simulate anomaly samples
|
| 100 |
def get_anomaly_samples():
|
| 101 |
# Simulated dataframe
|
|
|
|
| 152 |
n_samples.change(fn=update_anomaly_comparison, inputs=anomaly_inputs, outputs=anomaly_outputs)
|
| 153 |
outliers_fraction.change(fn=update_anomaly_comparison, inputs=anomaly_inputs, outputs=anomaly_outputs)
|
| 154 |
|
| 155 |
+
# Interactive Feature Scatter Plot
|
| 156 |
+
gr.Markdown("### 2. Interactive Feature Scatter Plot")
|
| 157 |
+
feature_x = gr.Dropdown(choices=["Feature1", "Feature2"], value="Feature1", label="Feature 1")
|
| 158 |
+
feature_y = gr.Dropdown(choices=["Feature1", "Feature2"], value="Feature2", label="Feature 2")
|
| 159 |
+
scatter_plot_button = gr.Button("Generate Scatter Plot")
|
| 160 |
+
scatter_plot = gr.Plot(label="Feature Scatter Plot")
|
| 161 |
+
|
| 162 |
+
scatter_plot_button.click(
|
| 163 |
+
fn=plot_interactive_feature_scatter,
|
| 164 |
+
inputs=[input_data, feature_x, feature_y, n_samples],
|
| 165 |
+
outputs=scatter_plot,
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
# Anomaly Samples Tab
|
| 169 |
with gr.Tab("Anomaly Samples"):
|
| 170 |
gr.Markdown("### Example Anomaly Records")
|