學(xué)習(xí)manim引擎
首先竹海,對manim的博客網(wǎng)站資料進行了學(xué)習(xí),manim沒有文檔丐黄,一切全是自己摸索斋配,加上我是一個編程半個小白,先看看別人有什么理解灌闺。
參考:B站up主的manim的使用艰争。
manim學(xué)習(xí)日記(三)。
網(wǎng)址就不放了桂对,講了一些簡單的制作甩卓。
結(jié)構(gòu)
manim有一些重要的構(gòu)成:
數(shù)學(xué)對象 場景 動畫 工具
對應(yīng)的文件夾:mobject sence animation utils
數(shù)學(xué)對象:我們在動畫中表達的一切都是數(shù)學(xué)對象 包括 點、線蕉斜、面逾柿、空間缀棍、左邊、文字等具體可以在包里查看鹿寻。
場景:就是背景睦柴,默認的是黑色背景,你也可以使用作者使用的“派”背景毡熏。
動畫:與動畫相關(guān)的各類方法坦敌,創(chuàng)造,刷新痢法,移動狱窘,指示,旋轉(zhuǎn)财搁,強調(diào)蘸炸,變形等,是核心組件尖奔。例如:ShowCreation()
utils:大部分引用外部應(yīng)用的功能都在這里搭儒,包括python的繪圖庫,貝塞爾曲線矢量圖提茁,著色系統(tǒng)淹禾,latex寫公式轉(zhuǎn)成圖片等功能
在這里我寫了一個極其簡單的程序:
from big_ol_pile_of_manim_imports import *
class Test(TeacherStudentsScene):
def construct(self):
text1 = TextMobject('I love you')
text2 = TextMobject('this is a funny!')
text3 = TextMobject('between we distance similar as :')
dot1 = Dot(color=COLOR_MAP["RED_A"],point=UL*2)
dot2 = Dot(color=COLOR_MAP["RED_B"],point=DR*2)
line1 = Line(dot1,dot2)
self.play(ShowCreation(text1))
self.wait(1)
self.play(ShrinkToCenter(text1))
self.play(FadeOutAndShift(text1))
self.wait(1)
self.play(ShowCreation(text2))
self.wait(1)
self.play(FadeOutAndShift(text2,UP))
self.wait(1)
self.play(ShowCreation(text3))
self.wait(1)
self.play(FadeOut(text3))
self.wait(1)
self.play(ShowCreation(dot1))
self.wait(1)
self.play(ShowCreation(dot2))
self.wait(1)
self.play(ShowCreation(line1))
self.wait(3)
'''self.play(Write(text))
c1 = Circle()
d1 = Dot(IN)
d2 = Dot(OUT+LEFT*2,color = COLOR_MAP["GREEN_A"])
s1 = Square()
e1 = Ellipse()
s2 = Sector(outer_radius=4,
inner_radius= 0,color=COLOR_MAP["MAROON_A"])
l1 = Line(d1,d2,color = COLOR_MAP["YELLOW_B"])
d3 = DashedLine(d1,d2)
a1 =Arrow(d1,d2)
#p1 = Polygon(np.array([1,2,3,4],[-1,-2,-3,-4]))
# p2 = RegularPolygon()
self.play(ShowCreation(c1))
self.play(ShowCreation(d1))
self.play(ShowCreation(d2))
self.play(ShowCreation(e1))
self.play(ShowCreation(s2))
self.play(ShowCreation(l1))
self.play(ShowCreation(a1))
# self.play(ShowCreation(p1))
# self.play(ShowCreation(p2))
self.play(Transform(c1,d1))
self.play(ShowCreation(s1))
self.play(FadeOutAndShift(d1))
self.play(FadeOutAndShift(d2))
self.play(FadeOutAndShift(e1))
self.play(FadeOutAndShift(s2))
self.play(FadeOutAndShift(l1))
self.play(FadeOutAndShift(a1))
self.wait()'''
程序雖然簡單,但是足以明白程序的實現(xiàn)過程茴扁。
另外:怎樣去執(zhí)行程序:
python -m manim -h #查看命令幫助 -m是指定驅(qū)動引擎
#接下來給個實例:
python -m manim -w test.py Test -pl
#-w 寫入媒體文件铃岔,-p 預(yù)覽, -l 低質(zhì)量 test.py 自己所寫的py文件峭火, Test實例化的動畫(在上面)
更多內(nèi)容查看官方的極簡文檔毁习。
keras對鳶尾花分類
之前學(xué)習(xí)多元統(tǒng)計分析時曾經(jīng)使用過判別分析對鳶尾花進行分類。使用軟件是R卖丸,準(zhǔn)確度在百分之九十以上纺且。
現(xiàn)在,使用基于多層感知器 (MLP) 的 softmax 多分類對鳶尾花進行分類稍浆。
鳶尾花數(shù)據(jù)是常用的測試算法的數(shù)據(jù)隆檀,隨便在網(wǎng)上就能下載。
import numpy as np
data = np.loadtxt('iris.csv',delimiter=',')#刪除數(shù)據(jù)的第一行粹湃,神經(jīng)網(wǎng)絡(luò)訓(xùn)練不需要變量名,對最后一列進行編碼泉坐,將類型變?yōu)? 1 2
X = data[:,0:-1]
y = data[:,-1]
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state = 0)
y_train = y_train.reshape(120,1)#sklearn方法切割的數(shù)據(jù)維度不符合为鳄,需要變形
y_test = y_test.reshape(30,1)
import keras
from keras.models import Sequential
from keras.layers import Dense,Dropout,Activation
from keras.optimizers import SGD
y_train = keras.utils.to_categorical(y_train,num_classes=3)
y_test = keras.utils.to_categorical(y_test,num_classes=3)#keras的方法標(biāo)記標(biāo)簽
model = Sequential()
model.add(Dense(100,input_dim=4,activation ='relu'))
model.add(Dropout(0.5))
model.add(Dense(100,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(3,activation='softmax'))
sgd = SGD(lr=0.01,decay=1e-6,momentum=0.9,nesterov=True)
model.compile(loss='categorical_crossentropy',
optimizer=sgd,
metrics=['accuracy'])
#輸入層的維度要和數(shù)據(jù)的變量維度相同,這里我有兩個隱含層,一個softmax層(最后的輸出層)腕让。SGD是隨機梯度下降法孤钦。
#complie是配置過程歧斟,在這里,需要設(shè)置損失函數(shù)偏形,優(yōu)化方法静袖,評價指標(biāo)。
model.fit(X_train,y_train,epochs=20,batch_size=20)
model.evaluate(X_test,y_test,batch_size=20)
#模型訓(xùn)練和模型評估俊扭。batch_size是隨機梯度下降一批的數(shù)量队橙,epochs 是迭代的次數(shù)。
import pydot_ng as pydot
from keras.utils import plot_model
plot_model(model,to_file='model.png')
#可視化萨惑,但是我失敗了捐康。用了2個小時的時間沒有找到原因庸蔼。
看Out[13] :我的LOSS為0.1028解总,準(zhǔn)確度為1,就是100%姐仅,看來我這次運氣不錯呀花枫。
我多試了幾次,結(jié)果都在90%以上掏膏。效果不錯劳翰。
改天貼 R的判別分析代碼。