Jayabalambika commited on
Commit
a1fddda
Β·
1 Parent(s): fd886b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -19
app.py CHANGED
@@ -26,6 +26,14 @@ y = np.concatenate(
26
  [np.ones((2 * n_samples), dtype=int), -np.ones((n_outliers), dtype=int)]
27
  )
28
 
 
 
 
 
 
 
 
 
29
  #Visualize the data as a scatter plot
30
 
31
  def visualize_input_data():
@@ -36,6 +44,7 @@ def visualize_input_data():
36
  plt.legend(handles=handles, labels=["outliers", "inliers"], title="true class")
37
  plt.title("Gaussian inliers with \nuniformly distributed outliers")
38
  # plt.show()
 
39
  return fig
40
 
41
 
@@ -44,7 +53,9 @@ def visualize_input_data():
44
  from sklearn.inspection import DecisionBoundaryDisplay
45
 
46
  def plot_decision_boundary():
47
-
 
 
48
  time.sleep(1)
49
 
50
  disp = DecisionBoundaryDisplay.from_estimator(
@@ -54,7 +65,8 @@ def plot_decision_boundary():
54
  alpha=0.5,
55
  )
56
  fig1 = plt.figure(1, facecolor="w", figsize=(5, 5))
57
- scatter = plt.scatter(X[:, 0], X[:, 1], c=y, s=20, edgecolor="k")
 
58
  disp.ax_.scatter(X[:, 0], X[:, 1], c=y, s=20, edgecolor="k")
59
  handles, labels = scatter.legend_elements()
60
  disp.ax_.set_title("Binary decision boundary \nof IsolationForest")
@@ -65,6 +77,26 @@ def plot_decision_boundary():
65
 
66
  return fig1
67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
 
70
  title = " An example using IsolationForest for anomaly detection."
@@ -74,25 +106,20 @@ with gr.Blocks(title=title) as demo:
74
 
75
 
76
  gr.Markdown(" **https://scikit-learn.org/stable/auto_examples/ensemble/plot_isolation_forest.html#sphx-glr-auto-examples-ensemble-plot-isolation-forest-py**")
 
 
77
 
 
 
 
78
 
79
-
80
- btn = gr.Button(value="Visualize input dataset")
81
- btn.click(visualize_input_data, outputs= gr.Plot(label='Visualizing input dataset') )
82
-
83
- # download
84
- repo_id="sklearn-docs/anomaly-detection"
85
- download_repo = "downloaded-model"
86
- hub_utils.download(repo_id=repo_id, dst=download_repo)
87
-
88
- time.sleep(2)
89
-
90
-
91
-
92
- print(os.listdir(download_repo))
93
- loaded_model = pickle.load(open('./downloaded-model/isolation_forest.pkl', 'rb'))
94
- btn_decision = gr.Button(value="Plot decision boundary")
95
- btn_decision.click(plot_decision_boundary, outputs= gr.Plot(label='Plot decision boundary') )
96
 
97
 
98
  gr.Markdown( f"## Success")
 
26
  [np.ones((2 * n_samples), dtype=int), -np.ones((n_outliers), dtype=int)]
27
  )
28
 
29
+ def load_hf_model_hub():
30
+ repo_id="sklearn-docs/anomaly-detection"
31
+ download_repo = "downloaded-model"
32
+ hub_utils.download(repo_id=repo_id, dst=download_repo)
33
+ time.sleep(2)
34
+ loaded_model = pickle.load(open('./downloaded-model/isolation_forest.pkl', 'rb'))
35
+ return loaded_model
36
+
37
  #Visualize the data as a scatter plot
38
 
39
  def visualize_input_data():
 
44
  plt.legend(handles=handles, labels=["outliers", "inliers"], title="true class")
45
  plt.title("Gaussian inliers with \nuniformly distributed outliers")
46
  # plt.show()
47
+ # plt.clear()
48
  return fig
49
 
50
 
 
53
  from sklearn.inspection import DecisionBoundaryDisplay
54
 
55
  def plot_decision_boundary():
56
+ # progress(0, desc="Starting...")
57
+ # plt.clear()
58
+ plt.clf()
59
  time.sleep(1)
60
 
61
  disp = DecisionBoundaryDisplay.from_estimator(
 
65
  alpha=0.5,
66
  )
67
  fig1 = plt.figure(1, facecolor="w", figsize=(5, 5))
68
+ scatter = plt.scatter(X[:, 0], X[:, 1], c=y, s=20, edgecolor="k")
69
+ # disp.ax_.
70
  disp.ax_.scatter(X[:, 0], X[:, 1], c=y, s=20, edgecolor="k")
71
  handles, labels = scatter.legend_elements()
72
  disp.ax_.set_title("Binary decision boundary \nof IsolationForest")
 
77
 
78
  return fig1
79
 
80
+ def plot_path_length():
81
+ plt.clf()
82
+
83
+ time.sleep(1)
84
+ disp = DecisionBoundaryDisplay.from_estimator(
85
+ loaded_model,
86
+ X,
87
+ response_method="decision_function",
88
+ alpha=0.5,
89
+ )
90
+ fig2 = plt.figure(1, facecolor="w", figsize=(5, 5))
91
+ scatter = disp.ax_.scatter(X[:, 0], X[:, 1], c=y, s=20, edgecolor="k")
92
+ handles, labels = scatter.legend_elements()
93
+ disp.ax_.set_title("Path length decision boundary \nof IsolationForest")
94
+ plt.axis("square")
95
+ plt.legend(handles=handles, labels=["outliers", "inliers"], title="true class")
96
+ plt.colorbar(disp.ax_.collections[1])
97
+ # plt.savefig('plot_path.png',dpi=300, bbox_inches = "tight")
98
+ return fig2
99
+
100
 
101
 
102
  title = " An example using IsolationForest for anomaly detection."
 
106
 
107
 
108
  gr.Markdown(" **https://scikit-learn.org/stable/auto_examples/ensemble/plot_isolation_forest.html#sphx-glr-auto-examples-ensemble-plot-isolation-forest-py**")
109
+
110
+ loaded_model = load_hf_model_hub()
111
 
112
+ with gr.Tab("Visualize Input dataset"):
113
+ btn = gr.Button(value="Visualize input dataset")
114
+ btn.click(visualize_input_data, outputs= gr.Plot(label='Visualizing input dataset') )
115
 
116
+ with gr.Tab("Plot Decision Boundary"):
117
+ btn_decision = gr.Button(value="Plot decision boundary")
118
+ btn_decision.click(plot_decision_boundary, outputs= gr.Plot(label='Plot decision boundary') )
119
+
120
+ with gr.Tab("Plot Path"):
121
+ btn_path = gr.Button(value="Path length decision boundary")
122
+ btn_path.click(plot_path_length, outputs= gr.Plot(label='Path length decision boundary') )
 
 
 
 
 
 
 
 
 
 
123
 
124
 
125
  gr.Markdown( f"## Success")