KDE在不同亩,SIGMA /映射功能蟒蛇到一个数组 [英] KDE in python with different mu, sigma / mapping a function to an array
问题描述
我有我想上执行高斯KDE,使用catch值的2维数组:点被假定为有不同的差异。对于这一点,我具有第二二维阵列(具有相同的形状),它是将用于各点的高斯的方差。在简单的例子,
导入numpy的是NP
数据= np.array([0.4,0.2],[0.1,0.5])
西格马= np.array([[0.05,0.1],[0.02,0.3]])
将有有四个高斯,其中第一个是在x = 0.4具有与西格马居中; = 0.05。的注:实际数据比2x2的更大的
我找了两件事情之一:
- 高斯KDE求解器,将允许对带宽改变每一个点
或
<醇开始=2>我发现来处理,这是通过六西格玛数组的数组乘法和数据阵列的最好方法。然后,我堆栈阵列的每个值我要解决的KDE的。
导入numpy的是NP高清solve_gaussian(VAL,data_array中,sigma_array):
返回(1 / sigma_array)* np.exp( - (VAL - data_array中)*(val - 一个data_array中)/(2 * sigma_array * sigma_array))高清solve_kde(的Xlist,data_array中,sigma_array):
kde_array = np.array([])
在XX的Xlist:
single_kde = solve_gaussian(XX,data_array中,sigma_array)
如果np.ndim(kde_array)== 3:
kde_array = np.concatenate((kde_array,single_kde [np.newaxis,:,:]),轴= 0)
其他:
kde_array = np.dstack(single_kde)
返回kde_array根据需要的Xlist = np.linspace(0,1,101)#Adjust
kde_array = solve_kde(的Xlist,data_array中,sigma_array)
kde_vector = np.sum(np.sum(kde_array,轴= 2),轴= 1)
mode_guess =的Xlist [np.argmax(kde_vector)
警告,任何人企图利用这个code:高斯的值是沿0轴,轴不作为2在原来的问题指定
I have a 2-dimensional array of values that I would like to perform a Gaussian KDE on, with a catch: the points are assumed to have different variances. For that, I have a second 2-dimensional array (with the same shape) that is the variance of the Gaussian to be used for each point. In the simple example,
import numpy as np
data = np.array([[0.4,0.2],[0.1,0.5]])
sigma = np.array([[0.05,0.1],[0.02,0.3]])
there would be four gaussians, the first of which is centered at x=0.4 with σ=0.05. Note: Actual data is much larger than 2x2
I am looking for one of two things:
- A Gaussian KDE solver that will allow for bandwidth to change for each point
or
- A way to map the results of each Gaussian into a 3-dimensional array, with each Gaussian evaluated across a range of points (say, evaluate each center/σ pair along np.linspace(0,1,101)). In this case, I could e.g. have the KDE value at x=0.5 by taking outarray[:,:,51].
The best way I found to handle this is through array multiplication of a sigma array and a data array. Then, I stack the arrays for each value I want to solve the KDE for.
import numpy as np
def solve_gaussian(val,data_array,sigma_array):
return (1. / sigma_array) * np.exp(- (val - data_array) * (val - data_array) / (2 * sigma_array * sigma_array))
def solve_kde(xlist,data_array,sigma_array):
kde_array = np.array([])
for xx in xlist:
single_kde = solve_gaussian(xx,data_array,sigma_array)
if np.ndim(kde_array) == 3:
kde_array = np.concatenate((kde_array,single_kde[np.newaxis,:,:]),axis=0)
else:
kde_array = np.dstack(single_kde)
return kde_array
xlist = np.linspace(0,1,101) #Adjust as needed
kde_array = solve_kde(xlist,data_array,sigma_array)
kde_vector = np.sum(np.sum(kde_array,axis=2),axis=1)
mode_guess = xlist[np.argmax(kde_vector)]
Caveat, for anyone attempting to use this code: the value of the Gaussian is along axis 0, not axis 2 as specified in the original question.
这篇关于KDE在不同亩,SIGMA /映射功能蟒蛇到一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!