Skip to main content
 首页 » 编程设计

python之使用 np.piecewise 进行均匀分布

2025年02月15日9kerrycode

我想绘制以下累积分布函数

为此我想我可以使用 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() 

我该怎么做?

请您参考如下方法:

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 .