将离散的δ函数平滑成正态分布。

huangapple go评论52阅读模式
英文:

Smooth a discrete delta function into a normal distribution

问题

我有一个对n个箱子进行了独热编码的分布,即如果它在特定的箱子中,则为1,否则为0:

test = np.array([0, 0, 1, 0, 0, 0, 0, 0, 0, 0])

我想要的是将它平滑,使其变成一个正态分布,峰值在同一位置,但更多的质量分布到其他箱子,如下所示:

test = np.array([0.04, 0.2, 0.5, 0.2, 0.04, 0.01, 0.005, 0.003, 0.0015, 0.0005])

(这些只是我编造的数字 - 分布仍然需要总和为1)

谢谢!

英文:

I have a one-hot encoded distribution over n bins, ie there is a 1 if it's in a particular bin, 0 otherwise:

test = np.array([0, 0, 1, 0, 0, 0, 0, 0, 0, 0])

what I'd like to do is smooth it so that it becomes a normal distribution, with the peak in the same place but more mass distributed to the other bins, like so:

test = np.array([0.04, 0.2, 0.5, 0.2, 0.04, 0.01, 0.005, 0.003, 0.0015, 0.0005])

(These are just numbers I made up - the distribution needs to still sum to 1)

Thank you!

答案1

得分: 1

生成高斯分布的权重,其平均值是位置为1的地方,然后将其标准化,使总和为1。

可以自行选择标准差,示例中将参数 sigma 设置为1。较大的 sigma 值将使值分布更广泛。

import numpy as np
test = np.array([0, 0, 1, 0, 0, 0, 0, 0, 0, 0])

def gauss(x, mu, sigma):
    # 由于我们正在处理离散分布,稍后将进行归一化。这里不需要添加 1/sigma/sqrt(2*pi)。
    return np.exp(-(x-mu)**2/(2*sigma**2))

def smooth(test, sigma):
    # 处理边界情况
    if sigma == 0:
        return test

    # 计算分布的平均值 - 输入中 1 的位置
    mu = np.argmax(test)

    # 计算高斯权重
    smoothed = np.array([gauss(i, mu, sigma) for i in range(len(test))])

    # 标准化使其总和为1
    return smoothed / sum(smoothed)

result = smooth(test, 1)
[round(i, 2) for i in result] # [0.05, 0.24, 0.4, 0.24, 0.05, 0.0, 0.0, 0.0, 0.0, 0.0]
英文:

You need to generate the weights of a gaussian distribution which average is the position of your 1. Then you normalize it so it sums to 1.

You can choose the Standard Deviation however you like, it is the parameter sigma that is set to 1 in the exemple. A greater sigma will give you more spread-out values.

import numpy as np
test = np.array([0, 0, 1, 0, 0, 0, 0, 0, 0, 0])

def gauss(x, mu, sigma):
    # Since we're doing a discrete distribution, we'll normalize later. We don't need to add a 1/sigma/sqrt(2*pi) here.
    return np.exp(-(x-mu)**2/(2*sigma**2))

def smooth(test, sigma):
    # Handle edge case 
    if sigma == 0:
        return test
    
    # Get the average of the distribution - the position of the 1 in the input
    mu = np.argmax(test)

    # We compute gaussians weight
    smoothed = np.array([gauss(i,mu,sigma) for i in range(len(test))])

    # We normalize it so it sums to 1
    return smoothed / sum(smoothed)

result = smooth(test, 1)
[round(i, 2) for i in result] # [0.05, 0.24, 0.4, 0.24, 0.05, 0.0, 0.0, 0.0, 0.0, 0.0]

huangapple
  • 本文由 发表于 2023年4月6日 19:39:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949090.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定