-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from longyangking/main
Add interactive PCA and Kmeans
- Loading branch information
Showing
9 changed files
with
525 additions
and
41 deletions.
There are no files selected for viewing
This file contains 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,18 @@ | ||
import sys | ||
sys.path.append('..') | ||
|
||
import numpy as np | ||
from vidar import interactive_kmeans | ||
|
||
rng = np.random.RandomState(0) | ||
n_samples = 1000 | ||
cov = [[0.4, 0], [0, 0.4]] | ||
X = np.concatenate([ | ||
rng.multivariate_normal(mean=[-2, 0], cov=cov, size=n_samples), | ||
rng.multivariate_normal(mean=[2, 0], cov=cov, size=n_samples), | ||
rng.multivariate_normal(mean=[0.3, 1], cov=cov, size=n_samples) | ||
]) | ||
|
||
n_clusters = 2 | ||
app = interactive_kmeans(X, n_clusters) | ||
app.show() |
This file contains 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,17 @@ | ||
import sys | ||
sys.path.append('..') | ||
|
||
import numpy as np | ||
from vidar import interactive_PCA | ||
|
||
rng = np.random.RandomState(0) | ||
n_samples = 1000 | ||
cov = [[1, 0], [0, 1]] | ||
X = np.concatenate([ | ||
rng.multivariate_normal(mean=[-2, 0], cov=cov, size=n_samples), | ||
rng.multivariate_normal(mean=[2, 0], cov=cov, size=n_samples)]) | ||
|
||
|
||
n_components = 1 | ||
app = interactive_PCA(X, n_components) | ||
app.show() |
130 changes: 130 additions & 0 deletions
130
notebooks/yang/Kmeans interactive _ more clusters.ipynb
This file contains 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,130 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# K-means interactive\n", | ||
"\n", | ||
"> Yang" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%matplotlib qt\n", | ||
"import matplotlib.pyplot as plt\n", | ||
"import numpy as np\n", | ||
"from matplotlib.widgets import Button\n", | ||
"from matplotlib.widgets import PolygonSelector\n", | ||
"from sklearn.cluster import KMeans\n", | ||
"\n", | ||
"def colors_from_lbs(lbs, colors=None):\n", | ||
" mpl_20 = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd',\n", | ||
" '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf',\n", | ||
" '#3397dc', '#ff993e', '#3fca3f', '#df5152', '#a985ca',\n", | ||
" '#ad7165', '#e992ce', '#999999', '#dbdc3c', '#35d8e9']\n", | ||
" \n", | ||
" if colors is None:\n", | ||
" colors = np.array(mpl_20)\n", | ||
" else:\n", | ||
" colors = np.array(colors)\n", | ||
" lbs = np.array(lbs) % len(colors)\n", | ||
" return colors[lbs]\n", | ||
"\n", | ||
"rng = np.random.RandomState(0)\n", | ||
"n_samples = 1000\n", | ||
"cov = [[0.4, 0], [0, 0.4]]\n", | ||
"X = np.concatenate([\n", | ||
" rng.multivariate_normal(mean=[-2, 0], cov=cov, size=n_samples), \n", | ||
" rng.multivariate_normal(mean=[2, 0], cov=cov, size=n_samples),\n", | ||
" rng.multivariate_normal(mean=[0.5, 1], cov=cov, size=n_samples)\n", | ||
" ])\n", | ||
"\n", | ||
"n_clusters=3\n", | ||
"kmeans = KMeans(n_clusters=n_clusters, random_state=0, n_init=\"auto\")\n", | ||
"labels = kmeans.fit_predict(X)\n", | ||
"\n", | ||
"centers = kmeans.cluster_centers_\n", | ||
"\n", | ||
"fig, (ax_orig, ax_redim) = plt.subplots(1, 2, figsize=(12, 6))\n", | ||
"\n", | ||
"def plot_figure(axe_list, X, centers):\n", | ||
" ax_orig, ax_redim = axe_list\n", | ||
"\n", | ||
" kmeans.cluster_centers_ = np.array(centers, dtype=np.float64)\n", | ||
" labels = kmeans.predict(X) \n", | ||
"\n", | ||
" ax_orig.clear()\n", | ||
" ax_orig.scatter(X[:, 0], X[:, 1], alpha=0.3, label=\"samples\", c=colors_from_lbs(labels))\n", | ||
" ax_orig.scatter(centers[:,0], centers[:,1], s=50, c='black', edgecolors='r')\n", | ||
" ax_orig.set(\n", | ||
" aspect=\"auto\", \n", | ||
" title=\"Interactive K-means\",\n", | ||
" xlabel=\"first feature\",\n", | ||
" ylabel=\"second feature\",\n", | ||
" )\n", | ||
"\n", | ||
" ax_redim.clear()\n", | ||
" class_name = ['class {0}'.format(i+1) for i in range(len(centers))]\n", | ||
"\n", | ||
" # update labels\n", | ||
" counts = [np.sum(labels==i) for i in range(len(centers))]\n", | ||
" \n", | ||
"\n", | ||
" ax_redim.bar(class_name, counts, label=class_name, color=colors_from_lbs(range(len(centers))))\n", | ||
" ax_redim.set(\n", | ||
" aspect=\"auto\",\n", | ||
" title=\"Clustering results\",\n", | ||
" xlabel=\"Main feature\",\n", | ||
" ylabel=\"Number of samples\",\n", | ||
" )\n", | ||
" fig.canvas.draw_idle()\n", | ||
"\n", | ||
"plot_figure((ax_orig, ax_redim), X, centers)\n", | ||
"\n", | ||
"def onselect(verts):\n", | ||
" centers = np.array(verts)\n", | ||
" plot_figure((ax_orig, ax_redim), X, centers)\n", | ||
"\n", | ||
"selector = PolygonSelector(ax_orig, onselect=onselect, \n", | ||
" props=dict(color='r', linestyle='', linewidth=3, alpha=0.6, label=f\"Component\"))\n", | ||
"selector.verts = centers\n", | ||
"\n", | ||
"plt.tight_layout()\n", | ||
"plt.show()\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "base", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.9.19" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains 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,124 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# K-means interactive\n", | ||
"\n", | ||
"> Yang" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 16, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%matplotlib qt\n", | ||
"import matplotlib.pyplot as plt\n", | ||
"import numpy as np\n", | ||
"from matplotlib.widgets import Button\n", | ||
"from matplotlib.widgets import PolygonSelector\n", | ||
"from sklearn.cluster import KMeans\n", | ||
"\n", | ||
"def colors_from_lbs(lbs, colors=None):\n", | ||
" mpl_20 = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd',\n", | ||
" '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf',\n", | ||
" '#3397dc', '#ff993e', '#3fca3f', '#df5152', '#a985ca',\n", | ||
" '#ad7165', '#e992ce', '#999999', '#dbdc3c', '#35d8e9']\n", | ||
" \n", | ||
" if colors is None:\n", | ||
" colors = np.array(mpl_20)\n", | ||
" else:\n", | ||
" colors = np.array(colors)\n", | ||
" lbs = np.array(lbs) % len(colors)\n", | ||
" return colors[lbs]\n", | ||
"\n", | ||
"rng = np.random.RandomState(0)\n", | ||
"n_samples = 1000\n", | ||
"cov = [[0.4, 0], [0, 0.4]]\n", | ||
"X = np.concatenate([\n", | ||
" rng.multivariate_normal(mean=[-2, 0], cov=cov, size=n_samples), \n", | ||
" rng.multivariate_normal(mean=[2, 0], cov=cov, size=n_samples),\n", | ||
" rng.multivariate_normal(mean=[0.3, 1], cov=cov, size=n_samples)\n", | ||
" ])\n", | ||
"\n", | ||
"kmeans = KMeans(n_clusters=2, random_state=0, n_init=\"auto\")\n", | ||
"labels = kmeans.fit_predict(X)\n", | ||
"\n", | ||
"centers = kmeans.cluster_centers_\n", | ||
"\n", | ||
"fig, (ax_orig, ax_redim) = plt.subplots(1, 2, figsize=(12, 6))\n", | ||
"\n", | ||
"def plot_figure(axe_list, X, centers):\n", | ||
" ax_orig, ax_redim = axe_list\n", | ||
"\n", | ||
" kmeans.cluster_centers_ = np.array(centers, dtype=np.float64)\n", | ||
" labels = kmeans.predict(X) \n", | ||
"\n", | ||
" ax_orig.clear()\n", | ||
" ax_orig.scatter(X[:, 0], X[:, 1], alpha=0.3, label=\"samples\", c=colors_from_lbs(labels))\n", | ||
" ax_orig.scatter(centers[:,0], centers[:,1], s=50, c='black', edgecolors='r')\n", | ||
" ax_orig.set(\n", | ||
" aspect=\"auto\", \n", | ||
" title=\"Interactive K-means\",\n", | ||
" xlabel=\"first feature\",\n", | ||
" ylabel=\"second feature\",\n", | ||
" )\n", | ||
"\n", | ||
" ax_redim.clear()\n", | ||
" class_name = ['class {0}'.format(i+1) for i in range(len(centers))]\n", | ||
"\n", | ||
" # update labels\n", | ||
" counts = [np.sum(labels==i) for i in range(len(centers))]\n", | ||
" \n", | ||
"\n", | ||
" ax_redim.bar(class_name, counts, \n", | ||
" label=class_name,\n", | ||
" color=colors_from_lbs(range(len(centers))))\n", | ||
" ax_redim.set(\n", | ||
" aspect=\"auto\",\n", | ||
" title=\"Clustering results\",\n", | ||
" xlabel=\"Main feature\",\n", | ||
" ylabel=\"Number of samples\",\n", | ||
" )\n", | ||
" fig.canvas.draw_idle()\n", | ||
"\n", | ||
"plot_figure((ax_orig, ax_redim), X, centers)\n", | ||
"\n", | ||
"def onselect(verts):\n", | ||
" centers = np.array(verts)\n", | ||
" plot_figure((ax_orig, ax_redim), X, centers)\n", | ||
"\n", | ||
"selector = PolygonSelector(ax_orig, onselect=onselect, \n", | ||
" props=dict(color='r', linestyle='', linewidth=3, alpha=0.6, label=f\"Component\"))\n", | ||
"selector.verts = centers\n", | ||
"\n", | ||
"plt.tight_layout()\n", | ||
"plt.show()\n" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "base", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.9.19" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains 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
Oops, something went wrong.