[Tutor] Fitting data to error function
Danny Yoo
dyoo at hashcollision.org
Fri Mar 13 19:27:00 CET 2015
On Fri, Mar 13, 2015 at 11:00 AM, Danny Yoo <dyoo at hashcollision.org> wrote:
>> The error I am recieving is as follows:
>>
>> TypeError: only length-1 arrays can be converted to Python scalars
>
>
> Hi Colin,
>
> Do you have a more informative "stack trace" of the entire error?
> Providing this will help localize the problem. As is, it's clear
> there's a type error... somewhere... :P Seeing the stack trace will
> make things easier for us.
Quick and dirty analysis. If I had to guess, I'd look at the mix of
math.erfc with numeric array values in the subexpression:
math.erfc((np.sqrt(2.)*x*1.E-3)/b)
np.sqrt(...) returns a numpy array, if I'm not mistaken. But
math.erfc() probably can't deal with numpy values. So there's
definitely a problem there.
See messages such as:
https://groups.google.com/forum/#!topic/comp.lang.python/9E4HX4AES-M
which suggest that trying to use the standard library math functions
on numpy arrays isn't going to work.
Unfortunately, without explicit stack trace, I don't know if that's
the *only* problem you're seeing. That's why providing a good stack
trace is so important in bug reports: there can be multiple causes for
something to go wrong. I'd rather make sure we've hit the one that's
causing you grief.
Anyway, remediation time. Reading docs... ok, you could probably make
a vectorized version of the erfc function:
vectorized_erfc = np.vectorize(math.erfc)
and use this in favor of the non-vectorized version. vectorize allows
you to take regular functions and turn them into ones that work on
numpy arrays:
http://docs.scipy.org/doc/numpy/reference/generated/numpy.vectorize.html
A better approach is probably to reuse the scipy.special.erfc function
within scipy itself:
http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.special.erfc.html
If you have more questions, please feel free to ask.
More information about the Tutor
mailing list