Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
from matplotlib.colors import ListedColormap
|
4 |
+
plt.rcParams['figure.dpi'] = 100
|
5 |
+
|
6 |
+
from sklearn.ensemble import AdaBoostClassifier
|
7 |
+
from sklearn.tree import DecisionTreeClassifier
|
8 |
+
from sklearn.datasets import make_gaussian_quantiles
|
9 |
+
from sklearn.inspection import DecisionBoundaryDisplay
|
10 |
+
|
11 |
+
import gradio as gr
|
12 |
+
|
13 |
+
#=======================================================
|
14 |
+
C1, C2 = '#ff0000', '#0000ff'
|
15 |
+
CMAP = ListedColormap([C1, C2])
|
16 |
+
GRANULARITY = 0.05
|
17 |
+
#=======================================================
|
18 |
+
def get_decision_surface(X, y, model):
|
19 |
+
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
|
20 |
+
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
|
21 |
+
xrange = np.arange(x_min, x_max, GRANULARITY)
|
22 |
+
yrange = np.arange(y_min, y_max, GRANULARITY)
|
23 |
+
xx, yy = np.meshgrid(xrange, yrange)
|
24 |
+
|
25 |
+
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
|
26 |
+
Z = Z.reshape(xx.shape)
|
27 |
+
|
28 |
+
return xx, yy, Z
|
29 |
+
|
30 |
+
def create_plot(x1, y1, x2, y2, cov1, cov2, n1, n2, max_depth, n_estimators):
|
31 |
+
#Generate the dataset
|
32 |
+
X1, y1 = make_gaussian_quantiles(
|
33 |
+
mean=(x1, y1), cov=cov1, n_samples=n1, n_features=2, n_classes=2
|
34 |
+
)
|
35 |
+
X2, y2 = make_gaussian_quantiles(
|
36 |
+
mean=(x2, y2), cov=cov2, n_samples=n2, n_features=2, n_classes=2
|
37 |
+
)
|
38 |
+
X = np.concatenate((X1, X2))
|
39 |
+
y = np.concatenate((y1, -y2 + 1))
|
40 |
+
|
41 |
+
clf = AdaBoostClassifier(DecisionTreeClassifier(max_depth=max_depth), algorithm="SAMME", n_estimators=n_estimators)
|
42 |
+
|
43 |
+
clf.fit(X, y)
|
44 |
+
|
45 |
+
fig = plt.figure(figsize=(12, 5))
|
46 |
+
ax = fig.add_subplot(121)
|
47 |
+
|
48 |
+
xx, yy, Z = get_decision_surface(X, y, clf)
|
49 |
+
ax.contourf(xx, yy, Z, cmap=CMAP, alpha=0.65)
|
50 |
+
|
51 |
+
X1, y1 = X[y==0], y[y==0]
|
52 |
+
X2, y2 = X[y==1], y[y==1]
|
53 |
+
|
54 |
+
ax.scatter(X1[:, 0], X1[:, 1], c=C1, edgecolor='k', s=40, label='Class A')
|
55 |
+
ax.scatter(X2[:, 0], X2[:, 1], c=C2, edgecolor='k', s=40, label='Class B')
|
56 |
+
|
57 |
+
ax.set_xlabel('x'); ax.set_ylabel('y')
|
58 |
+
ax.legend()
|
59 |
+
ax.set_title(f'AdaBoostClassifier Decision Surface')
|
60 |
+
|
61 |
+
scores = clf.decision_function(X)
|
62 |
+
|
63 |
+
ax = fig.add_subplot(122)
|
64 |
+
ax.hist(scores[y==0], bins=100, range=(scores.min(), scores.max()), facecolor=C1, label="Class A", alpha=0.5, edgecolor="k")
|
65 |
+
ax.hist(scores[y==1], bins=100, range=(scores.min(), scores.max()), facecolor=C2, label="Class B", alpha=0.5, edgecolor="k")
|
66 |
+
|
67 |
+
ax.set_xlabel('Score'); ax.set_ylabel('Frequency')
|
68 |
+
ax.legend()
|
69 |
+
ax.set_title('Decision Scores')
|
70 |
+
|
71 |
+
return fig
|
72 |
+
|
73 |
+
info = '''
|
74 |
+
This example fits an [AdaBoost classifier](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.AdaBoostClassifier.html#sklearn.ensemble.AdaBoostClassifier) on two non-linearly separable classes. The samples are generated using two [Gaussian quantiles](https://scikit-learn.org/stable/modules/generated/sklearn.datasets.make_gaussian_quantiles.html#sklearn.datasets.make_gaussian_quantiles) of configurable mean and covariance (see the sliders below).
|
75 |
+
|
76 |
+
For the first generated Gaussian, the inner half quantile is assigned to Class A and the outer half quantile is assigned to class B. For the second generated quantile, the opposite assignment happens (inner = Class B, outer = Class A).
|
77 |
+
|
78 |
+
A histogram of the decision scores of the AdaBoostClassifer is shown below. Values closer to -1 mean a high confidence that the sample belongs to Class A, and values closer to 1 mean a high confidence that the sample belongs to Class B.
|
79 |
+
|
80 |
+
Use the controls below to change the Gaussian distribution parameters, number of generated samples in each Gaussian distribution, and the classifier's max_depth and n_estimators.
|
81 |
+
'''
|
82 |
+
with gr.Blocks(analytics_enabled=False) as demo:
|
83 |
+
gr.Markdown(info)
|
84 |
+
|
85 |
+
with gr.Row():
|
86 |
+
with gr.Column():
|
87 |
+
s_x1 = gr.Slider(-10, 10, value=0, step=0.1, label='Mean x1')
|
88 |
+
with gr.Column():
|
89 |
+
s_y1 = gr.Slider(-10, 10, value=0, step=0.1, label='Mean y1')
|
90 |
+
with gr.Row():
|
91 |
+
with gr.Column():
|
92 |
+
s_x2 = gr.Slider(-10, 10, value=2, step=0.1, label='Mean x2')
|
93 |
+
with gr.Column():
|
94 |
+
s_y2 = gr.Slider(-10, 10, value=2, step=0.1, label='Mean y2')
|
95 |
+
|
96 |
+
with gr.Row():
|
97 |
+
with gr.Column():
|
98 |
+
s_cov1 = gr.Slider(0.01, 5, value=1, step=0.01, label='Covariance 1')
|
99 |
+
with gr.Column():
|
100 |
+
s_cov2 = gr.Slider(0.01, 5, value=2, step=0.01, label='Covariance 2')
|
101 |
+
|
102 |
+
with gr.Row():
|
103 |
+
with gr.Column():
|
104 |
+
s_n_samples1 = gr.Slider(1, 1000, value=200, step=1, label='n_samples 1')
|
105 |
+
with gr.Column():
|
106 |
+
s_n_samples2 = gr.Slider(1, 1000, value=300, step=1, label='n_samples 2')
|
107 |
+
|
108 |
+
with gr.Row():
|
109 |
+
with gr.Column():
|
110 |
+
s_max_depth = gr.Slider(1, 50, value=1, step=1, label='AdaBoostClassifier max_depth')
|
111 |
+
with gr.Column():
|
112 |
+
s_n_estimators = gr.Slider(1, 500, value=300, step=1, label='AdaBoostClassifier n_estimators')
|
113 |
+
|
114 |
+
btn = gr.Button('Submit')
|
115 |
+
|
116 |
+
plot = gr.Plot(label='Decision Surfaces & Histogram of Scores')
|
117 |
+
|
118 |
+
btn.click(create_plot, inputs=[s_x1, s_y1, s_x2, s_y2, s_cov1, s_cov2, s_n_samples1, s_n_samples2, s_max_depth, s_n_estimators], outputs=[plot])
|
119 |
+
demo.load(create_plot, inputs=[s_x1, s_y1, s_x2, s_y2, s_cov1, s_cov2, s_n_samples1, s_n_samples2, s_max_depth, s_n_estimators], outputs=[plot])
|
120 |
+
|
121 |
+
demo.launch(share=True, debug=True)
|
122 |
+
#=======================================================
|