Spaces:
Runtime error
Runtime error
add initial version for the kmeans assumption dashboard
Browse files- .gitignore +160 -0
- README.md +1 -0
- app.py +161 -0
- requirements.txt +2 -0
.gitignore
ADDED
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Byte-compiled / optimized / DLL files
|
2 |
+
__pycache__/
|
3 |
+
*.py[cod]
|
4 |
+
*$py.class
|
5 |
+
|
6 |
+
# C extensions
|
7 |
+
*.so
|
8 |
+
|
9 |
+
# Distribution / packaging
|
10 |
+
.Python
|
11 |
+
build/
|
12 |
+
develop-eggs/
|
13 |
+
dist/
|
14 |
+
downloads/
|
15 |
+
eggs/
|
16 |
+
.eggs/
|
17 |
+
lib/
|
18 |
+
lib64/
|
19 |
+
parts/
|
20 |
+
sdist/
|
21 |
+
var/
|
22 |
+
wheels/
|
23 |
+
share/python-wheels/
|
24 |
+
*.egg-info/
|
25 |
+
.installed.cfg
|
26 |
+
*.egg
|
27 |
+
MANIFEST
|
28 |
+
|
29 |
+
# PyInstaller
|
30 |
+
# Usually these files are written by a python script from a template
|
31 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
32 |
+
*.manifest
|
33 |
+
*.spec
|
34 |
+
|
35 |
+
# Installer logs
|
36 |
+
pip-log.txt
|
37 |
+
pip-delete-this-directory.txt
|
38 |
+
|
39 |
+
# Unit test / coverage reports
|
40 |
+
htmlcov/
|
41 |
+
.tox/
|
42 |
+
.nox/
|
43 |
+
.coverage
|
44 |
+
.coverage.*
|
45 |
+
.cache
|
46 |
+
nosetests.xml
|
47 |
+
coverage.xml
|
48 |
+
*.cover
|
49 |
+
*.py,cover
|
50 |
+
.hypothesis/
|
51 |
+
.pytest_cache/
|
52 |
+
cover/
|
53 |
+
|
54 |
+
# Translations
|
55 |
+
*.mo
|
56 |
+
*.pot
|
57 |
+
|
58 |
+
# Django stuff:
|
59 |
+
*.log
|
60 |
+
local_settings.py
|
61 |
+
db.sqlite3
|
62 |
+
db.sqlite3-journal
|
63 |
+
|
64 |
+
# Flask stuff:
|
65 |
+
instance/
|
66 |
+
.webassets-cache
|
67 |
+
|
68 |
+
# Scrapy stuff:
|
69 |
+
.scrapy
|
70 |
+
|
71 |
+
# Sphinx documentation
|
72 |
+
docs/_build/
|
73 |
+
|
74 |
+
# PyBuilder
|
75 |
+
.pybuilder/
|
76 |
+
target/
|
77 |
+
|
78 |
+
# Jupyter Notebook
|
79 |
+
.ipynb_checkpoints
|
80 |
+
|
81 |
+
# IPython
|
82 |
+
profile_default/
|
83 |
+
ipython_config.py
|
84 |
+
|
85 |
+
# pyenv
|
86 |
+
# For a library or package, you might want to ignore these files since the code is
|
87 |
+
# intended to run in multiple environments; otherwise, check them in:
|
88 |
+
# .python-version
|
89 |
+
|
90 |
+
# pipenv
|
91 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
92 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
93 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
94 |
+
# install all needed dependencies.
|
95 |
+
#Pipfile.lock
|
96 |
+
|
97 |
+
# poetry
|
98 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
99 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
100 |
+
# commonly ignored for libraries.
|
101 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
102 |
+
#poetry.lock
|
103 |
+
|
104 |
+
# pdm
|
105 |
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
106 |
+
#pdm.lock
|
107 |
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
108 |
+
# in version control.
|
109 |
+
# https://pdm.fming.dev/#use-with-ide
|
110 |
+
.pdm.toml
|
111 |
+
|
112 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
113 |
+
__pypackages__/
|
114 |
+
|
115 |
+
# Celery stuff
|
116 |
+
celerybeat-schedule
|
117 |
+
celerybeat.pid
|
118 |
+
|
119 |
+
# SageMath parsed files
|
120 |
+
*.sage.py
|
121 |
+
|
122 |
+
# Environments
|
123 |
+
.env
|
124 |
+
.venv
|
125 |
+
env/
|
126 |
+
venv/
|
127 |
+
ENV/
|
128 |
+
env.bak/
|
129 |
+
venv.bak/
|
130 |
+
|
131 |
+
# Spyder project settings
|
132 |
+
.spyderproject
|
133 |
+
.spyproject
|
134 |
+
|
135 |
+
# Rope project settings
|
136 |
+
.ropeproject
|
137 |
+
|
138 |
+
# mkdocs documentation
|
139 |
+
/site
|
140 |
+
|
141 |
+
# mypy
|
142 |
+
.mypy_cache/
|
143 |
+
.dmypy.json
|
144 |
+
dmypy.json
|
145 |
+
|
146 |
+
# Pyre type checker
|
147 |
+
.pyre/
|
148 |
+
|
149 |
+
# pytype static type analyzer
|
150 |
+
.pytype/
|
151 |
+
|
152 |
+
# Cython debug symbols
|
153 |
+
cython_debug/
|
154 |
+
|
155 |
+
# PyCharm
|
156 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
157 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
158 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
159 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
160 |
+
#.idea/
|
README.md
CHANGED
@@ -9,5 +9,6 @@ app_file: app.py
|
|
9 |
pinned: false
|
10 |
license: apache-2.0
|
11 |
---
|
|
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
9 |
pinned: false
|
10 |
license: apache-2.0
|
11 |
---
|
12 |
+
This dashboard is a live demonstration of the sklearn document at https://scikit-learn.org/stable/auto_examples/cluster/plot_kmeans_assumptions.html#sphx-glr-auto-examples-cluster-plot-kmeans-assumptions-py
|
13 |
|
14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""This dashboard is a live demonstration of the sklearn document at
|
2 |
+
https://scikit-learn.org/stable/auto_examples/cluster/plot_kmeans_assumptions.html#sphx-glr-auto-examples-cluster-plot-kmeans-assumptions-py
|
3 |
+
"""
|
4 |
+
import numpy as np
|
5 |
+
import typing as tp
|
6 |
+
import pandas as pd
|
7 |
+
import gradio as gr
|
8 |
+
from sklearn.datasets import make_blobs
|
9 |
+
from sklearn.cluster import KMeans
|
10 |
+
import matplotlib.pyplot as plt
|
11 |
+
|
12 |
+
title = "Demonstration of k-means assumptions"
|
13 |
+
random_state = 170
|
14 |
+
transformation = [[0.60834549, -0.63667341], [-0.40887718, 0.85253229]]
|
15 |
+
|
16 |
+
# Defines 4 Apps for each demo senario
|
17 |
+
class App:
|
18 |
+
name: tp.ClassVar[str]
|
19 |
+
description: tp.ClassVar[str]
|
20 |
+
|
21 |
+
def make_data(self, n_samples: int) -> tp.Tuple[np.ndarray, np.ndarray]:
|
22 |
+
raise NotImplementedError()
|
23 |
+
|
24 |
+
def kmeans_predict(self, n_cluster: int, X: np.ndarray) -> np.ndarray:
|
25 |
+
raise NotImplementedError()
|
26 |
+
|
27 |
+
class MixGaussianBlobs(App):
|
28 |
+
name = "Mixture of Gaussian Blobs"
|
29 |
+
description = (
|
30 |
+
"In a real setting there is no uniquely defined true number of clusters. "
|
31 |
+
"An appropriate number of clusters has to be decided from data-based criteria"
|
32 |
+
" and knowledge of the intended goal."
|
33 |
+
)
|
34 |
+
|
35 |
+
def make_data(self, n_samples):
|
36 |
+
return make_blobs(n_samples=n_samples, random_state=random_state)
|
37 |
+
|
38 |
+
def kmeans_predict(self, n_clusters, X):
|
39 |
+
return KMeans(
|
40 |
+
n_clusters=n_clusters, n_init="auto", random_state=random_state
|
41 |
+
).fit_predict(X)
|
42 |
+
|
43 |
+
|
44 |
+
class AnisoDistBlobs(MixGaussianBlobs):
|
45 |
+
name = "Anisotropically Distributed Blobs"
|
46 |
+
description = (
|
47 |
+
"k-means consists of minimizing sample’s euclidean distances to the centroid of the"
|
48 |
+
" cluster they are assigned to. As a consequence, k-means is more appropriate for "
|
49 |
+
"clusters that are isotropic and normally distributed (i.e. spherical gaussians)"
|
50 |
+
)
|
51 |
+
|
52 |
+
def make_data(self, n_samples):
|
53 |
+
X, y = super().make_data(n_samples=n_samples)
|
54 |
+
X = np.dot(X, transformation)
|
55 |
+
return X, y
|
56 |
+
|
57 |
+
|
58 |
+
class UnequalVariance(MixGaussianBlobs):
|
59 |
+
name = "Unequal Variance"
|
60 |
+
description = (
|
61 |
+
"k-means is equivalent to taking the maximum likelihood estimator for a 'mixture' "
|
62 |
+
"of k gaussian distributions with the same variances but with possibly different "
|
63 |
+
" means."
|
64 |
+
)
|
65 |
+
|
66 |
+
def make_data(self, n_samples):
|
67 |
+
return make_blobs(
|
68 |
+
n_samples=n_samples, cluster_std=[1.0, 2.5, 0.5], random_state=random_state
|
69 |
+
)
|
70 |
+
|
71 |
+
|
72 |
+
class UnevenlySizedBlobs(MixGaussianBlobs):
|
73 |
+
name = "Unevenly Sized Blobs"
|
74 |
+
description = (
|
75 |
+
"There is no theoretical result about k-means that states that it requires similar"
|
76 |
+
" cluster sizes to perform well, yet minimizing euclidean distances does mean that"
|
77 |
+
" the more sparse and high-dimensional the problem is, the higher is the need to run "
|
78 |
+
"the algorithm with different centroid seeds to ensure a global minimal inertia."
|
79 |
+
)
|
80 |
+
|
81 |
+
def make_data(self, n_samples):
|
82 |
+
X, y = super().make_data(n_samples=n_samples)
|
83 |
+
X_filter = np.vstack(
|
84 |
+
(
|
85 |
+
X[y == 0][:500],
|
86 |
+
X[y == 1][:100],
|
87 |
+
X[y == 2][:10],
|
88 |
+
)
|
89 |
+
)
|
90 |
+
# print(len(X_filter[:, 0]))
|
91 |
+
# print(len(X_filter[:, 1]))
|
92 |
+
y_filter = [0] * 500 + [1] * 100 + [2] * 10
|
93 |
+
return X_filter, y_filter
|
94 |
+
|
95 |
+
|
96 |
+
# Define instances of the apps
|
97 |
+
_apps = [
|
98 |
+
MixGaussianBlobs(),
|
99 |
+
AnisoDistBlobs(),
|
100 |
+
UnequalVariance(),
|
101 |
+
UnevenlySizedBlobs(),
|
102 |
+
]
|
103 |
+
apps = {k.name: k for k in _apps}
|
104 |
+
data_choices = [k.name for k in _apps]
|
105 |
+
|
106 |
+
|
107 |
+
# Define the callback to the triggered when a button or a slider used by the user.
|
108 |
+
def fn(data_choice, n_samples, n_clusters):
|
109 |
+
# Find the app and create sample data based on the user choice.
|
110 |
+
app = apps[data_choice]
|
111 |
+
X, y = app.make_data(n_samples)
|
112 |
+
fig_sample, ax_sample = plt.subplots()
|
113 |
+
ax_sample.set_title(app.name)
|
114 |
+
|
115 |
+
# Execute the KMeans clustering.
|
116 |
+
y_pred = app.kmeans_predict(n_clusters, X)
|
117 |
+
ax_sample.scatter(X[:, 0], X[:, 1], c=y)
|
118 |
+
fig_pred, ax_pred = plt.subplots()
|
119 |
+
ax_pred.scatter(X[:, 0], X[:, 1], c=y_pred)
|
120 |
+
ax_pred.set_title(f"Unexpected KMeans Clusters (n_cluster={n_clusters})")
|
121 |
+
|
122 |
+
return f"## {app.description}", fig_sample, fig_pred
|
123 |
+
|
124 |
+
|
125 |
+
# Define the dashboard layout and buttons
|
126 |
+
with gr.Blocks(title=title) as demo:
|
127 |
+
gr.Markdown(f"# {title}")
|
128 |
+
with gr.Row():
|
129 |
+
data_choice = gr.Radio(
|
130 |
+
choices=data_choices,
|
131 |
+
value=data_choices[0],
|
132 |
+
)
|
133 |
+
with gr.Row():
|
134 |
+
n_samples = gr.Slider(
|
135 |
+
minimum=1500, maximum=3000, step=50, label="Number of Samples"
|
136 |
+
)
|
137 |
+
n_clusters = gr.Slider(minimum=2, maximum=8, step=1, label="Number of Clusters")
|
138 |
+
with gr.Accordion("Description"):
|
139 |
+
description = gr.Markdown(label="Description")
|
140 |
+
with gr.Row():
|
141 |
+
plot_sample = gr.Plot(label="Ground Truth Cluster")
|
142 |
+
plot_kmeans = gr.Plot(label="Unexpected KMeans Cluster")
|
143 |
+
|
144 |
+
data_choice.change(
|
145 |
+
fn=fn,
|
146 |
+
inputs=[data_choice, n_samples, n_clusters],
|
147 |
+
outputs=[description, plot_sample, plot_kmeans],
|
148 |
+
)
|
149 |
+
n_samples.change(
|
150 |
+
fn=fn,
|
151 |
+
inputs=[data_choice, n_samples, n_clusters],
|
152 |
+
outputs=[description, plot_sample, plot_kmeans],
|
153 |
+
)
|
154 |
+
n_clusters.change(
|
155 |
+
fn=fn,
|
156 |
+
inputs=[data_choice, n_samples, n_clusters],
|
157 |
+
outputs=[description, plot_sample, plot_kmeans],
|
158 |
+
)
|
159 |
+
|
160 |
+
|
161 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
scikit-learn==1.2.2
|
2 |
+
matplotlib==3.7.1
|