概要
wslでmanimやってみた。
manim見つけたので、やってみた。
参考にしたページ
サンプルコード
from manim import *
import numpy as np
class PCAVisualizer(Scene):
def construct(self):
caption_1 = Text("データ点の作成と表示", font = "IPAGothic", font_size = 24).to_edge(UP)
self.play(Write(caption_1))
self.wait(2)
data = np.array([[2, 1, 0], [1, 3, 0], [3, 4, 0], [4, 3, 0], [3, 2, 0], [2, 4, 0]], dtype = float)
dots = VGroup(*[Circle(radius = 0.07, fill_color = YELLOW, fill_opacity = 1).move_to([x, y, 0]) for x, y, _ in data])
self.play(DrawBorderThenFill(dots))
self.wait()
self.play(FadeOut(caption_1))
caption_2 = Text("主成分の計算と表示", font = "IPAGothic", font_size = 24).to_edge(UP)
self.play(Write(caption_2))
self.wait(2)
mean = np.mean(data, axis = 0)
centered_data = data - mean
cov_matrix = np.cov(centered_data.T)
eigenvalues, eigenvectors = np.linalg.eig(cov_matrix)
pc1 = eigenvectors[:, np.argmax(eigenvalues)][:2]
pc1_line = Line(np.append(pc1 * 3 + mean[:2], 0), np.append(-pc1 * 3 + mean[:2], 0), color = GREEN)
self.play(Create(pc1_line))
self.wait()
self.play(FadeOut(caption_2))
caption_3 = Text("分散の可視化", font = "IPAGothic", font_size = 24).to_edge(UP)
self.play(Write(caption_3))
self.wait(2)
variance_box = Rectangle(height = 1, width = eigenvalues.max() * 2).move_to([pc1[0] * eigenvalues.max() + mean[0], pc1[1] * eigenvalues.max() + mean[1], 0])
self.play(FadeIn(variance_box))
self.wait()
self.play(FadeOut(caption_3))
caption_4 = Text("データ点の主成分への射影", font = "IPAGothic", font_size = 24).to_edge(UP)
self.play(Write(caption_4))
self.wait(2)
projected_data = np.dot(centered_data[:, :2], pc1)
projected_dots = VGroup(*[Dot(np.array([projected, mean[2], 0]), radius = 0.05, color = BLUE) for projected in projected_data])
self.play(ReplacementTransform(dots, projected_dots))
self.wait()
self.play(FadeOut(caption_4))
scene = PCAVisualizer()
scene.render(preview = True)
成果物
以上。