[Numpy-discussion] Using normal()

Zachary Pincus zachary.pincus at yale.edu
Thu Apr 24 13:57:49 EDT 2008


>> 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

Python's built in pow() and exp() functions can't handle numpy arrays,  
and thus try (and fail) to convert arrays to scalar values. You want  
to use numpy.exp and numpy.power (or just the ** operator), to do  
these operations to numpy arrays elementwise.



More information about the NumPy-Discussion mailing list