本文介绍了numpy:生成二维高斯和的pdf数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试生成一个[600 x 600] numpy数组,其中包含10个类似高斯的数组的总和(每个数组都有一个随机生成的中心).

I'm trying to generate a [600 x 600] numpy array that contains the sum of 10 Gaussian-like arrays (each with a randomly-generated center).

我尝试过使用高斯滤波器来生成单个类似高斯的数组,然后对其求和,但是我敢肯定有一种矢量化的方法可以解决这个问题.即使使用num_centers=10,它的速度仍然很慢,我可能需要累加多达20个高斯信号.

I've tried using a Gaussian filter to generate the individual Gaussian-like arrays, then summing them up, but I'm sure there's a vectorized way to approach this. Even with num_centers=10 it's slow, and I might need to sum as many as 20 Gaussians.

这里有一个类似的问题,但是似乎没有一个很好的或结论性的答案,而且我不确定如何将其应用于我的问题.高斯人的总和成为快速的脾气暴躁?

There is a similar question here, but it doesn't seem to have a good or conclusive answer and I'm not sure how to apply it to my problem.Sum of Gaussians into fast Numpy?

这就是我尝试过的.

import numpy as np
from scipy.ndimage import gaussian_filter
import matplotlib.pyplot as plt


num_centers = 10               # number of Gaussians to sum
sigma = 100                    # std. dev. of each Gaussian
result = np.zeros((600, 600))


for _ in range(num_centers):

    # Pick a random coordinate within the array as the center
    center = np.random.uniform(result.shape).astype(int)

    # Make array with 1 at the center and 0 everywhere else
    temp = np.zeros_like(result)
    temp[center[0], center[1]] = 1

    # Apply filter
    gaussian = gaussian_filter(temp, sigma)

    # Add to result
    result += gaussian


# Result should look like a contour map with several hills
plt.imshow(result * 1000)        # scale up to see the coloring
plt.show()

推荐答案

您可以消除循环,而是在每个中心创建一个值为1的数组,然后将gaussian_filter 一次应用于这个数组.所有步骤都可以向量化.

You can eliminate the loop, and instead create an array with the value 1 at each center and then apply gaussian_filter once to this array. All the steps can be vectorized.

这是一个例子.我将sigma减小了,以便于区分中心,然后将宽度增加到800(没有特殊原因:).

Here's an example. I made sigma smaller so it was easier to distinguish the centers, and I increased the width to 800 (for no particular reason :).

import numpy as np
from scipy.ndimage import gaussian_filter
import matplotlib.pyplot as plt


num_centers = 10
sigma = 25
size = (600, 800)

impulses = np.zeros(size)

# rows and cols are the row and column indices of the centers
# of the gaussian peaks.
np.random.seed(123456)
rows, cols = np.unravel_index(np.random.choice(impulses.size, replace=False,
                                               size=num_centers),
                              impulses.shape)
impulses[rows, cols] = 1
# or use this if you want duplicates to sum:
# np.add.at(impulses, (rows, cols), 1)

# Filter impulses to create the result.
result = gaussian_filter(impulses, sigma, mode='nearest')

plt.imshow(result)
plt.show()

这是情节:

您可以尝试使用gaussian_filtermode参数来查看哪种模式最适合您.

You can experiment with the mode argument of gaussian_filter to see which mode works best for you.

这篇关于numpy:生成二维高斯和的pdf数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 16:33