线性回归 Manim 演示

Linear Regression Overview

https://youtube.com/watch?v=3vl17ymkODg

机器学习算法如果能向 3B1B 一样展示,必能加深对算法的理解。

准备做一些视频展示,可以直接调用 sklearn,当然也可以自己写一套算法,为了了解原理,还是直接调用 sklearn等成熟的算法库好了(自己写有点慢,把代码看懂还是有必要的)。

Manim 非官方文档:https://elteoremadebeethoven.github.io/manim_3feb_docs.github.io/html/index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

Manim

文件的执行

保存为视频并立即播放

  • -p, --preview Automatically open the saved file once its done

保留帧

  • -s, --save_last_frame Save the last frame

视频尺寸

  • -r RESOLUTION, --resolution RESOLUTION Resolution, passed as "height,width"

视频通道

  • -t, --transparent Render to a movie file with an alpha channel

保留进度显示条

  • --leave_progress_bars Leave progress bars displayed in terminal

从某一个动画状态(animation)开始存为视频

  • -n START_AT_ANIMATION_NUMBER, --start_at_animation_number START_AT_ANIMATION_NUMBER

Gif

  • -i, --save_as_gif Save the video as gif

显示操作

基本步骤

位置相关的函数

  • to_edge()

    • 0.1RIGHT+0.2UP+LEFT+DOWN
  • to_corner()

    • UR,UL,DR,DL
  • move_to()

    • 另一个 Obj 位置

    • 在当前位置移动到对应的位置

  • next_to()

    • someObject1对于someObject2的相对位置
  • shift()

    • 自己垂直方向平移
  • rotate()

    • 逆时针旋转
  • flip()

    • 按照矢量量方向翻转180度,遵循右手定则

播放动画

  • play()

    • 多线程的

    • 显示动画函数

      • fading.py 渐变效果

        • FadeIn

        • FadeInFromDown

        • FadeOutAndShiftDown

        • FadeInFromPoint

        • FadeInFromLarge

      • creation.py 书写效果

        • ShowCreation

        • UnCreate

          • 倒放ShowCreation动画效果
        • DrawBorderThenFill

          • 画出边界然后填充
        • Write

          • 从左往右逐渐显示的效果
      • transform.py 渐换

        • Transform

          • 永远是 1 个
        • ReplacementTransform

          • copy
      • growing.py

        • GrowFromCenter

        • GrowFromPoint

        • GrowFromEdge

        • GrowArrow

      • update.py

        • UpdateFromFunc

          • 同步播放
    • runtime

      • 动画的快慢
    • rate_func

      • there_and_back 来回

      • linear 一去不复返

      • smooth 平滑

  • add()

    • 无动画添加文字,可以是多个text
  • wait()

    • 等待动画停留时间
  • remove()

    • 移除text,可以是多个text

设置颜色

  • set_color(COLOR)

缩放大小

  • scale(number)

并行动画

  • add_updater

    • 跟随另一个物体的移动
  • remove_updater

    • 取消并行播放函数
  • clear_updaters

    • 取消所有的并行播放函数
  • UpdateFromFunc(Animation)

    • play函数中生效

text数组

TexMobject("A","{B","\over","C}","D","E")

Scene
基础动画

camera_config

  • background_image

GraphScene

二维坐标

setup_axes(animate=True)

  • 显示动画

get_graph()

  • 坐标系的句柄

coords_to_point(x, y)

  • 坐标变成对应的帧中的点

point_to_coords(point)

  • 帧中的点转换为坐标,返回x,y组成的元组

get_graph_label()

  • 坐标标签

get_vertical_line_to_graph()

  • 竖线

getverticallinestograph()

  • 多条竖线

改变坐标标签的颜色

  • "x_label_color":RED,

  • "y_label_color":BLUE

TextMobject

普通字符串

LaTeX

混合

  • “$\(”或者“\)

多个字符串

  • 二维数组

TexMobject

LaTeX字符串数组

二维图形类

点Dot

  • "radius": DEFAULT_DOT_RADIUS,

  • "stroke_width": 0,

  • "fill_opacity": 1.0,

  • "color": WHITE

圆形Circle

  • "color": RED,

  • "close_new_points": True,

  • "anchors_span_full_range": False

环Annulus

  • "inner_radius": 1,

  • "outer_radius": 2,

  • "fill_opacity": 1,

  • "stroke_width": 0,

  • "color": WHITE,

  • "mark_paths_closed": False,

长方形Rectangle

  • "color": WHITE,

  • "height": 2.0,

  • "width": 4.0,

  • "mark_paths_closed": True,

  • "close_new_points": True,

方形Square

  • "side_length": 2.0,

椭圆Ellipse

  • "width": 2,

  • "height": 1

弧Arc

  • "radius": 1.0,

  • "num_components": 9,

  • "anchors_span_full_range": True,

  • "arc_center": ORIGIN,

线Line

三维图形类

球 Sphere

  • "resolution": (12, 24),

  • "radius": 1,

  • "u_min": 0.001,

  • "u_max": PI - 0.001,

  • "v_min": 0,

  • "v_max": TAU,

立方 Cube

  • "fill_opacity": 0.75,

  • "fill_color": BLUE,

  • "stroke_width": 0,

  • "side_length": 2,

棱柱 Prism

  • "dimensions": [3, 2, 1]

参数曲面

  • ParametricSurface

  • ParametricFunction

群组类VGroup

批量同步操作

arrange()

相机参数
3D动画类(ThreeDScene)

set_camera_orientation()

  • 相机的角度

move_camera()

set_to_default_angled_camera_orientation()

  • 还原为默认角度

add_fixed_in_frame_mobjects()

  • 固定在屏幕的图像

坐标系类

NumberLine 数轴类

坐标系抽象类 CoordinateSystem

Axes 二维坐标类

ThreeDAxes三维坐标系类

NumberPlane

ComplexPlane复数坐标系

常见的常数

颜色

  • constants.py中COLOR_MAP

方向

  • ORIGIN = np.array((0., 0., 0.))

  • UP = np.array((0., 1., 0.))

  • DOWN = np.array((0., -1., 0.))

  • RIGHT = np.array((1., 0., 0.))

  • LEFT = np.array((-1., 0., 0.))

  • IN = np.array((0., 0., -1.))

  • OUT = np.array((0., 0., 1.))

  • X_AXIS = np.array((1., 0., 0.))

  • Y_AXIS = np.array((0., 1., 0.))

  • Z_AXIS = np.array((0., 0., 1.))

  • Useful abbreviations for diagonals

  • UL = UP + LEFT

  • UR = UP + RIGHT

  • DL = DOWN + LEFT

  • DR = DOWN + RIGHT

  • TOP = FRAME_Y_RADIUS * UP

  • BOTTOM = FRAME_Y_RADIUS * DOWN

  • LEFT_SIDE = FRAME_X_RADIUS * LEFT

  • RIGHT_SIDE = FRAME_X_RADIUS * RIGHT

角度

  • PI = np.pi

  • TAU = 2 * PI

  • DEGREES = TAU / 360

距离

  • SMALL_BUFF = 0.1

  • MED_SMALL_BUFF = 0.25

  • MED_LARGE_BUFF = 0.5

  • LARGE_BUFF = 1

  • DEFAULT_MOBJECT_TO_EDGE_BUFFER = MED_LARGE_BUFF

  • DEFAULT_MOBJECT_TO_MOBJECT_BUFFER = MED_SMALL_BUFF