rtik007 commited on
Commit
4f99aa7
·
verified ·
1 Parent(s): 7840140

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -31
app.py CHANGED
@@ -22,8 +22,8 @@ def train_models(input_data, outliers_fraction, n_samples, clf_name):
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 = {
@@ -45,7 +45,8 @@ def train_models(input_data, outliers_fraction, n_samples, clf_name):
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]
@@ -53,25 +54,30 @@ def train_models(input_data, outliers_fraction, n_samples, clf_name):
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
  # Gradio Interface
@@ -82,30 +88,32 @@ with gr.Blocks() as demo:
82
  gr.Markdown(f"## {title}")
83
  gr.Markdown(description)
84
 
85
- # Inputs
86
- input_data = gr.Radio(
87
- choices=["Central Blob", "Two Blobs", "Blob with Noise", "Moons", "Noise"],
88
- value="Moons",
89
- label="Dataset"
90
- )
91
- n_samples = gr.Slider(minimum=100, maximum=500, step=25, value=300, label="Number of Samples")
92
- outliers_fraction = gr.Slider(minimum=0.1, maximum=0.9, step=0.1, value=0.2, label="Fraction of Outliers")
93
-
94
- # Models and their plots in a row
95
- input_models = ["Robust covariance", "One-Class SVM", "One-Class SVM (SGD)", "Isolation Forest", "Local Outlier Factor"]
96
- plots = []
97
-
98
  with gr.Row():
99
- for model_name in input_models:
100
- plot = gr.Plot(label=model_name)
101
- plots.append((model_name, plot))
102
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  # Update function
104
  def update(input_data, outliers_fraction, n_samples):
105
  results = []
106
  for clf_name, plot in plots:
107
  fig = train_models(input_data, outliers_fraction, n_samples, clf_name)
108
  results.append(fig)
 
109
  return results
110
 
111
  # Set change triggers
@@ -114,5 +122,8 @@ with gr.Blocks() as demo:
114
  input_data.change(fn=update, inputs=inputs, outputs=demo_outputs)
115
  n_samples.change(fn=update, inputs=inputs, outputs=demo_outputs)
116
  outliers_fraction.change(fn=update, inputs=inputs, outputs=demo_outputs)
 
 
 
117
 
118
  demo.launch(debug=True)
 
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_inliers, noise=0.05, random_state=0)[0] - np.array([0.5, 0.25])),
26
+ "Noise": 14.0 * (np.random.RandomState(42).rand(n_inliers, 2) - 0.5),
27
  }
28
 
29
  NAME_CLF_MAPPING = {
 
45
 
46
  X = DATA_MAPPING[input_data]
47
  rng = np.random.RandomState(42)
48
+ X_outliers = rng.uniform(low=-6, high=6, size=(n_outliers, 2))
49
+ X = np.concatenate([X, X_outliers], axis=0)
50
 
51
  xx, yy = np.meshgrid(np.linspace(-7, 7, 150), np.linspace(-7, 7, 150))
52
  clf = NAME_CLF_MAPPING[clf_name]
 
54
  t0 = time.time()
55
  if clf_name == "Local Outlier Factor":
56
  y_pred = clf.fit_predict(X)
57
+ # The decision_function is inverse of the LocalOutlierFactor._score_samples
58
+ Z = clf._decision_function(np.c_[xx.ravel(), yy.ravel()])
59
  else:
60
  clf.fit(X)
61
  y_pred = clf.predict(X)
62
+ Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
63
  t1 = time.time()
64
 
65
  # Plot
66
+ plt.figure(figsize=(6, 6))
67
+ Z = Z.reshape(xx.shape)
68
+ plt.contourf(xx, yy, Z, levels=np.linspace(Z.min(), 0, 7), cmap=plt.cm.Blues_r)
69
+ a = plt.contour(xx, yy, Z, levels=[0], linewidths=2, colors="red")
70
+ plt.contourf(xx, yy, Z, levels=[0, Z.max()], colors="palevioletred")
71
+
72
+ s = 20
73
+ b1 = plt.scatter(X[:-n_outliers, 0], X[:-n_outliers, 1], c="white", s=s, edgecolors="k")
74
+ b2 = plt.scatter(X[-n_outliers:, 0], X[-n_outliers:, 1], c="black", s=s, edgecolors="k")
75
+ plt.axis("tight")
76
+ plt.xlim((-7, 7))
77
+ plt.ylim((-7, 7))
78
  plt.xticks(())
79
  plt.yticks(())
80
+ plt.title(f"{clf_name} ({t1 - t0:.2f}s)")
81
  return plt.gcf()
82
 
83
  # Gradio Interface
 
88
  gr.Markdown(f"## {title}")
89
  gr.Markdown(description)
90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  with gr.Row():
92
+ with gr.Column(scale=1):
93
+ # Inputs
94
+ input_data = gr.Radio(
95
+ choices=["Central Blob", "Two Blobs", "Blob with Noise", "Moons", "Noise"],
96
+ value="Moons",
97
+ label="Dataset"
98
+ )
99
+ n_samples = gr.Slider(minimum=100, maximum=500, step=25, value=300, label="Number of Samples")
100
+ outliers_fraction = gr.Slider(minimum=0.1, maximum=0.9, step=0.1, value=0.2, label="Fraction of Outliers")
101
+ with gr.Column(scale=3):
102
+ # Models and their plots
103
+ input_models = ["Robust covariance", "One-Class SVM", "One-Class SVM (SGD)", "Isolation Forest", "Local Outlier Factor"]
104
+ plots = []
105
+ with gr.Row():
106
+ for model_name in input_models:
107
+ plot = gr.Plot(label=model_name)
108
+ plots.append((model_name, plot))
109
+
110
  # Update function
111
  def update(input_data, outliers_fraction, n_samples):
112
  results = []
113
  for clf_name, plot in plots:
114
  fig = train_models(input_data, outliers_fraction, n_samples, clf_name)
115
  results.append(fig)
116
+ plt.close(fig)
117
  return results
118
 
119
  # Set change triggers
 
122
  input_data.change(fn=update, inputs=inputs, outputs=demo_outputs)
123
  n_samples.change(fn=update, inputs=inputs, outputs=demo_outputs)
124
  outliers_fraction.change(fn=update, inputs=inputs, outputs=demo_outputs)
125
+
126
+ # Initial update to display plots on load
127
+ demo.load(fn=update, inputs=inputs, outputs=demo_outputs)
128
 
129
  demo.launch(debug=True)