Use Numpy.frompyfunc To Add Broadcasting To A Python Function With Argument
Solution 1:
Converting vline
to a numpy ufunc fundamentally doesn't make sense, since ufuncs are always applied to numpy arrays in an elementwise fashion. Because of this, the input arguments must either have the same shape, or must be broadcastable to the same shape. You are passing two arrays with incompatible shapes to your ufunc_vline
function (db.shape == (6, 4)
and mask.shape == (3,)
), hence the ValueError
you are seeing.
There are a couple of other issues with ufunc_vline
:
np.frompyfunc(vline, 2, 1)
specifies thatvline
should return a single output argument, whereasvline
actually returns nothing (but modifiesout
in place).You are passing
db
as the first argument toufunc_vline
, whereasvline
expects the first argument to beidx
, which is used as an index into the rows ofdb
.
Also, bear in mind that creating a ufunc from a Python function using np.frompyfunc
will not yield any noticeable performance benefit over a standard Python for
loop. To see any serious improvement you would probably need to code the ufunc in a low-level language such as C (see this example in the documentation).
Having said that, your vline
function can be easily vectorized using standard boolean array operations:
def vline_vectorized(db, mask):
return db[:, 0] & np.all((mask & db[:, 1:]) == mask, axis=1)
For example:
db = np.array([ # out for mask = [1, 0, 1]
# target, vector #
[1, 1, 0, 1], # 1
[0, 1, 1, 1], # 0 (fit to mask but target == 0)
[0, 0, 1, 0], # 0
[1, 1, 0, 1], # 1
[0, 1, 1, 0], # 0
[1, 0, 0, 0], # 0
])
mask = np.array([1, 0, 1])
print(repr(vline_vectorized(db, mask)))
# array([1, 0, 0, 1, 0, 0])
Post a Comment for "Use Numpy.frompyfunc To Add Broadcasting To A Python Function With Argument"