[英]python, plot triangular function into 2d arrays
I'm new to python, in this case, I want to put my function into 2d arrays, so I can plot the function. 我是python的新手,在这种情况下,我想将函数放入2d数组中,以便可以绘制函数。 Here is my triangle function, I'm using it for fuzzy logic:
这是我的三角函数,正在将其用于模糊逻辑:
def triangle (z,a,b,c):
if (z<=a) | (z>=c):
y = 0
elif (a<=z) & (z<=b):
y = (z-a) / (b-a)
elif (b<=z) & (z<=c):
y = (b-z) / (c-b)
return y
and I'm trying to make the array using numpy, np.linspace
but I can't get it done, I've tried to use the fuzzy library, but nothing works. 我试图使用numpy,
np.linspace
制作数组,但无法完成,我试图使用模糊库,但没有任何效果。
Looks like a, b, c
are constants and z
is a np.linspace
between a
, and c
. 看起来
a, b, c
是常量,而z
是a和c
之间a
np.linspace
。
You can make use of Boolean Indexing , SciPy cookbook/Indexing 您可以使用布尔索引 , SciPy食谱/索引
a = 1
b = 2
c = 3
def triangle (z, a = a, b = b, c = c):
y = np.zeros(z.shape)
y[z <= a] = 0
y[z >= c] = 0
first_half = np.logical_and(a < z, z <= b)
y[first_half] = (z[first_half]-a) / (b-a)
second_half = np.logical_and(b < z, z < c)
y[second_half] = (c-z[second_half]) / (c-b)
return y
z = np.linspace(a, c, num = 51)
y = triangle(z, a, b, c)
q = np.vstack((z, y)) # shape = (2, 50) ... [[z, z, z, ...], [y, y, y, ...]]
q = q.T # shape = (50, 2) ... [[z, y], [z, y], ....]
When you use a numpy ndarray in a comparison expression the result is a boolean array: 在比较表达式中使用numpy ndarray时,结果是一个布尔数组:
>>> q = np.linspace(0, 20, num = 50)
>>> print(q)
[ 0. 0.40816327 0.81632653 1.2244898 1.63265306
2.04081633 2.44897959 2.85714286 3.26530612 3.67346939
4.08163265 4.48979592 4.89795918 5.30612245 5.71428571
6.12244898 6.53061224 6.93877551 7.34693878 7.75510204
8.16326531 8.57142857 8.97959184 9.3877551 9.79591837
10.20408163 10.6122449 11.02040816 11.42857143 11.83673469
12.24489796 12.65306122 13.06122449 13.46938776 13.87755102
14.28571429 14.69387755 15.10204082 15.51020408 15.91836735
16.32653061 16.73469388 17.14285714 17.55102041 17.95918367
18.36734694 18.7755102 19.18367347 19.59183673 20. ]
>>> print(q < 5)
[ True True True True True True True True True True True True
True False False False False False False False False False False False
False False False False False False False False False False False False
False False False False False False False False False False False False
False False]
>>> print(q > 15)
[False False False False False False False False False False False False
False False False False False False False False False False False False
False False False False False False False False False False False False
False True True True True True True True True True True True
True True]
>>> print(np.logical_and(q > 5, q < 15))
[False False False False False False False False False False False False
False True True True True True True True True True True True
True True True True True True True True True True True True
True False False False False False False False False False False False
False False]
>>>
You can use a boolean array to select portions of an array that meet your condition: 您可以使用布尔数组选择满足条件的数组部分:
>>> q[np.logical_and(q > 7, q < 11)]
array([ 7.34693878, 7.75510204, 8.16326531, 8.57142857,
8.97959184, 9.3877551 , 9.79591837, 10.20408163, 10.6122449 ])
>>>
When you use boolean indexing in an assignment statement the right-hand-side is only assigned to the indices where the comparison is True
: 当在赋值语句中使用布尔索引时, 右侧仅分配给比较为
True
的索引:
>>> q[np.logical_and(q > 7, q < 11)] = -1
>>> print(q)
[ 0. 0.40816327 0.81632653 1.2244898 1.63265306
2.04081633 2.44897959 2.85714286 3.26530612 3.67346939
4.08163265 4.48979592 4.89795918 5.30612245 5.71428571
6.12244898 6.53061224 6.93877551 -1. -1. -1. -1.
-1. -1. -1. -1. -1. 11.02040816
11.42857143 11.83673469 12.24489796 12.65306122 13.06122449
13.46938776 13.87755102 14.28571429 14.69387755 15.10204082
15.51020408 15.91836735 16.32653061 16.73469388 17.14285714
17.55102041 17.95918367 18.36734694 18.7755102 19.18367347
19.59183673 20. ]
>>>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.