2017-02-14 118 views
2

我想绘制以下累积分布函数使用np.piecewise为均匀分布

enter image description here

而要做到这一点,我想我可以用np.piecewise如下

x = np.linspace(3, 9, 100) 
np.piecewise(x, [x < 3, 3 <= x <= 9, x > 9], [0, float((x - 3))/(9 - 3), 1]) 

但这给出以下错误

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() 

如何我可以这样做吗?

回答

1

np.piecewise是一个反复无常的野兽。

用途:

x = np.linspace(3, 9, 100) 
cond = [x < 3, (3 <= x) & (x <= 9), x > 9]; 
func = [0, lambda x : (x - 3)/(9 - 3), 1]; 
np.piecewise(x, cond, func) 

说明here