0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

wslでmanim その8

Posted at

概要

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)





成果物

PCAVisualizer_ManimCE_v0.18.1.gif

以上。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?