[Numpy-discussion] Using normal()

Keith Goodman kwgoodman at gmail.com
Thu Apr 24 13:58:34 EDT 2008


On Thu, Apr 24, 2008 at 10:51 AM, Rich Shepard <rshepard at appl-ecosys.com> wrote:
> On Thu, 24 Apr 2008, Keith Goodman wrote:
>
>  > norm = 1 / (scale * sqrt(2 * pi))
>  > y = norm * exp(-power((x - loc), 2) / (2 * scale**2))
>
>    Hmm-m-m. I don't understand the source of the error:
>
>    y = norm * exp(-pow((x - loc), 2) / (2 * scale**2))
>  TypeError: only length-1 arrays can be converted to Python scalars

I don't understand the error (I don't use arrays). Maybe try
evaluating pieces of the formula, like pow(x-loc, 2) etc to see where
the error is.

It works for me:

>> x = arange(0,10)
>> scale=1
>> loc=1
>> norm = 1 / (scale * sqrt(2 * pi))
>> y = norm * exp(-power((x - loc), 2) / (2 * scale**2))
>> y

array([  1.46762663e-01,   3.98942280e-01,   1.46762663e-01,
         5.39909665e-02,   2.68805194e-03,   1.33830226e-04,
         9.01740968e-07,   6.07588285e-09,   5.54048800e-12,
         5.05227108e-15])



More information about the NumPy-Discussion mailing list