小编alg*_*gol的帖子

python中的快速向量化多项式

我目前正在使用 NumPy 执行以下任务:我有一个很大的值网格,我需要在每个点提取多项式样本。多项式的概率向量因网格站点而异,因此 NumPy 多项式函数对我来说不太适用,因为它从相同的分布中进行所有绘制。迭代每个站点似乎效率极低,我想知道是否有一种方法可以在 NumPy 中有效地做到这一点。如果我使用 Theano (参见这个答案),这样的事情似乎是可能的(而且很快),但这需要大量的重写,我希望避免这种情况。多项式采样能否在基本 NumPy 中有效矢量化?

编辑:很容易修改 Warren 的代码以允许不同站点的不同计数,因为我发现我需要:所有需要做的就是传入完整数组count并删除第一个,如下所示:

import numpy as np


def multinomial_rvs(count, p):
    """
    Sample from the multinomial distribution with multiple p vectors.

    * count must be an (n-1)-dimensional numpy array.
    * p must an n-dimensional numpy array, n >= 1.  The last axis of p
      holds the sequence of probabilities for a multinomial distribution.

    The return value has the same shape as p.
    """
    out = np.zeros(p.shape, …
Run Code Online (Sandbox Code Playgroud)

python optimization numpy vectorization multinomial

6
推荐指数
1
解决办法
2827
查看次数

在 matplotlib 中将一个图像动画化到另一个图像之上

我一直在使用 matplotlib 为一些图像制作动画,但我现在发现我想为这些动画添加更多信息,所以我想覆盖一个指示重要特征的散点图。这是迄今为止我一直用于生成电影的代码:

def make_animation(frames,path,name): 

    plt.rcParams['animation.ffmpeg_path'] = u'/Users/~/anaconda3/bin/ffmpeg' #ffmpeg path    
    n_images=frames.shape[2] 
    assert (n_images>1)   
    figsize=(10,10)
    fig, ax = plt.subplots(figsize=figsize)
    fig.tight_layout()
    fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=None, hspace=None)
    #lineR, = ax.plot(xaxis_data[0],R_data[0],'c-',label="resources")
    img = ax.imshow(frames[:,:,0], animated = True)   


    def updatefig(img_num): 

        #lineR.set_data(xaxis_data[img_num],R_data[img_num],'r-')

        img.set_data(frames[:,:,img_num])

        return [img]


    ani = animation.FuncAnimation(fig, updatefig, np.arange(1, n_images), interval=50, blit=True)
    mywriter = animation.FFMpegWriter(fps = 20)
    #ani.save('mymovie.mp4',writer=mywriter)

    ani.save("/Users/~/output/"+ path + "/" + name + ".mp4",writer=mywriter)

    plt.close(fig)
Run Code Online (Sandbox Code Playgroud)

我想在每一帧的顶部添加一个散点图,就像我可以用这样的常规图做的那样:

fig, ax = plt.subplots()
img = ax.imshow(frames[:,:,0])
img = ax.scatter(scatter_pts[0],scatter_pts[1],marker='+',c='r')
Run Code Online (Sandbox Code Playgroud)

我在这方面的第一次尝试看起来像这样:

def make_animation_scatter(frames,path,name,scatter): 

    plt.rcParams['animation.ffmpeg_path'] = u'/Users/~/anaconda3/bin/ffmpeg' …
Run Code Online (Sandbox Code Playgroud)

python animation matplotlib

3
推荐指数
1
解决办法
213
查看次数