本文介绍了将numpy数组的每一列与另一个数组的每个值相乘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下两个numpy数组:

Suppose I have the following two numpy arrays:

In [251]: m=np.array([[1,4],[2,5],[3,6]])

In [252]: m
Out[252]:
array([[1, 4],
       [2, 5],
       [3, 6]])

In [253]: c= np.array([200,400])

In [254]: c
Out[254]: array([200, 400])

我想一步一步得到以下数组,但是我一生无法弄清楚:

I would like to get the following array in one step, but for the life of me I cannot figure it out:

In [252]: k
Out[252]:
array([[200, 800, 400, 1600],
       [400, 1000, 800, 2000],
       [600, 1200, 1200,2400]])

推荐答案

所需的转换称为Kronecker产品. Numpy的功能为numpy.kron:

The transformation you want is called the Kronecker product. Numpy has this function as numpy.kron:

In [1]: m = np.array([[1,4],[2,5],[3,6]])

In [2]: c = np.array([200,400])

In [3]: np.kron(c, m)
Out[3]:
array([[ 200,  800,  400, 1600],
       [ 400, 1000,  800, 2000],
       [ 600, 1200, 1200, 2400]])

这篇关于将numpy数组的每一列与另一个数组的每个值相乘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 06:02