A detailed explanation of the vector trinomial operators in numpy

  • 2021-01-19 22:19:02
  • OfStack

If data filtering is used, you can use the logical implementation of x if condition else y. If you are using pure Python, you can iterate through each set of element combinations and filter accordingly. However, if you use the vectorizer in numpy, you can greatly speed up the calculation process.

numpy.where There is a vector version of this 3-item operation numpy.where in numpy. where method can accept three parameters, the first parameter is a conditional vector, and the second parameter. The third parameter can be either a matrix or a scalar. Next, I will do the pure Python function implementation and vector implementation of the corresponding function.

The record is as follows:


In [76]: xarr = np.array([1.1,1.2,1.3,1.4,1.5])

In [77]: yarr = xarr + 1


In [78]: xarr
Out[78]: array([ 1.1, 1.2, 1.3, 1.4, 1.5])


In [79]: yarr
Out[79]: array([ 2.1, 2.2, 2.3, 2.4, 2.5])


In [80]: cond = np.array([True,False,True,True,False])


In [81]: cond
Out[81]: array([ True, False, True, True, False], dtype=bool)


In [82]: result1 = [(x if c else y) for x,y,c in zip(xarr,yarr,cond)]


In [83]: result1
Out[83]: [1.1000000000000001, 2.2000000000000002, 1.3, 1.3999999999999999, 2.5]


In [84]: result2 = np.where(cond,xarr,yarr)


In [85]: result2
Out[85]: array([ 1.1, 2.2, 1.3, 1.4, 2.5])

From the floating point representation, there is a small difference of one point, after the decimal point, usually in the numerical representation is negligible. However, it is important to make a one-size-one judgment of the next two results, because we have seen the machine-dependent differences in the floating-point representation of Python before.

The results of the test are as follows:


In [87]: result1 == result2
Out[87]: array([ True, True, True, True, True], dtype=bool)

As can be seen from the above results, the two calculations are uniform.


Related articles: