输入
对txt文件:
with open('C:UsersmageDesktoptest.txt’,'r') as f:
txt=f.read()
txt=txt.replace('n','') #删除数据中的回车符号
data=[]
for i in range (0,n):
t=[]
t.append(txt.split(',')[i])#读取数据 数据间分割符号是逗号
划分数据集
def sampleSplit(data, ratio=0.2):
"""
将数据随机划分为训练集和测试集,ratio是测试集数据比例
"""
np.random.shuffle(data) #打乱数据顺序
n = np.int(data.shape[0]*ratio)
testData, trainData = data[:n], data[n:]
return trainData, testData
输出
画图:
fig=plt.figure()
x=[1,2,3]
y=[1,4,9]
plt.scatter(x,y,c='red', marker='+') # marker点的形状 默认是圆形
plt.show
fig.savefig('保存图片为png格式')
动图
from matplotlib import animation
# 将迭代过程中保存下来的中间过程的图片制作成gif, 可以看到GMM模型的变化过程
imgs = []
fig = plt.figure()
#模型迭代时产生了一共有model.iterations张图片,存放在C:/Users/mage/Desktop/powSys/EM_GMM2/img中,制作动画就是根据这些静态图
for i in range(model.iterations):
path = "C:/Users/mage/Desktop/powSys/EM_GMM2/img/iteration" + str(i) + ".png"
img = plt.imread(path)
img = plt.imshow(img, animated=True)
imgs.append([img])
"""plt.figure()函数的作用是新建一张图像,这个图像是一个类似于相框一样的东西,上面可以放图片。这里"""
ani = animation.ArtistAnimation(fig,
imgs,
interval=500,
blit=True,
repeat_delay=1000)
ani.save("C:/Users/mage/Desktop/powSys/EM_GMM2/iterationAnime.gif", writer="pillow")
还需要什么评论,我来更新



