将多个numpy样本乘以单个样本,然后沿沿着轴=1对输出求和

kqqjbcuj  于 2023-10-19  发布在  其他
关注(0)|答案(2)|浏览(129)

假设我有一个numpy数组(10,5),它是10个样本,5个特征。我如何将整个数组乘以一个样本(,5),并将输出按行求和,使我的输出为(10,),并且每个样本都有一个值。

z0qdvdin

z0qdvdin1#

import numpy as np

# Create your (10, 5) array (10 samples with 5 features)
array_10x5 = np.random.rand(10, 5)

# Create your single sample (1, 5)
single_sample = np.random.rand(1, 5)

# Multiply the entire (10, 5) array with the single sample using broadcasting
result = np.sum(array_10x5 * single_sample, axis=1)

# result is now a (10,) array where each element corresponds to the sum of the 
element-wise products
print(result)
vaj7vani

vaj7vani2#

import numpy as np

a1 = np.random.rand(10, 5)

a2 = np.random.rand(1, 5)

result = np.zeros((10, 1))
print(result)

product = a1 * a2
print(product)

for i in range(len(product)):
    result[i] = np.sum(product[i])

print(result)

我写这段代码是基于我对你所说的理解。这是你想要的输出吗?

相关问题