Skip to content

Instantly share code, notes, and snippets.

@audy
Forked from amueller/digits_video.py
Created October 11, 2012 19:50

Revisions

  1. @amueller amueller revised this gist Oct 5, 2012. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions digits_video.py
    Original file line number Diff line number Diff line change
    @@ -71,8 +71,7 @@ def make_video(X, y=None, frames=500, filename="video.mp4"):
    anim = FuncAnimation(fig, projector.animate, frames=frames, interval=100,
    blit=True, init_func=projector.init_figure)

    anim.save(filename, fps=20,
    extra_args=['-vcodec', 'libx264'])
    #anim.save(filename, fps=20, extra_args=['-vcodec', 'libx264'])

    plt.show()

  2. @amueller amueller revised this gist Oct 5, 2012. 2 changed files with 50 additions and 50 deletions.
    8 changes: 4 additions & 4 deletions digits_video.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # (c) 2012 Andreas Mueller [email protected]
    # (c) 2012 Andreas Mueller [email protected]
    # License: BSD 2-Clause
    #
    # See my blog for details: http://peekaboo-vision.blogspot.com
    @@ -44,8 +44,8 @@ def __init__(self, X, y, frames=200):
    def init_figure(self):
    for p in self.points:
    p.set_data([], [])
    plt.xlim((-2, 2))
    plt.ylim((-2, 2))
    plt.xlim((-2, 2))
    plt.ylim((-2, 2))
    plt.xticks(())
    plt.yticks(())
    return self.points
    @@ -64,6 +64,7 @@ def animate(self, i):
    p.set_data(interpolation[y == c, 0], interpolation[y == c, 1])
    return self.points


    def make_video(X, y=None, frames=500, filename="video.mp4"):
    fig = plt.figure()
    projector = RandomProjector(X, y, frames)
    @@ -100,4 +101,3 @@ def make_video(X, y=None, frames=500, filename="video.mp4"):
    # PCA here optional. Also try without.
    X = PCA().fit_transform(X)
    make_video(X, y, filename='digits_two_classes.mp4', frames=1000)

    92 changes: 46 additions & 46 deletions iris_video.py
    Original file line number Diff line number Diff line change
    @@ -1,47 +1,47 @@
    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'])
    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()
  3. @amueller amueller revised this gist Oct 5, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion digits_video.py
    Original file line number Diff line number Diff line change
    @@ -70,7 +70,7 @@ def make_video(X, y=None, frames=500, filename="video.mp4"):
    anim = FuncAnimation(fig, projector.animate, frames=frames, interval=100,
    blit=True, init_func=projector.init_figure)

    anim.save(filename, fps=30,
    anim.save(filename, fps=20,
    extra_args=['-vcodec', 'libx264'])

    plt.show()
  4. @amueller amueller revised this gist Oct 5, 2012. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions digits_video.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,8 @@
    # (c) 2012 Andreas Mueller [email protected]
    # License: BSD 2-Clause
    #
    # See my blog for details: http://peekaboo-vision.blogspot.com

    import numpy as np

    import matplotlib.pyplot as plt
  5. @amueller amueller revised this gist Oct 5, 2012. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions digits_video.py
    Original file line number Diff line number Diff line change
    @@ -92,4 +92,7 @@ def make_video(X, y=None, frames=500, filename="video.mp4"):
    label=c)
    plt.legend()
    plt.savefig("digits_pca.png", bbox_inches="tight")
    # PCA here optional. Also try without.
    X = PCA().fit_transform(X)
    make_video(X, y, filename='digits_two_classes.mp4', frames=1000)

  6. @amueller amueller revised this gist Oct 5, 2012. 1 changed file with 95 additions and 0 deletions.
    95 changes: 95 additions & 0 deletions digits_video.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,95 @@
    import numpy as np

    import matplotlib.pyplot as plt
    from matplotlib.animation import FuncAnimation

    from sklearn.datasets import load_digits
    from sklearn.preprocessing import StandardScaler
    from sklearn.decomposition import PCA


    class RandomProjector(object):
    def __init__(self, X, y, frames=200):
    self.X = X
    self.y = y
    self.frames = frames

    if y is not None:
    # Spectral has 255 colors (I think)
    num_classes = len(np.unique(y))
    colors = plt.cm.Spectral(np.arange(num_classes)
    * 255 / num_classes)
    else:
    num_classes = 1
    colors = 'b'

    self.points = [plt.plot([], [], 'o', color=colors[i])[0]
    for i in range(num_classes)]

    n_features = X.shape[1]

    # initialize projection matrix
    self.projection = np.zeros((n_features, 2))

    # rest is n_features - 2 large
    size = n_features - 2
    self.frequencies = 10 + np.random.randint(10, size=(size, 2))
    self.phases = np.random.uniform(size=(size, 2))

    def init_figure(self):
    for p in self.points:
    p.set_data([], [])
    plt.xlim((-2, 2))
    plt.ylim((-2, 2))
    plt.xticks(())
    plt.yticks(())
    return self.points

    def animate(self, i):
    # set top 2x2 to identity
    self.projection[0, 0] = 1
    self.projection[1, 1] = 1
    # set "free entries" of projection matrix
    # gives them a "rotation" feel and makes the whole thing seamless.
    scale = 2 * np.pi * i / self.frames
    self.projection[2:, :] = np.sin(self.frequencies * scale + self.phases)
    interpolation = np.dot(X, self.projection)
    interpolation /= interpolation.max(axis=0)
    for p, c in zip(self.points, np.unique(y)):
    p.set_data(interpolation[y == c, 0], interpolation[y == c, 1])
    return self.points

    def make_video(X, y=None, frames=500, filename="video.mp4"):
    fig = plt.figure()
    projector = RandomProjector(X, y, frames)
    anim = FuncAnimation(fig, projector.animate, frames=frames, interval=100,
    blit=True, init_func=projector.init_figure)

    anim.save(filename, fps=30,
    extra_args=['-vcodec', 'libx264'])

    plt.show()


    if __name__ == "__main__":
    #iris = load_iris()
    iris = load_digits()
    X, y = iris.data, iris.target

    mask = (y == 1) + (y == 2) + (y == 7)
    y = y[mask]
    X = X[mask]

    # we should at least remove the mean
    X = StandardScaler(with_std=False).fit_transform(X)

    # make boring PCA visualization for comparison
    num_classes = len(np.unique(y))
    colors = plt.cm.Spectral(np.arange(num_classes) * 255 / num_classes)
    X_pca = PCA(n_components=2).fit_transform(X)
    for i, c in enumerate(np.unique(y)):
    plt.plot(X_pca[y == c, 0], X_pca[y == c, 1], 'o', color=colors[i],
    label=c)
    plt.legend()
    plt.savefig("digits_pca.png", bbox_inches="tight")
    X = PCA().fit_transform(X)
  7. @amueller amueller created this gist Oct 5, 2012.
    47 changes: 47 additions & 0 deletions iris_video.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    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()