Python Stupid numpy.piecewise

Python Stupid numpy.piecewise

The piecewise() function in numpy is not very good. Due to the writing of the function, one wouldn’t be surprised to encounter the following error,

if (n != n2):
    raise ValueError(
        "function list and condition list must be the same")

in which n is the length of condition list and n2 is the length of function list.

To avoid it, the input should always be prepared as following

x = np.asarray(x)
# The following is important to avoid the weird behavior of piecewise()
if not x.shape:
    x = np.asarray([x])

Planted: by ;

L Ma (2015). 'Python Stupid numpy.piecewise', Datumorphism, 12 April. Available at: https://datumorphism.leima.is/til/programming/python/python-stupid-numpy-piecewise/.