【发布时间】:2013-10-24 22:53:51
【问题描述】:
我正在学习关于模糊系统的课程,并在我的计算机上学习my notes。这意味着我必须不时在我的电脑上绘制图表。由于这些图的定义非常明确,我觉得用numpy 绘制它们是个好主意(我用 LaTeX 做笔记,而且我在 python shell 上很快,所以我想我可以摆脱这个) .
fuzzy membership functions 的图是高度分段的,例如:
为了绘制这个,我尝试了numpy.piecewise 的以下代码(这给了我一个神秘的错误):
In [295]: a = np.arange(0,5,1)
In [296]: condlist = [[b<=a<b+0.25, b+0.25<=a<b+0.75, b+0.75<=a<b+1] for b in range(3)]
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-296-a951e2682357> in <module>()
----> 1 condlist = [[b<=a<b+0.25, b+0.25<=a<b+0.75, b+0.75<=a<b+1] for b in range(3)]
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
In [297]: funclist = list(itertools.chain([lambda x:-4*x+1, lambda x: 0, lambda x:4*x+1]*3))
In [298]: np.piecewise(a, condlist, funclist)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-298-41168765ae55> in <module>()
----> 1 np.piecewise(a, condlist, funclist)
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/function_base.pyc in piecewise(x, condlist, funclist, *args, **kw)
688 if (n != n2):
689 raise ValueError(
--> 690 "function list and condition list must be the same")
691 zerod = False
692 # This is a hack to work around problems with NumPy's
ValueError: function list and condition list must be the same
此时,我对如何绘制此函数感到相当困惑。我不太理解错误消息,这进一步阻碍了我调试它的努力。
最终,我希望绘制此函数并将其导出到 EPS 文件中,因此我也希望能在这些方面提供任何帮助。
【问题讨论】:
-
尝试打破链式条件,astackoverflow.com/questions/10377096/…
-
@Zhenya 说了什么。例如。
b <= a < b + 0.25必须写成(b <= a) & (a < b + 0.25)。
标签: python numpy plot piecewise