[SciPy-User] scipy.stats.norm strange doc string

Daniel Lepage dplepage at gmail.com
Sun Mar 13 16:18:58 EDT 2011


norm.pdf takes x, loc, and scale, and returns y such that y[i] is the
value of a normal pdf with mean loc[i] and scale scale[i] evaluated at
x[i]. If x, loc, or scale is a scalar, it's treated as though you
passed in an array containing all the same element.

Your error is because norm.pdf requires that if any of x, loc, and
scale aren't scalars, they should be arrays *of the same length*. For
example, the following three are (roughly) equivalent:

norm.pdf([0,1,2], loc = 1, scale = 2)
norm.pdf([0,1,2], loc = [1,1,1], scale = [2,2,2])
[norm.pdf(x,l,s) for (x,l,s) in zip([0,1,2], [1,1,1], [2,2,2])]

but
norm.pdf([0,1,2], loc = [1,1], scale = 2)
will produce a shape mismatch because a length-3 array can't be
broadcast to the same shape as a length-3 array.

-- 
Dan Lepage


On Sun, Mar 13, 2011 at 3:52 PM, nicky van foreest <vanforeest at gmail.com> wrote:
> Hi,
>
> The doc string of scipy.stats.norm tells me that the location and
> scale parameters are array-like. However, when I try to pass arrays to
> the loc and scale keywords I get an error. Specifically:
>
>
> In [1]: from scipy.stats import norm
>
> In [2]: import numpy as np
>
> In [3]: mu = np.array([1,1])
>
> In [4]: simga = np.array([1,1])
>
> In [5]: x = [0,1,2,3]
>
> In [6]: norm.pdf(x, loc = mu, scale = simga)
>
>
> results in :
>
> ValueError: shape mismatch: objects cannot be broadcast to a single shape
>
> I do understand how to resolve this problem, but for my specific
> purpose I would have liked to pass mu and sigma as arrays, that is, I
> would have liked to achieve
>
>        tau = np.zeros([g,m])
>        for i in range(g):
>            tau[i] = p[i]*norm.pdf(x, loc=mu[i], scale = sigma[i])
>
>
> in one pass.
>
> BTW:
>
> I am using this code to fit a set of normal distributions to a given
> (quite) general distribution function by using the EM algorithm. Is
> this already coded somewhere in scipy? If not, is somebody interested
> in me making this available on the scipy cookbook?
>
> bye
>
> Nicky
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>



More information about the SciPy-User mailing list