Init commit
Browse files- .gitignore +125 -0
- app.py +94 -0
- 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,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
import numpy as np
|
4 |
+
from sklearn.datasets import make_regression
|
5 |
+
from sklearn.linear_model import Ridge
|
6 |
+
from sklearn.metrics import mean_squared_error
|
7 |
+
|
8 |
+
|
9 |
+
def get_plots(min_alpha, max_alpha):
|
10 |
+
clf = Ridge()
|
11 |
+
|
12 |
+
X, y, w = make_regression(
|
13 |
+
n_samples=10, n_features=10, coef=True, random_state=1, bias=3.5
|
14 |
+
)
|
15 |
+
|
16 |
+
coefs = []
|
17 |
+
errors = []
|
18 |
+
|
19 |
+
alphas = np.logspace(min_alpha, max_alpha, 200)
|
20 |
+
|
21 |
+
# Train the model with different regularisation strengths
|
22 |
+
for a in alphas:
|
23 |
+
clf.set_params(alpha=a)
|
24 |
+
clf.fit(X, y)
|
25 |
+
coefs.append(clf.coef_)
|
26 |
+
errors.append(mean_squared_error(clf.coef_, w))
|
27 |
+
|
28 |
+
# Display results
|
29 |
+
fig, ax = plt.subplots(1, 2, figsize=(20, 6))
|
30 |
+
|
31 |
+
ax[0].plot(alphas, coefs)
|
32 |
+
ax[0].set_xscale("log")
|
33 |
+
ax[0].set_xlabel("alpha")
|
34 |
+
ax[0].set_ylabel("weights")
|
35 |
+
ax[0].set_title("Ridge coefficients as a function of the regularization")
|
36 |
+
|
37 |
+
ax[1].plot(alphas, errors)
|
38 |
+
ax[1].set_xscale("log")
|
39 |
+
ax[1].set_xlabel("alpha")
|
40 |
+
ax[1].set_ylabel("error")
|
41 |
+
ax[1].set_title("Coefficient error as a function of the regularization")
|
42 |
+
fig.tight_layout()
|
43 |
+
|
44 |
+
return fig
|
45 |
+
|
46 |
+
|
47 |
+
with gr.Blocks() as demo:
|
48 |
+
with gr.Row():
|
49 |
+
with gr.Column(scale=1):
|
50 |
+
gr.Markdown(
|
51 |
+
"Choose the range of alpha values to plot."
|
52 |
+
+ " The models you input for alpha are for the exponents of 10,"
|
53 |
+
+ " so a value of -6 means 10^-6."
|
54 |
+
)
|
55 |
+
min_alpha = gr.Slider(
|
56 |
+
minimum=-10,
|
57 |
+
maximum=10,
|
58 |
+
step=0.5,
|
59 |
+
value=-6,
|
60 |
+
label="Minimum Alpha Exponent",
|
61 |
+
)
|
62 |
+
max_alpha = gr.Slider(
|
63 |
+
minimum=-10,
|
64 |
+
maximum=10,
|
65 |
+
step=0.5,
|
66 |
+
value=6,
|
67 |
+
label="Maximum Alpha Exponent",
|
68 |
+
)
|
69 |
+
|
70 |
+
with gr.Column(scale=4):
|
71 |
+
plots = gr.Plot()
|
72 |
+
|
73 |
+
min_alpha.change(
|
74 |
+
get_plots,
|
75 |
+
[min_alpha, max_alpha],
|
76 |
+
plots,
|
77 |
+
queue=False,
|
78 |
+
)
|
79 |
+
max_alpha.change(
|
80 |
+
get_plots,
|
81 |
+
[min_alpha, max_alpha],
|
82 |
+
plots,
|
83 |
+
queue=False,
|
84 |
+
)
|
85 |
+
|
86 |
+
demo.load(
|
87 |
+
get_plots,
|
88 |
+
[min_alpha, max_alpha],
|
89 |
+
plots,
|
90 |
+
queue=False,
|
91 |
+
)
|
92 |
+
|
93 |
+
if __name__ == "__main__":
|
94 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
scikit-learn==1.2.2
|
2 |
+
matplotlib==3.7.1
|