A few pieces in numpy.piecewise

I take a course on Fuzzy Systems and take my notes on my computer. This means that from time to time I have to draw graphics on my computer. Since these graphs are reasonably well defined, I believe that creating them using numpy would be a good idea (I take notes using LaTeX and I am pretty quick with the python shell, so I suppose I can handle this).

Graphs for fuzzy membership functions are very piecewise, for example:

Fuzzy Membership Function

To build this, I tried the following code for numpy.piecewise (which gives me a cryptic error):

 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 

At this point, I'm pretty confused about how to build this function. I really don't understand the error message, which further complicates my attempts to debug this.

Ultimately, I am looking to create and export this function to an EPS file, so I would appreciate any help on these lines.

+6
source share
1 answer

In general, numpy arrays are very good at doing reasonable things when you just write code, as if they were just numbers. Chain comparisons are one of the rare exceptions. The error you see is essentially this (obfuscating bits using piecewise internal elements and formatting ipython errors):

 >>> 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) >>> 

Alternatively, you can use np.logical_and , but bitwise and works just fine.

As for plotting, numpy itself does not. Here is an example with 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') 

Note that piecewise is a moody beast. In particular, he needs the argument x as an array, and he will not even try to convert it if it is not (in numpy parlance: x should be ndarray , not 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]) 
+5
source

Source: https://habr.com/ru/post/956685/


All Articles