Search code examples
pythonnumpymultidimensional-arraymultiplicationelementwise-operations

hadamard product of uneven shaped arrays


I'm doing a whole bunch of hadamard products, as part of a machine learning project. To convey the problem, below is the setup:

# shape: (2, 3)
In [17]: arr1
Out[17]: 
array([[0.44486617, 0.21001534, 0.63833794],
       [0.90878526, 0.61692562, 0.01978946]])

# shape: (5, 3)
In [18]: arr2
Out[18]: 
array([[0.00640485, 0.22768134, 0.62845291],
       [0.58168743, 0.65527711, 0.14765079],
       [0.61389269, 0.38546809, 0.62696518],
       [0.73977707, 0.03737199, 0.45905132],
       [0.51932163, 0.00119124, 0.07241033]])

Now, I want to perform a hadamard product of each of the rows in arr1 with arr2 and thus obtain the resultant array, call it res, of shape (10, 3).

 (2, 3)
  *  | 
 (5, 3)
   ||
 (10,3)

How can we do this with the least possible overhead using only NumPy?


Solution

  • We can leverage broadcasting after extending one of the arrays to 3D -

    (a[:,None]*b).reshape(-1,a.shape[1]) # a,b are input arrays
    

    For large arrays, to achieve memory efficiency with multi-core usage and hence performance, we can make use of numexpr module -

    import numexpr as ne
    
    ne.evaluate('a3D*b',{'a3D':a[:,None]}).reshape(-1,a.shape[1])
    

    Timings -

    In [20]: a = np.random.rand(200,30)
    
    In [21]: b = np.random.rand(500,30)
    
    In [22]: %timeit (a[:,None]*b).reshape(-1,a.shape[1])
    100 loops, best of 3: 4.61 ms per loop
    
    In [27]: %timeit ne.evaluate('a3D*b',{'a3D':a[:,None]}).reshape(-1,a.shape[1])
    100 loops, best of 3: 2.28 ms per loop