Search code examples
pythonmatrixmultiplication

Vector implementation of product of column vectors in python


Is there any vector implementation of multiplying columns in 2D-data to generate a single column which contains the product of all column values in python? For example [[1,2,3],[2,1,4],[1,7,3],[4,1,1]] to [6, 8, 21, 4]


Solution

  • Try np.multiply or np.prod

    a = np.array([[1,2,3],[2,1,4],[1,7,3],[4,1,1]])
    np.multiply.reduce(a, axis=1)
    

    OR

    np.prod(a, axis=1)
    

    array([ 6,  8, 21,  4])