Numpy.piecewise没有按预期工作

时间:2016-11-17 01:30:22

标签: numpy boolean-expression piecewise

In[2]: from numpy import *
In[3]: alpha = lambda x: piecewise(x,[x <= 4, 4 < x <= 24, x > 24], [10, 20, 50])
In[4]: print(alpha(5))
0
In[5]: print(alpha(3))
10
In[6]: print(alpha(26))
0

为什么这不起作用?有3个条件和3个功能

1 个答案:

答案 0 :(得分:1)

发现select做了我想要的

In[2]: from numpy import *
In[3]: alpha = lambda x: select([x <= 4, (4 < x) & (x <= 24), x > 24], [10, 20, 50])
In[4]: print(alpha(5))
20
In[5]: print(alpha(3))
10
In[6]: print(alpha(26))
50