rtik007 commited on
Commit
e534cc9
·
verified ·
1 Parent(s): e759241

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -111
app.py CHANGED
@@ -1,89 +1,32 @@
1
  import numpy as np
2
  import matplotlib.pyplot as plt
3
- from sklearn import svm
4
- from sklearn.covariance import EllipticEnvelope
5
- from sklearn.ensemble import IsolationForest
6
- from sklearn.neighbors import LocalOutlierFactor
7
- from sklearn.linear_model import SGDOneClassSVM
8
- 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 time
13
 
14
- # Function to train models and generate plots
15
- def train_models(input_data, outliers_fraction, n_samples, clf_name):
16
- # Prepare data
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)
20
-
21
- DATA_MAPPING = {
22
- "Central Blob": make_blobs(centers=[[0, 0], [0, 0]], cluster_std=0.5, **blobs_params)[0],
23
- "Two Blobs": make_blobs(centers=[[2, 2], [-2, -2]], cluster_std=[0.5, 0.5], **blobs_params)[0],
24
- "Blob with Noise": make_blobs(centers=[[2, 2], [-2, -2]], cluster_std=[1.5, 0.3], **blobs_params)[0],
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
- NAME_CLF_MAPPING = {
30
- "Robust covariance": EllipticEnvelope(contamination=outliers_fraction),
31
- "One-Class SVM": svm.OneClassSVM(nu=outliers_fraction, kernel="rbf", gamma=0.1),
32
- "One-Class SVM (SGD)": make_pipeline(
33
- Nystroem(gamma=0.1, random_state=42, n_components=150),
34
- SGDOneClassSVM(
35
- nu=outliers_fraction,
36
- shuffle=True,
37
- fit_intercept=True,
38
- random_state=42,
39
- tol=1e-6,
40
- ),
41
- ),
42
- "Isolation Forest": IsolationForest(contamination=outliers_fraction, random_state=42),
43
- "Local Outlier Factor": LocalOutlierFactor(n_neighbors=35, contamination=outliers_fraction),
44
- }
45
-
46
- X = DATA_MAPPING[input_data]
47
- rng = np.random.RandomState(42)
48
- X = np.concatenate([X, rng.uniform(low=-6, high=6, size=(n_outliers, 2))], axis=0)
49
 
50
- xx, yy = np.meshgrid(np.linspace(-7, 7, 150), np.linspace(-7, 7, 150))
51
- clf = NAME_CLF_MAPPING[clf_name]
52
-
53
- t0 = time.time()
54
- if clf_name == "Local Outlier Factor":
55
- y_pred = clf.fit_predict(X)
56
  else:
57
- clf.fit(X)
58
- y_pred = clf.predict(X)
59
- t1 = time.time()
60
 
61
- # Plot
62
- plt.figure(figsize=(5, 5))
63
- if clf_name != "Local Outlier Factor":
64
- Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
65
- Z = Z.reshape(xx.shape)
66
- plt.contour(xx, yy, Z, levels=[0], linewidths=2, colors="black")
67
 
68
- colors = np.array(["#377eb8", "#ff7f00"])
69
- plt.scatter(X[:, 0], X[:, 1], s=30, color=colors[(y_pred + 1) // 2])
70
- plt.title(f"{clf_name} ({t1 - t0:.2f}s)")
71
- plt.xlim(-7, 7)
72
- plt.ylim(-7, 7)
73
- plt.xticks(())
74
- plt.yticks(())
75
- return plt.gcf()
76
-
77
- # Function for Feature Scatter Plots
78
- def plot_feature_scatter(input_data, n_samples):
79
- data, _ = make_moons(n_samples=n_samples, noise=0.05) if input_data == "Moons" else make_blobs(n_samples=n_samples, random_state=0)
80
- plt.figure(figsize=(5, 5))
81
- plt.scatter(data[:, 0], data[:, 1], alpha=0.8, c="blue", s=20, label="Features")
82
- plt.title(f"Feature Scatter Plot - {input_data}")
83
  plt.legend()
84
  return plt.gcf()
85
 
86
- # Function for Anomaly Examples
 
87
  def plot_anomaly_examples(input_data, n_samples, outliers_fraction):
88
  n_outliers = int(outliers_fraction * n_samples)
89
  rng = np.random.RandomState(42)
@@ -96,54 +39,48 @@ def plot_anomaly_examples(input_data, n_samples, outliers_fraction):
96
  plt.legend()
97
  return plt.gcf()
98
 
99
- # Gradio Interface
100
- description = "Compare how different anomaly detection algorithms perform on various datasets."
101
- title = "🕵️‍♀️ Compare Anomaly Detection Algorithms 🕵️‍♂️"
102
 
 
103
  with gr.Blocks() as demo:
104
- gr.Markdown(f"## {title}")
105
- gr.Markdown(description)
106
 
107
- # Inputs
108
  input_data = gr.Radio(
109
  choices=["Central Blob", "Two Blobs", "Blob with Noise", "Moons", "Noise"],
110
  value="Moons",
111
  label="Dataset"
112
  )
113
- n_samples = gr.Slider(minimum=100, maximum=500, step=25, value=300, label="Number of Samples")
114
- outliers_fraction = gr.Slider(
115
- minimum=0.01, maximum=0.99, step=0.01, value=0.2, label="Fraction of Outliers"
116
  )
117
 
118
- # Models and their plots in a row
119
- input_models = ["Robust covariance", "One-Class SVM", "One-Class SVM (SGD)", "Isolation Forest", "Local Outlier Factor"]
120
- plots = []
121
-
122
  with gr.Row():
123
- for model_name in input_models:
124
- plot = gr.Plot(label=model_name)
125
- plots.append((model_name, plot))
126
 
127
- # Additional Rows for Scatter and Anomaly Examples
 
 
 
 
 
 
 
 
128
  with gr.Row():
129
- feature_plot = gr.Plot(label="Feature Scatter Plot")
130
- anomaly_plot = gr.Plot(label="Anomaly Examples")
131
-
132
- # Update function
133
- def update(input_data, outliers_fraction, n_samples):
134
- model_results = []
135
- for clf_name, plot in plots:
136
- fig = train_models(input_data, outliers_fraction, n_samples, clf_name)
137
- model_results.append(fig)
138
- feature_fig = plot_feature_scatter(input_data, n_samples)
139
- anomaly_fig = plot_anomaly_examples(input_data, n_samples, outliers_fraction)
140
- return model_results + [feature_fig, anomaly_fig]
141
-
142
- # Set change triggers
143
- inputs = [input_data, outliers_fraction, n_samples]
144
- demo_outputs = [plot for _, plot in plots] + [feature_plot, anomaly_plot]
145
- input_data.change(fn=update, inputs=inputs, outputs=demo_outputs)
146
- n_samples.change(fn=update, inputs=inputs, outputs=demo_outputs)
147
- outliers_fraction.change(fn=update, inputs=inputs, outputs=demo_outputs)
148
 
149
  demo.launch(debug=True)
 
1
  import numpy as np
2
  import matplotlib.pyplot as plt
3
+ from sklearn.datasets import make_moons, make_blobs
 
 
 
 
 
 
 
4
  import gradio as gr
 
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
+ # Function to generate interactive feature scatter plots
8
+ def plot_interactive_feature_scatter(input_data, feature_x, feature_y, n_samples):
9
+ # Generate data based on the selected dataset
10
+ if input_data == "Moons":
11
+ data, _ = make_moons(n_samples=n_samples, noise=0.05)
 
12
  else:
13
+ data, _ = make_blobs(n_samples=n_samples, random_state=0)
 
 
14
 
15
+ # Simulate feature selection by indexing
16
+ x_data = data[:, 0] if feature_x == "Feature1" else data[:, 1]
17
+ y_data = data[:, 1] if feature_y == "Feature2" else data[:, 0]
 
 
 
18
 
19
+ # Generate scatter plot
20
+ plt.figure(figsize=(6, 6))
21
+ plt.scatter(x_data, y_data, alpha=0.8, c="blue", s=20, label="Features")
22
+ plt.title(f"Feature Interaction Scatter Plot - {feature_x} vs {feature_y}")
23
+ plt.xlabel(feature_x)
24
+ plt.ylabel(feature_y)
 
 
 
 
 
 
 
 
 
25
  plt.legend()
26
  return plt.gcf()
27
 
28
+
29
+ # Function for anomaly examples (Optional feature row)
30
  def plot_anomaly_examples(input_data, n_samples, outliers_fraction):
31
  n_outliers = int(outliers_fraction * n_samples)
32
  rng = np.random.RandomState(42)
 
39
  plt.legend()
40
  return plt.gcf()
41
 
 
 
 
42
 
43
+ # Gradio Interface
44
  with gr.Blocks() as demo:
45
+ gr.Markdown(f"## 🕵️‍♀️ Interactive Anomaly Detection and Feature Scatter Plot 🕵️‍♂️")
 
46
 
47
+ # Inputs for dataset selection and sample size
48
  input_data = gr.Radio(
49
  choices=["Central Blob", "Two Blobs", "Blob with Noise", "Moons", "Noise"],
50
  value="Moons",
51
  label="Dataset"
52
  )
53
+ n_samples = gr.Slider(
54
+ minimum=100, maximum=500, step=25, value=300, label="Number of Samples"
 
55
  )
56
 
57
+ # Row for Interactive Feature Scatter Plot
58
+ gr.Markdown("### Feature Interaction: Scatter Plot")
 
 
59
  with gr.Row():
60
+ feature_x = gr.Dropdown(choices=["Feature1", "Feature2"], value="Feature1", label="Feature 1")
61
+ feature_y = gr.Dropdown(choices=["Feature1", "Feature2"], value="Feature2", label="Feature 2")
62
+ scatter_plot_button = gr.Button("Generate Scatter Plot")
63
 
64
+ scatter_plot = gr.Plot(label="Interactive Feature Scatter Plot")
65
+ scatter_plot_button.click(
66
+ fn=plot_interactive_feature_scatter,
67
+ inputs=[input_data, feature_x, feature_y, n_samples],
68
+ outputs=scatter_plot,
69
+ )
70
+
71
+ # Row for Anomaly Examples (Optional)
72
+ gr.Markdown("### Anomaly Examples")
73
  with gr.Row():
74
+ outliers_fraction = gr.Slider(
75
+ minimum=0.01, maximum=0.99, step=0.01, value=0.2, label="Fraction of Outliers"
76
+ )
77
+ anomaly_plot_button = gr.Button("Generate Anomaly Examples")
78
+
79
+ anomaly_plot = gr.Plot(label="Anomaly Examples")
80
+ anomaly_plot_button.click(
81
+ fn=plot_anomaly_examples,
82
+ inputs=[input_data, n_samples, outliers_fraction],
83
+ outputs=anomaly_plot,
84
+ )
 
 
 
 
 
 
 
 
85
 
86
  demo.launch(debug=True)