使用np.piecewise进行均匀分布

时间:2017-02-14 16:21:22

标签: python numpy

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

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 个答案:

答案 0 :(得分: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