Numpy vs. Overloaded Operator

I have two numpy arrays containing objects with an overloaded comparison operator that returns another object instead of True or False. How to create an array containing the results of individual comparisons. I would like the result to be an array of objects, as in the following

lhs = ... # np.array of objects with __le__ overloaded
rhs = ... # another np.array
result = np.array([l <= r for l, r in izip(lhs, rhs)])

but lhs <= rhsgives me an array of bools. Is there a way to get an resultarray of method invocation results __le__without writing a python loop?

+4
source share
1 answer

Page Numpy Github on ndarrayindicates that the comparison operators are equivalent forms ufunc in Numpy. Therefore lhs <= rhsequivalentnp.less_equal(lhs, rhs)

np.info(np.less_equal)

 Returns
 ------- out : bool or ndarray of bool
   Array of bools, or a single bool if `x1` and `x2` are scalars.

, :

import operator
result = np.vectorize(operator.le)(lhs, rhs)

np.vectorize Numpy . numpy , .

+3

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


All Articles