【问题标题】:Creating gaussians of fixed width and std创建固定宽度和标准的高斯
【发布时间】:2021-07-01 19:08:22
【问题描述】:

我试图使高于 25.2 的每个点都成为 x 轴上宽度为 2 的高斯峰。 enter image description here

不太清楚如何在 python 中生成高斯曲线。

【问题讨论】:

    标签: python-3.x gaussian


    【解决方案1】:

    关于如何为任意数量的轴和中心位置数量生成高斯分布的完整示例。它需要包matplotlibscipynumpy

    模块可以通过以下方式控制:

    • dim 表示维度(轴)的数量。
    • fwhmfull width half maximum(估计高斯分布的宽度。)
    • centers 一个 np.arraylist 的索引,它们是高斯​​分布的中心。
    import matplotlib.cm as mpl_cm
    import matplotlib.colors as mpl_colors
    import matplotlib.pyplot as plt
    import numpy as np
    
    from scipy.spatial.distance import cdist
    
    
    class Gaussian:
        def __init__(self, size):
            self.size = size
            self.center = np.array(self.size) / 2
            self.axis = self._calculate_axis()
    
        def _calculate_axis(self):
            """
                Generate a list of rows, columns over multiple axis.
    
                Example:
                    Input: size=(5, 3)
                    Output: [array([0, 1, 2, 3, 4]), array([[0], [1], [2]])]
            """
            axis = [np.arange(size).reshape(-1, *np.ones(idx, dtype=np.uint8))
                    for idx, size in enumerate(self.size)]
            return axis
    
        def update_size(self, size):
            """ Update the size and calculate new centers and axis.  """
            self.size = size
            self.center = np.array(self.size) / 2
            self.axis = self._calculate_axis()
    
        def create(self, dim=1, fwhm=3, center=None):
            """ Generate a gaussian distribution on the center of a certain width.  """
            center = center if center is not None else self.center[:dim]
            distance = sum((ax - ax_center) ** 2 for ax_center, ax in zip(center, self.axis))
            distribution = np.exp(-4 * np.log(2) * distance / fwhm ** 2)
            return distribution
    
        def creates(self, dim=2, fwhm=3, centers: np.ndarray = (None,)):
            """ Combines multiple gaussian distributions based on multiple centers.  """
            centers = np.array(centers).T
            indices = np.indices(self.size).reshape(dim, -1).T
    
            distance = np.min(cdist(indices, centers, metric='euclidean'), axis=1)
            distance = np.power(distance.reshape(self.size), 2)
    
            distribution = np.exp(-4 * np.log(2) * distance / fwhm ** 2)
            return distribution
    
        @staticmethod
        def plot(distribution, show=True):
            """ Plotter, in case you do not know the dimensions of your distribution, or want the same interface.  """
            if len(distribution.shape) == 1:
                return Gaussian.plot1d(distribution, show)
            if len(distribution.shape) == 2:
                return Gaussian.plot2d(distribution, show)
            if len(distribution.shape) == 3:
                return Gaussian.plot3d(distribution, show)
            raise ValueError(f"Trying to plot {len(distribution.shape)}-dimensional data, "
                             f"Only 1D, 2D, and 3D distributions are valid.")
    
        @staticmethod
        def plot1d(distribution, show=True, vmin=None, vmax=None, cmap=None):
            norm = mpl_colors.Normalize(
                    vmin=vmin if vmin is not None else distribution.min(),
                    vmax=vmax if vmin is not None else distribution.max()
            )
            cmap = mpl_cm.ScalarMappable(norm=norm, cmap=cmap or mpl_cm.get_cmap('jet'))
            cmap.set_array(distribution)
            c = [cmap.to_rgba(value) for value in distribution]  # defines the color
    
            fig, ax = plt.subplots()
            ax.scatter(np.arange(len(distribution)), distribution, c=c)
            fig.colorbar(cmap)
            if show: plt.show()
            return fig
    
        @staticmethod
        def plot2d(distribution, show=True):
            fig, ax = plt.subplots()
            img = ax.imshow(distribution, cmap='jet')
            fig.colorbar(img)
            if show: plt.show()
            return fig
    
        @staticmethod
        def plot3d(distribution, show=True):
            m, n, c = distribution.shape
            x, y, z = np.mgrid[:m, :n, :c]
            out = np.column_stack((x.ravel(), y.ravel(), z.ravel(), distribution.ravel()))
            x, y, z, values = np.array(list(zip(*out)))
    
            fig = plt.figure()
            ax = fig.add_subplot(111, projection='3d')
    
            # Standalone colorbar, directly creating colorbar on fig results in strange artifacts.
            img = ax.scatter([0, 0], [0, 0], [0, 0], c=[0, 1], cmap=mpl_cm.get_cmap('jet'))
            img.set_visible = False
            fig.colorbar(img)
    
            ax.scatter(x, y, z, c=values, cmap=mpl_cm.get_cmap('jet'))
            if show: plt.show()
            return fig
    

    示例

    gaussian = Gaussian(size=(20,))
    dist = gaussian.create(dim=1, centers=(1,)
    gaussian.plot1d(dist, show=True)
    

    你的问题

    为了获得适合您问题的解决方案,以下代码将起作用:

    import numpy as np
    
    arr = np.random.randint(0, 28, (25,))
    
    gaussian = Gaussian(size=arr.shape)
    centers = np.where(arr > 25.2)
    
    distribution = gaussian.creates(dim=len(arr.shape), fwhm=2, centers=centers)
    gaussian.plot(distribution, show=True)
    

    为此,中心由条件arr > 25.2 确定。请注意,如果没有值,则下一行将崩溃。为了获得 2 的宽度,将值 fwhm 设置为 2,但您可以更改它直到获得所需的宽度,或使用 Finding the full width half maximum of a peak

    【讨论】:

    • 非常感谢,有什么想法可以在 pandas 数据框上做这个吗?
    • 尝试在 pandas 数据帧上使用它时出现错误:MemoryError Traceback (most recent call last) <ipython-input-83-f76a78614e7b> in <module> 6 centers = np.where(arr > 25.0) 7 ----> 8 distribution = gaussian.creates(dim=len(arr.shape), fwhm=2, centers=centers) 9 gaussian.plot(distribution, show=True) MemoryError: Unable to allocate 47.3 GiB for an array with shape (100000, 63488) and data type float64
    • 使用df.to_numpy 可以直接工作。 df.iloc[40:90].time.to_numpy()。否则,您必须提供数据集的 minimal reproducable example。那不是图片,因为我不能轻易复制它。
    • 所以我编辑了我的 df 列高、fwhm、中心像这样...stackoverflow.com/questions/67074258/…
    猜你喜欢
    • 1970-01-01
    • 2016-04-08
    • 2019-11-16
    • 2018-03-20
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    • 1970-01-01
    • 2021-05-15
    相关资源
    最近更新 更多