python 中列向量乘积的向量实现

Vector implementation of product of column vectors in python

是否有任何矢量实现在二维数据中乘以列以生成包含 python 中所有列值的乘积的单个列? 例如 [[1,2,3],[2,1,4],[1,7,3],[4,1,1]] 到 [6, 8, 21, 4]

尝试np.multiplynp.prod

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

np.prod(a, axis=1)

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

从 pandas

开始尝试 product
L = [[1,2,3],[2,1,4],[1,7,3],[4,1,1]]
pd.DataFrame(L).product(axis=1).to_list()
# [6, 8, 21, 4]

您可以选择使用 functools 中的 reduce。此函数以累积方式“减少”应用操作的列表。操作将成为产品,作为 lambda 实现,它总是更 pythonic。

from functools import reduce
x = [[1,2,3],[2,1,4],[1,7,3],[4,1,1]]
y = [reduce(lambda x,y: x*y, element) for element in x] #=[6, 8, 21, 4]