[SciPy-User] numpy.piecewise doesn't work with lists, only ndarrays

Warren Weckesser warren.weckesser@enthought....
Mon Oct 8 16:26:24 CDT 2012


On Mon, Oct 8, 2012 at 5:14 PM, Juan Luis Cano Rodríguez <
juanlu001@gmail.com> wrote:

> I have noticed this behaviour of numpy.piecewise:
>
> In [1]: import numpy as np
>
> In [2]: q = [1, 2, 3, 4, 5, 6]
>
> In [3]: np.piecewise(q, [q < 3, 3 <= q], [-1, 1])
> Out[3]: array([ 1, -1,  0,  0,  0,  0])
>
>
The `condlist` argument must be a list of bool arrays.  In your case,
because `q` is  python list, `q < 3` is not an array.  It is simply `False`:

In [3]: q = [1, 2, 3, 4, 5, 6]

In [4]: q < 3
Out[4]: False


It will work if you pass in lists of boolean values.  E.g.:

In [6]: piecewise(q, [[x < 3 for x in q], [x >= 3 for x in q]], [-1, 1])
Out[6]: array([-1, -1,  1,  1,  1,  1])


Warren


In [4]: q = np.array(q)
>
> In [5]: np.piecewise(q, [q < 3, 3 <= q], [-1, 1])
> Out[5]: array([-1, -1,  1,  1,  1,  1])
>
> Maybe the function should work the same both with lists and arrays? Should
> I file a bug?
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20121008/baff58b8/attachment.html 


More information about the SciPy-User mailing list