I have a function that takes one 2D vector and returns pdf (a scalar) of that point.
我有一个函数,它接受一个2D向量并返回该点的pdf(标量)。
As an illustration, myPDF( np.array[4,4] )
returns a single pdf value at <4,4>. This function cannot take a list of vectors, and it only accepts one 2D vector as an argument.
举例说明,myPDF(np.array [4,4])在<4,4>处返回单个pdf值。此函数不能获取向量列表,它只接受一个2D向量作为参数。
Now I want to plot this pdf using plot_surface like below: This is what I've tried
现在我想用plot_surface绘制这个pdf,如下所示:这就是我尝试过的
x = np.arange(-10,10)
y = np.arange(-10,10)
z = [ myPDF( np.array([xx,yy])) for xx in x for yy in y]
xy = [ [xx, yy] for xx in x for yy in y]
And here I am stuck. I have to somehow reshape z and xy in some proper forms to use them with plot_surface, but I do not now how to do so. I also tried meshgrid, but did not know how to convert different shapes of data into legitimate forms for plot_surface.
在这里,我被困住了。我必须以某种方式重塑z和xy,以便将它们与plot_surface一起使用,但我现在不知道怎么做。我也尝试过meshgrid,但不知道如何将不同形状的数据转换为plot_surface的合法形式。
x, y = np.mgrid[-5:5,-5:5]
If I use mgrid as above, it gives me 10x10 matrix. Then, I have to generate corresponding z values in 10x10 matrix using my function, but could not find a way to decompose x and y, generate z values and reformat z values in a proper form.
如果我使用上面的mgrid,它给我10x10矩阵。然后,我必须使用我的函数在10x10矩阵中生成相应的z值,但是找不到分解x和y的方法,生成z值并以适当的形式重新格式化z值。
The major problem here is that I have little knowledge in manipulating data, so could not find a good way of doing this task. Thus, I'd like to learn a proper way of generating a dataset for plot_surface using my pdf function.
这里的主要问题是我对操作数据知之甚少,因此无法找到执行此任务的好方法。因此,我想学习一种使用我的pdf函数为plot_surface生成数据集的正确方法。
What's a good way to generate data points to plot like this using my pdf function? Thanks for helping this newB!
使用我的pdf函数生成数据点的好方法是什么?感谢您帮助这个新B!
1 个解决方案
#1
1
Use apply_along_axis
使用apply_along_axis
xy = np.stack(np.mgrid[-5:5,-5:5], axis=-1) # xy[0,0] = x, y
z = np.apply_along_axis(myPDF, axis=-1, arr=xy) # z[0,0] = myPDF(xy[0,0])
#1
1
Use apply_along_axis
使用apply_along_axis
xy = np.stack(np.mgrid[-5:5,-5:5], axis=-1) # xy[0,0] = x, y
z = np.apply_along_axis(myPDF, axis=-1, arr=xy) # z[0,0] = myPDF(xy[0,0])