TSNE动态可视化

来源:互联网 发布:北京seo外包公司 编辑:程序博客网 时间:2024/06/14 10:37

这里将上一篇博客中的迭代数据保存下来,用matplotlib一次次更新数据,利用moviepy合成一个小视频,效果看上去有点酷酷的,因为mp4格式无法上传。。。。所以:生成视频链接:http://pan.baidu.com/s/1ge7BYXd

# -*-encoding:utf-8-*-from sklearn import manifoldfrom sklearn import decompositionfrom sklearn import datasetsfrom sklearn import svmfrom sklearn.preprocessing import scale,MinMaxScalerfrom matplotlib import pyplot,colorsimport numpy,pandasfrom pandas.tools import plottingfrom pylab import mplmpl.rcParams['axes.unicode_minus']=Falsempl.rcParams['font.sans-serif']=['SimHei']from moviepy.video.io.bindings import mplfig_to_npimage as figageimport moviepy.editor as mpeimport shelvefrom scipy import spatial,sparsea=sparse.csc_matrixspatial.distance# data getdata=datasets.load_digits()x=data.datay=data.target# color list'''color=[]for i in colors.cnames:    color.append(i)'''color=['yellow','black','green','red','blue','orange','brown','pink','purple','grey']def make_frame(t):    tt=int(t/0.1)    # print tt    pyplot.clf()    for i in range(10):        xxx1 = XX[tt][YY == i, 0]        xxx2 = XX[tt][YY == i, 1]        #print xxx1.shape,xxx2.shape        pyplot.scatter(xxx1,xxx2,c=color[i])    pyplot.title('SNE VISUALIZING DYNAMIC '.decode('utf-8', 'ignore'))    pyplot.axis('off')    M1=numpy.min(XX[tt])    M2=numpy.max(XX[tt])    if M1>0:        M1=M1*0.8    else:        M1=M1*1.2    if M2>0:        M2=M2*1.2    else:        M2=M2*0.8    pyplot.xlim(M1, M2)    pyplot.ylim(M1, M2)    return figage(figure1)DD=shelve.open('tsne.dat')XX=DD['data']YY=yN=len(XX)DD.close()figure1=pyplot.figure('SNE VISUALIZING DYNAMIC',dpi=800)picture=mpe.VideoClip(make_frame,duration=40)picture.write_videofile("tsne_0.mp4",codec='mpeg4',fps=10)



1 0