Skip to content

Instantly share code, notes, and snippets.

@audy
Forked from amueller/digits_video.py
Created October 11, 2012 19:50
Show Gist options
  • Save audy/3875060 to your computer and use it in GitHub Desktop.
Save audy/3875060 to your computer and use it in GitHub Desktop.
Visualization of iris and digits datasets via random projections
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from sklearn.datasets import load_iris
from sklearn.preprocessing import StandardScaler
iris = load_iris()
X, y = iris.data, iris.target
X_pca = StandardScaler().fit_transform(X)
fig = plt.figure()
colors = plt.cm.Spectral(y * 255 / y.max())
n_iter = 200
points = [plt.plot([], [], 'o', color=['r', 'g', 'b'][i])[0]
for i in np.unique(y)]
def init():
global points
for p in points:
p.set_data([], [])
plt.xlim((-4, 4))
plt.ylim((-3, 3))
plt.xticks(())
plt.yticks(())
return points
def animate(i):
global points
alpha = 2 * np.pi * i / n_iter
beta = 4 * np.pi * i / n_iter
interpolation1 = np.cos(alpha) * X_pca[:, 1] + np.sin(alpha) * X_pca[:, 2]
interpolation2 = np.cos(beta) * X_pca[:, 0] + np.sin(beta) * X_pca[:, 3]
for p, c in zip(points, np.unique(y)):
p.set_data(interpolation1[y == c], interpolation2[y == c])
return points
anim = FuncAnimation(fig, animate, frames=n_iter, interval=100, blit=True,
init_func=init)
#anim.save("iris.mp4", fps=20, extra_args=['-vcodec', 'libx264'])
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment