2017-02-14 66 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