- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在学习关于模糊系统的类(class),我学习了 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 文件中,因此我也非常感谢在这些方面的任何帮助。
最佳答案
一般来说,numpy 数组非常擅长做一些明智的事情,当你只是像写数字一样编写代码时。链接比较是罕见的异常(exception)之一。您看到的错误本质上是这样的(被 piecewise
内部和 ipython 错误格式混淆了一点):
>>> a = np.array([1, 2, 3])
>>> 1.5 < a
array([False, True, True], dtype=bool)
>>>
>>> 1.5 < a < 2.5
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
>>>
>>> (1.5 < a) & (a < 2.5)
array([False, True, False], dtype=bool)
>>>
您也可以使用 np.logical_and
,但按位 and
在这里工作得很好。
就绘图而言,numpy 本身不做任何事情。这是一个使用 matplotlib 的示例:
>>> import numpy as np
>>> def piecew(x):
... conds = [x < 0, (x > 0) & (x < 1), (x > 1) & (x < 2), x > 2]
... funcs = [lambda x: x+1, lambda x: 1,
... lambda x: -x + 2., lambda x: (x-2)**2]
... return np.piecewise(x, conds, funcs)
>>>
>>> import matplotlib.pyplot as plt
>>> xx = np.linspace(-0.5, 3.1, 100)
>>> plt.plot(xx, piecew(xx))
>>> plt.show() # or plt.savefig('foo.eps')
注意 piecewise
是一个反复无常的野兽。特别是,它需要它的 x
参数是一个数组,如果不是,甚至不会尝试转换它(用 numpy
的说法:x
需要是 ndarray
,而不是 array_like
):
>>> piecew(2.1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in piecew
File "/home/br/.local/lib/python2.7/site-packages/numpy/lib/function_base.py", line 690, in piecewise
"function list and condition list must be the same")
ValueError: function list and condition list must be the same
>>>
>>> piecew(np.asarray([2.1]))
array([ 0.01])
关于python - numpy.piecewise 中的多个部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19578185/
我有一系列由类型级整数索引的数据类型,我以“分段”方式将它们定义为某个类型类的实例,这在尝试派生另一个类的实例时会导致问题。为了说明,我将问题隔离如下。考虑这段代码: {-# LANGUAGE Sco
在 sympy 中,如何声明一个对子函数中的多个变量具有多个限制的 Piecewise 函数? 这是我的背景和尝试: from sympy import Piecewise, Symbol, exp
我试图找到的傅里叶级数 像 simpy 一样: p = Piecewise((sin(t), 0 0) | (t < pi)), (0, (pi < t) | (t < 2*pi))), (t, 0
我正在尝试使用两个 x0 和 y0 坐标数组中的数据来创建一个使用提供的 x0 和 的函数>y0 计算分段的一系列线段。 为此,我创建了一个函数 import numpy as np import m
我正在学习关于模糊系统的类(class),我学习了 my notes在我的电脑上。这意味着我不得不时不时地在电脑上画图。由于这些图表定义明确,我觉得用 numpy 绘制它们是个好主意(我用 LaTeX
我希望从一组数据点生成一个最适合这些点的三次样条函数。 也就是说,寻求获得最适合数据的单个三阶多项式函数 - 而不是分段多项式。(最终目标是找到该函数二阶导数的最大值) 我已经尝试过scipy.int
我定义了一个分段复杂函数 import numpy as np def foo(x): return np.piecewise(x, [x>0], [np.exp(1j*x)]) 当我尝试评估
我想绘制以下累积分布函数 为此我想我可以使用 np.piecewise 如下 x = np.linspace(3, 9, 100) np.piecewise(x, [x 9], [0, float(
我的代码 我在一个较大的程序中有效地减少了以下代码片段: import sympy as sp print('sympy version:', sp.__version__) n = sp.symbo
我正在编写一个程序来进行三次样条插值。基本上,该程序将在某些间隔内拼凑三次多项式。如果可能的话,我想用piecewise[] 或其他类似的函数来绘制这个结果。 在我的代码中,我的方程在一个数组中,输出
我正在尝试使用 np.piecewise 定义一个多元分段函数,如下所示: X = np.array([ [1, 2], [3, 4], [5, 6] ]) pw = np.p
我正在尝试在 Mathematica 中绘制缓和函数的导数。它区分函数 OK,并且可以使用 % 绘制函数,但我希望能够通过将导数指定为函数 f[t_] 来绘制,然后 绘制[ f[t] , {t,-1,
我一直在尝试让 numpy.piecewise 将系数列表 {a_ij} 转换为分段三次多项式。每当我运行以下代码时, import math import numpy as np x = np.li
我想创建一个巨大的打包数据阵列,并将其保存在磁盘上。我正在使用 writePackedMessageToFd()。但是,由于输入数据非常大 (50GB),我需要将消息片段写入磁盘以释放内存。 Cap'
我正在尝试为一些数字信号处理构造一个分段函数,但我无法获得 numpy.piecewise 以允许我指定一个范围。 这里是我要输入的内容: t = np.arange(-10,10,1) x = la
我对为什么以下代码的答案是 10 而不是 1 感到困惑。有人可以帮助我了解 lambdify 发生了什么或者是什么导致了错误答案吗? import sympy from sympy.utilities
我想绘制一个分段函数,例如: import sympy as sym x = sym.symbols("x") f = sym.Piecewise((-1, x = 1)) sym.plotting.
我是一名优秀的程序员,十分优秀!