partitions.py 文件源码

python
阅读 19 收藏 0 点赞 0 评论 0

项目:neoSBM 作者: piratepeel 项目源码 文件源码
def plot(k=1,xyzFile='xyz_synth_surf.txt',write=False):
    with open(xyzFile) as f:
        xyz=np.float64([row.split() for row in f.readlines()])

    #~ plt.figure()
    #~ plt.scatter(xyz[:, 0], xyz[:, 1], c=xyz[:,2])
    #~ plt.plot(xyz[:3, 0], xyz[:3, 1], c='k', marker='s',ms=10)
    #~ plt.plot(xyz[:50, 0], xyz[:50, 1], xyz[:50,2], c='k', marker='s',ms=3)

    fig=plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.scatter(xyz[:, 0], xyz[:, 1], xyz[:,2], c=xyz[:,2], marker='o',linewidths=0)
    ax.plot(xyz[:50, 0], xyz[:50, 1], xyz[:50,2], c='k', marker='s',ms=3)
    #~ ax.scatter(xyz[:50, 0], xyz[:50, 1], xyz[:50,2], c='k', marker='s',linewidths=0,cmap=plt.cm.bone)

    xmin=np.min(xyz[:,0])
    xmax=np.max(xyz[:,0])
    step=(xmax-xmin)/100.

    x_=np.arange(np.min(xyz[:,0]),np.max(xyz[:,0]),step)
    y_=np.arange(np.min(xyz[:,0]),np.max(xyz[:,0]),step)
    xx,yy=np.meshgrid(x_,y_)

    xy=np.append(xx.ravel()[:,np.newaxis],yy.ravel()[:,np.newaxis],1)

    knn = neighbors.KNeighborsRegressor(k, weights='distance',p=1)
    z_= knn.fit(xyz[:,:2],xyz[:,2]).predict(xy)


    fig=plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.plot_surface(xx, yy, z_.reshape(np.shape(xx)),rstride=1, cstride=1, cmap=plt.cm.spectral,
                       linewidth=0, antialiased=False)
    ax.plot(xyz[:50, 0], xyz[:50, 1], xyz[:50,2], c='k', marker='s',ms=3)
    if write:
        with open('knn_'+ xyzFile,'w') as f:
            for xi,yi,zi in zip(xx.ravel(),yy.ravel(),z_):
                f.write('%f %f %f\n' % (xi,yi,zi))




#################################################################
#calculate a distance matrix based on variation of information
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号