-
Notifications
You must be signed in to change notification settings - Fork 52
add kmeans clustering example #734
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
76322a6
add kmeans clustering example
clewis7 be9a311
update conf
clewis7 7c13050
switch to tool tip
clewis7 713f102
switch to linear interp and 3D camera for kmeans
clewis7 d073c66
increase timeout for deploy docs connection
kushalkolar e948145
increase log level
kushalkolar dc2bb2c
requested changes
clewis7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
""" | ||
K-Means Clustering of MNIST Dataset | ||
=================================== | ||
|
||
Example showing how you can perform K-Means clustering on the MNIST dataset. | ||
""" | ||
|
||
# test_example = false | ||
# sphinx_gallery_pygfx_docs = 'screenshot' | ||
|
||
import fastplotlib as fpl | ||
import numpy as np | ||
from sklearn.datasets import load_digits | ||
from sklearn.cluster import KMeans | ||
from sklearn.decomposition import PCA | ||
|
||
# load the data | ||
mnist = load_digits() | ||
|
||
# get the data and labels | ||
data = mnist['data'] # (1797, 64) | ||
labels = mnist['target'] # (1797,) | ||
|
||
# visualize the first 5 digits | ||
# NOTE: this is just to give a sense of the dataset if you are unfamiliar, | ||
# the more interesting visualization is below :D | ||
fig_data = fpl.Figure(shape=(1, 5), size=(900, 300)) | ||
|
||
# iterate through each subplot | ||
for i, subplot in enumerate(fig_data): | ||
# reshape each image to (8, 8) | ||
subplot.add_image(data[i].reshape(8,8), cmap="gray", interpolation="linear") | ||
# add the label as a title | ||
subplot.set_title(f"Label: {labels[i]}") | ||
# turn off the axes and toolbar | ||
subplot.axes.visible = False | ||
subplot.toolbar = False | ||
|
||
fig_data.show() | ||
|
||
# project the data from 64 dimensions down to the number of unique digits | ||
n_digits = len(np.unique(labels)) # 10 | ||
|
||
reduced_data = PCA(n_components=n_digits).fit_transform(data) # (1797, 10) | ||
|
||
# performs K-Means clustering, take the best of 4 runs | ||
kmeans = KMeans(n_clusters=n_digits, n_init=4) | ||
# fit the lower-dimension data | ||
kmeans.fit(reduced_data) | ||
|
||
# get the centroids (center of the clusters) | ||
centroids = kmeans.cluster_centers_ | ||
|
||
# plot the kmeans result and corresponding original image | ||
figure = fpl.Figure( | ||
shape=(1,2), | ||
size=(700, 400), | ||
cameras=["3d", "2d"], | ||
controller_types=[["fly", "panzoom"]] | ||
) | ||
|
||
# set the axes to False | ||
figure[0, 0].axes.visible = False | ||
figure[0, 1].axes.visible = False | ||
|
||
figure[0, 0].set_title(f"K-means clustering of PCA-reduced data") | ||
|
||
# plot the centroids | ||
figure[0, 0].add_scatter( | ||
data=np.vstack((centroids[:, 0], centroids[:, 1], centroids[:, 2])).T, | ||
clewis7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
colors="white", | ||
sizes=15 | ||
) | ||
# plot the down-projected data | ||
digit_scatter = figure[0,0].add_scatter( | ||
data=np.vstack((reduced_data[:, 0], reduced_data[:, 1], reduced_data[:, 2])).T, | ||
clewis7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sizes=5, | ||
cmap="tab10", # use a qualitative cmap | ||
cmap_transform=kmeans.labels_, # color by the predicted cluster | ||
) | ||
|
||
# initial index | ||
ix = 0 | ||
|
||
# plot the initial image | ||
digit_img = figure[0, 1].add_image(data[ix].reshape(8,8), cmap="gray", name="digit", interpolation="linear") | ||
clewis7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# change the color and size of the initial selected data point | ||
digit_scatter.colors[ix] = "magenta" | ||
digit_scatter.sizes[ix] = 10 | ||
|
||
# define event handler to update the selected data point | ||
@digit_scatter.add_event_handler("pointer_enter") | ||
def update(ev): | ||
# reset colors and sizes | ||
digit_scatter.cmap = "tab10" | ||
digit_scatter.sizes = 5 | ||
|
||
# update with new seleciton | ||
ix = ev.pick_info["vertex_index"] | ||
|
||
digit_scatter.colors[ix] = "magenta" | ||
digit_scatter.sizes[ix] = 10 | ||
|
||
# update digit fig | ||
figure[0, 1]["digit"].data = data[ix].reshape(8, 8) | ||
|
||
figure.show() | ||
|
||
# NOTE: `if __name__ == "__main__"` is NOT how to use fastplotlib interactively | ||
# please see our docs for using fastplotlib interactively in ipython and jupyter | ||
if __name__ == "__main__": | ||
print(__doc__) | ||
fpl.loop.run() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.