【问题标题】:scipy truncnorm with array vectors as argumnents for a and bscipy truncnorm 以数组向量作为 a 和 b 的参数
【发布时间】:2015-04-24 07:35:26
【问题描述】:

所以我需要使用 Python 中的 truncnorm 函数来评估下限为零的截断正态分布中值的可能性。

由于truncnorm 适用于标准正态分布,如果您使用自己的musigma 参数,则必须重新参数化abab 写成 (myclip - mu) / sigma

我需要在musigma 的不同参数化下一次评估多个值,这意味着我必须将数组作为ab 的参数。但是,每次我这样做时都会收到一条错误消息。一些简化的示例代码如下:

import numpy as np
from scipy.stats import truncnorm
v = np.array([5, 4])
mus = np.ones(2)
sigmas = np.ones(2)
a = (0 - mus) / sigmas
b = np.ones(2) * np.inf

like = truncnorm.pdf(vals, a, b, mus, sigmas)

我收到此错误消息:

cond0 = self._argcheck(*args) & (scale > 0)
File "/Users/adamosth/anaconda/lib/python2.7/sitepackages/scipy/stats/_continuous_distns.py", line 3818, in _argcheck
if self.a > 0:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

如果a 作为单个值输入,我没有问题,但这并不是我真正想要的。

【问题讨论】:

    标签: python arrays numpy scipy


    【解决方案1】:

    在将 scipy v0.13 更新到 v0.14 后,我遇到了同样的问题。它仍然无法在 v0.15(我现在已经安装)中工作。所以我可以确认你的错误。

    目前,我使用一种解决方法,逐个循环遍历所有值,这当然非常慢,而且不是真正的长期解决方案。我希望有人有更好的建议。

    【讨论】:

    • 这已在较新的 scipy 版本中得到修复。
    猜你喜欢
    • 1970-01-01
    • 2019-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-30
    • 1970-01-01
    • 1970-01-01
    • 2020-11-04
    相关资源
    最近更新 更多