Consider the following inputs
factors = np.array([[1, 2], [3, 4], [5, 6]])
weights = np.array([[2, 1, 0]]) # notice how this is a 2d array
First convert weights
into a 2D "Vector" so that you can multiply each term
weights = weights.T # [[2], [1], [0]]
Then you can just simply multiply by using the __mul__
dunder
new_factors = weights * factors # [[2, 4],[3, 4],[0, 0]]
Then you can just use np.array.sum
to sum each row
new_factors.sum(axis=1)
The output is
array([6, 7, 0])
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…