将n个NumPy样本乘以单个样本,并沿沿着轴对输出求和=1

yvfmudvl  于 2023-11-18  发布在  其他
关注(0)|答案(2)|浏览(130)

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

rdlzhqv9

rdlzhqv91#

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)

字符串

bmp9r5qi

bmp9r5qi2#

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)

字符串
我根据你所说的理解写了这段代码。这是你想要的输出吗?

相关问题