是否可以矢量化 scipy Dirichlet PDF 函数?
Is it possible to vectorize the scipy Dirichlet PDF function?
我知道一些 scipy.stats
pdf 函数是开箱即用的矢量化,如 manual 中所述。
我的问题是我已经尝试将这种矢量化与 Dirichlet 的 pdf 一起使用,scipy.stats.dirchlet.pdf(x, alpha)
希望我可以为相同的 alpha
计算多个 x
的值数组] 范围。可以得到与
相同结果的东西
import numpy as np
import scipy.stats as st
alpha = [1,10,1]
list_of_xs = np.random.dirichlet(alpha,10**6)
values = np.array([st.dirichlet.pdf(x, alpha) for x in list_of_xs)])
老实说,我并不真的需要矢量化 pdf 函数,但是使用 for
循环执行它需要很长时间,所以我希望矢量化可以加速代码。
我尝试过的事情
- 将
alpha
作为形状数组传递 (10**6, 3)
将它们与 np.vstack([alpha] * 10**6)
堆叠
这不起作用,因为 Scipy 抛出以下错误:
ValueError: Parameter vector 'a' must be one dimensional, but a.shape = (1000000, 3).
这(在我的理解中)表明该函数不是开箱即用的矢量化,至少以我预期的方式。
- 用
f = np.vectorize(st.dirichlet.pdf)
向量化函数,然后用堆叠的 alpha f(x, alpha)
调用新函数 f
这没有用,因为 Scipy 抛出以下错误:
ValueError: Parameter vector 'a' must be one dimensional, but a.shape = ().
我认为这是因为我误用了 np.vectorize()
函数,或者 pdf 函数无法以这种方式矢量化。
那么,如果可以向量化这个函数,我该怎么做呢?为它付出的努力值得吗?我的意思是,在我的简单理解中,矢量化几乎总是有助于加速这种代码,但我不确定这里是否是这种情况。
您可以通过简单地转置第一个参数来获得您想要的准确结果:
st.dirichlet.pdf(list_of_xs.T, alpha)
dirichlet
的文档暗示这可能是可能的,但链接是一团糟,因此从未明确说明:
Note that the dirichlet interface is somewhat inconsistent. The array returned by the rvs function is transposed with respect to the format expected by the pdf
and logpdf
.
在相关说明中,我已将 PR #14190 提交给 scipy 以查看是否可以修复文档上的链接。
我知道一些 scipy.stats
pdf 函数是开箱即用的矢量化,如 manual 中所述。
我的问题是我已经尝试将这种矢量化与 Dirichlet 的 pdf 一起使用,scipy.stats.dirchlet.pdf(x, alpha)
希望我可以为相同的 alpha
计算多个 x
的值数组] 范围。可以得到与
import numpy as np
import scipy.stats as st
alpha = [1,10,1]
list_of_xs = np.random.dirichlet(alpha,10**6)
values = np.array([st.dirichlet.pdf(x, alpha) for x in list_of_xs)])
老实说,我并不真的需要矢量化 pdf 函数,但是使用 for
循环执行它需要很长时间,所以我希望矢量化可以加速代码。
我尝试过的事情
- 将
alpha
作为形状数组传递(10**6, 3)
将它们与np.vstack([alpha] * 10**6)
堆叠
这不起作用,因为 Scipy 抛出以下错误:
ValueError: Parameter vector 'a' must be one dimensional, but a.shape = (1000000, 3).
这(在我的理解中)表明该函数不是开箱即用的矢量化,至少以我预期的方式。
- 用
f = np.vectorize(st.dirichlet.pdf)
向量化函数,然后用堆叠的 alphaf(x, alpha)
调用新函数 f
这没有用,因为 Scipy 抛出以下错误:
ValueError: Parameter vector 'a' must be one dimensional, but a.shape = ().
我认为这是因为我误用了 np.vectorize()
函数,或者 pdf 函数无法以这种方式矢量化。
那么,如果可以向量化这个函数,我该怎么做呢?为它付出的努力值得吗?我的意思是,在我的简单理解中,矢量化几乎总是有助于加速这种代码,但我不确定这里是否是这种情况。
您可以通过简单地转置第一个参数来获得您想要的准确结果:
st.dirichlet.pdf(list_of_xs.T, alpha)
dirichlet
的文档暗示这可能是可能的,但链接是一团糟,因此从未明确说明:
Note that the dirichlet interface is somewhat inconsistent. The array returned by the rvs function is transposed with respect to the format expected by the
logpdf
.
在相关说明中,我已将 PR #14190 提交给 scipy 以查看是否可以修复文档上的链接。