cmpatino commited on
Commit
307e9da
1 Parent(s): d354388

Intital commit

Browse files
Files changed (3) hide show
  1. .gitignore +125 -0
  2. app.py +177 -0
  3. requirements.txt +2 -0
.gitignore ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ .coverage
3
+ .coverage.*
4
+ .cache
5
+ nosetests.xml
6
+ coverage.xml
7
+ *.cover
8
+ *.py,cover
9
+ .hypothesis/
10
+ .pytest_cache/
11
+ cover/
12
+
13
+ # Translations
14
+ *.mo
15
+ *.pot
16
+
17
+ # Django stuff:
18
+ *.log
19
+ local_settings.py
20
+ db.sqlite3
21
+ db.sqlite3-journal
22
+
23
+ # Flask stuff:
24
+ instance/
25
+ .webassets-cache
26
+
27
+ # Scrapy stuff:
28
+ .scrapy
29
+
30
+ # Sphinx documentation
31
+ docs/_build/
32
+
33
+ # PyBuilder
34
+ .pybuilder/
35
+ target/
36
+
37
+ # Jupyter Notebook
38
+ .ipynb_checkpoints
39
+
40
+ # IPython
41
+ profile_default/
42
+ ipython_config.py
43
+
44
+ # pyenv
45
+ # For a library or package, you might want to ignore these files since the code is
46
+ # intended to run in multiple environments; otherwise, check them in:
47
+ # .python-version
48
+
49
+ # pipenv
50
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
51
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
52
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
53
+ # install all needed dependencies.
54
+ #Pipfile.lock
55
+
56
+ # poetry
57
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
58
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
59
+ # commonly ignored for libraries.
60
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
61
+ #poetry.lock
62
+
63
+ # pdm
64
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
65
+ #pdm.lock
66
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
67
+ # in version control.
68
+ # https://pdm.fming.dev/#use-with-ide
69
+ .pdm.toml
70
+
71
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
72
+ __pypackages__/
73
+
74
+ # Celery stuff
75
+ celerybeat-schedule
76
+ celerybeat.pid
77
+
78
+ # SageMath parsed files
79
+ *.sage.py
80
+
81
+ # Environments
82
+ .env
83
+ .venv
84
+ env/
85
+ venv/
86
+ ENV/
87
+ env.bak/
88
+ venv.bak/
89
+
90
+ # Spyder project settings
91
+ .spyderproject
92
+ .spyproject
93
+
94
+ # Rope project settings
95
+ .ropeproject
96
+
97
+ # mkdocs documentation
98
+ /site
99
+
100
+ # mypy
101
+ .mypy_cache/
102
+ .dmypy.json
103
+ dmypy.json
104
+
105
+ # Pyre type checker
106
+ .pyre/
107
+
108
+ # pytype static type analyzer
109
+ .pytype/
110
+
111
+ # Cython debug symbols
112
+ cython_debug/
113
+
114
+ # PyCharm
115
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
116
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
117
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
118
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
119
+ #.idea/
120
+
121
+ # VS Code
122
+ .vscode/
123
+
124
+ # pycache
125
+ __pycache__/
app.py ADDED
@@ -0,0 +1,177 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import matplotlib.pyplot as plt
3
+ import numpy as np
4
+ from sklearn.ensemble import RandomForestClassifier, VotingClassifier
5
+ from sklearn.linear_model import LogisticRegression
6
+ from sklearn.naive_bayes import GaussianNB
7
+
8
+
9
+ def choose_model(model):
10
+ if model == "Logistic Regression":
11
+ return LogisticRegression(max_iter=1000, random_state=123)
12
+ elif model == "Random Forest":
13
+ return RandomForestClassifier(n_estimators=100, random_state=123)
14
+ elif model == "Gaussian Naive Bayes":
15
+ return GaussianNB()
16
+ else:
17
+ raise ValueError("Model is not supported.")
18
+
19
+
20
+ def get_proba_plots(
21
+ model_1, model_2, model_3, model_1_weight, model_2_weight, model_3_weight
22
+ ):
23
+ clf1 = choose_model(model_1)
24
+ clf2 = choose_model(model_2)
25
+ clf3 = choose_model(model_3)
26
+ X = np.array([[-1.0, -1.0], [-1.2, -1.4], [-3.4, -2.2], [1.1, 1.2]])
27
+ y = np.array([1, 1, 2, 2])
28
+
29
+ eclf = VotingClassifier(
30
+ estimators=[("clf1", clf1), ("clf2", clf2), ("clf3", clf3)],
31
+ voting="soft",
32
+ weights=[model_1_weight, model_2_weight, model_3_weight],
33
+ )
34
+
35
+ # predict class probabilities for all classifiers
36
+ probas = [c.fit(X, y).predict_proba(X) for c in (clf1, clf2, clf3, eclf)]
37
+
38
+ # get class probabilities for the first sample in the dataset
39
+ class1_1 = [pr[0, 0] for pr in probas]
40
+ class2_1 = [pr[0, 1] for pr in probas]
41
+
42
+ # plotting
43
+
44
+ N = 4 # number of groups
45
+ ind = np.arange(N) # group positions
46
+ width = 0.35 # bar width
47
+
48
+ fig, ax = plt.subplots()
49
+
50
+ # bars for classifier 1-3
51
+ p1 = ax.bar(
52
+ ind, np.hstack(([class1_1[:-1], [0]])), width, color="green", edgecolor="k"
53
+ )
54
+ p2 = ax.bar(
55
+ ind + width,
56
+ np.hstack(([class2_1[:-1], [0]])),
57
+ width,
58
+ color="lightgreen",
59
+ edgecolor="k",
60
+ )
61
+
62
+ # bars for VotingClassifier
63
+ ax.bar(ind, [0, 0, 0, class1_1[-1]], width, color="blue", edgecolor="k")
64
+ ax.bar(
65
+ ind + width, [0, 0, 0, class2_1[-1]], width, color="steelblue", edgecolor="k"
66
+ )
67
+
68
+ # plot annotations
69
+ plt.axvline(2.8, color="k", linestyle="dashed")
70
+ ax.set_xticks(ind + width)
71
+ ax.set_xticklabels(
72
+ [
73
+ f"{model_2}\nweight {model_1_weight}",
74
+ f"{model_1}\nweight {model_2_weight}",
75
+ f"{model_3}\nweight {model_3_weight}",
76
+ "VotingClassifier\n(average probabilities)",
77
+ ],
78
+ rotation=40,
79
+ ha="right",
80
+ )
81
+ plt.ylim([0, 1])
82
+ plt.title("Class probabilities for sample 1 by different classifiers")
83
+ plt.legend([p1[0], p2[0]], ["class 1", "class 2"], loc="upper left")
84
+ plt.tight_layout()
85
+ plt.show()
86
+ return fig
87
+
88
+
89
+ with gr.Blocks() as demo:
90
+ with gr.Row():
91
+ model_1 = gr.Dropdown(
92
+ [
93
+ "Logistic Regression",
94
+ "Random Forest",
95
+ "Gaussian Naive Bayes",
96
+ ],
97
+ label="Model 1",
98
+ value="Logistic Regression",
99
+ )
100
+ model_2 = gr.Dropdown(
101
+ [
102
+ "Logistic Regression",
103
+ "Random Forest",
104
+ "Gaussian Naive Bayes",
105
+ ],
106
+ label="Model 2",
107
+ value="Random Forest",
108
+ )
109
+ model_3 = gr.Dropdown(
110
+ [
111
+ "Logistic Regression",
112
+ "Random Forest",
113
+ "Gaussian Naive Bayes",
114
+ ],
115
+ label="Model 3",
116
+ value="Gaussian Naive Bayes",
117
+ )
118
+
119
+ with gr.Row():
120
+ model_1_weight = gr.Slider(
121
+ minimum=1, maximum=10, value=1, label="Model 1 Weight", step=1
122
+ )
123
+ model_2_weight = gr.Slider(
124
+ minimum=1, maximum=10, value=1, label="Model 2 Weight", step=1
125
+ )
126
+ model_3_weight = gr.Slider(
127
+ minimum=1, maximum=10, value=5, label="Model 3 Weight", step=1
128
+ )
129
+
130
+ proba_plots = gr.Plot()
131
+
132
+ model_1.change(
133
+ get_proba_plots,
134
+ [model_1, model_2, model_3, model_1_weight, model_2_weight, model_3_weight],
135
+ proba_plots,
136
+ queue=False,
137
+ )
138
+ model_2.change(
139
+ get_proba_plots,
140
+ [model_1, model_2, model_3, model_1_weight, model_2_weight, model_3_weight],
141
+ proba_plots,
142
+ queue=False,
143
+ )
144
+ model_3.change(
145
+ get_proba_plots,
146
+ [model_1, model_2, model_3, model_1_weight, model_2_weight, model_3_weight],
147
+ proba_plots,
148
+ queue=False,
149
+ )
150
+ model_1_weight.change(
151
+ get_proba_plots,
152
+ [model_1, model_2, model_3, model_1_weight, model_2_weight, model_3_weight],
153
+ proba_plots,
154
+ queue=False,
155
+ )
156
+ model_2_weight.change(
157
+ get_proba_plots,
158
+ [model_1, model_2, model_3, model_1_weight, model_2_weight, model_3_weight],
159
+ proba_plots,
160
+ queue=False,
161
+ )
162
+ model_3_weight.change(
163
+ get_proba_plots,
164
+ [model_1, model_2, model_3, model_1_weight, model_2_weight, model_3_weight],
165
+ proba_plots,
166
+ queue=False,
167
+ )
168
+
169
+ demo.load(
170
+ get_proba_plots,
171
+ [model_1, model_2, model_3, model_1_weight, model_2_weight, model_3_weight],
172
+ proba_plots,
173
+ queue=False,
174
+ )
175
+
176
+ if __name__ == "__main__":
177
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ scikit-learn==1.2.2
2
+ matplotlib==3.7.1