Python:Numpy将数组的每一行与另一个数组的每一行相乘 [英] Python: Numpy Multiply each row of a array with each rows of another array

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

问题描述

我知道对此存在一些疑问,广播应该可以实现。但是,以某种方式,我并没有真正了解广播如何通过添加附加轴来实现。还有一个类似的问题,此处一个数组的每一列与另一数组的每一列相乘:将每一列与每一列相乘。就我而言,我想将2d阵列的每一行彼此相乘

I know there has been some questions about this and it should be possible with broadcasting. But somehow I dont really get how broadcasting works with adding ann additional axis. There is a similar question where each column of one array is multiplied with each column of another array here: Multiply each column with each column. In my case I want to multiply each row of the 2d arrays against each other

我只是创建了一个3维数组作为三角矩阵:

I simply have a 3 dimensional array created as the triangular matrix:

matrix = np.tril(np.ones((3,3,3),dtype='bool'))

为简单起见,请看一下前两个数组:

For simplification just look at the first two arrays:

matrix[:2]

[[[ True False False]
  [ True  True False]
  [ True  True  True]]

 [[ True False False]
  [ True  True False]
  [ True  True  True]]]

这应该变成:

[[[ True False False]
  [ True False False]
  [ True False False]]#First row of first array multiplied with each row of second array

 [[ True False False]
  [ True  True False]
  [ True  True False]]#Second row of first array multiplied with each row of second array

 [[ True False False]
  [ True  True False]
  [ True  True  True]]]#Third row of first array multiplied with each row of third array

我可以实现

matrix[0][None,...]*matrix[1][:,None])

但是如何在不循环的情况下对整个矩阵进行处理,广播如何工作?
然后应产生一个包含9个2d数组的3d数组。

But how can I do it for the whole matrix without looping and how is the broadcasting works? This should then result in an 3d array with 9 2d arrays.

EDIT

详细了解所有内容以及结果数组的外观。我有很多类别,可以说3。这3个类别中的所有类别都可以有4个状态,例如,每个状态都包含一个具有4个bool值的bool数组(也可以是10个类别,具有100个bool值的100个状态)。我现在想要一个可以索引到的结果数组。因此,例如,我想要所有3个类别的相乘状态的输出及其3个值。我将使用resultArray [0,0,0,1]将其索引为乘法的第二个布尔值。

For detailed explanation of what this is all about and how the resulting array should look like. I have a number of categories, lets say 3. All of this 3 categories could have 4 states, every state consists of a bool array with 4 bool values for example(Could also be 10 categories with 100 states with 100 bool values). I now want a resulting array which i can index to. So for example I want the output of the multiplied states of all 3 categories and the 3 value of it. I would index to it with resultingArray[0,0,0,1] for the second bool value of the multiplication.

具有3x4x4数组(3个类别,4个状态) ,4bool值),但要用数字可视化,则如下所示:

With a 3x4x4 array (3 caategories,4 states,4bool values) but for visualtization with numbers this would look like as follows:

cats = 3
values = 4

matrix = np.arange(48).reshape(cats,values,values)
matrix.shape


totalArray=np.zeros((values,values,values,values))
for row1 in range(len(matrix[0])):
    for row2 in range(len(matrix[1])):
        for row3 in range(len(matrix[2])):
            totalArray[row1,row2,row3] = matrix[0][row1]*matrix[1][row2]*matrix[2][row3]

print(matrix)            
print(totalArray)

输出

[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]
  [12 13 14 15]]

 [[16 17 18 19]
  [20 21 22 23]
  [24 25 26 27]
  [28 29 30 31]]

 [[32 33 34 35]
  [36 37 38 39]
  [40 41 42 43]
  [44 45 46 47]]]
[[[[    0.   561.  1224.  1995.]
   [    0.   629.  1368.  2223.]
   [    0.   697.  1512.  2451.]
   [    0.   765.  1656.  2679.]]

  [[    0.   693.  1496.  2415.]
   [    0.   777.  1672.  2691.]
   [    0.   861.  1848.  2967.]
   [    0.   945.  2024.  3243.]]

  [[    0.   825.  1768.  2835.]
   [    0.   925.  1976.  3159.]
   [    0.  1025.  2184.  3483.]
   [    0.  1125.  2392.  3807.]]

  [[    0.   957.  2040.  3255.]
   [    0.  1073.  2280.  3627.]
   [    0.  1189.  2520.  3999.]
   [    0.  1305.  2760.  4371.]]]


 [[[ 2048.  2805.  3672.  4655.]
   [ 2304.  3145.  4104.  5187.]
   [ 2560.  3485.  4536.  5719.]
   [ 2816.  3825.  4968.  6251.]]

  [[ 2560.  3465.  4488.  5635.]
   [ 2880.  3885.  5016.  6279.]
   [ 3200.  4305.  5544.  6923.]
   [ 3520.  4725.  6072.  7567.]]

  [[ 3072.  4125.  5304.  6615.]
   [ 3456.  4625.  5928.  7371.]
   [ 3840.  5125.  6552.  8127.]
   [ 4224.  5625.  7176.  8883.]]

  [[ 3584.  4785.  6120.  7595.]
   [ 4032.  5365.  6840.  8463.]
   [ 4480.  5945.  7560.  9331.]
   [ 4928.  6525.  8280. 10199.]]]


 [[[ 4096.  5049.  6120.  7315.]
   [ 4608.  5661.  6840.  8151.]
   [ 5120.  6273.  7560.  8987.]
   [ 5632.  6885.  8280.  9823.]]

  [[ 5120.  6237.  7480.  8855.]
   [ 5760.  6993.  8360.  9867.]
   [ 6400.  7749.  9240. 10879.]
   [ 7040.  8505. 10120. 11891.]]

  [[ 6144.  7425.  8840. 10395.]
   [ 6912.  8325.  9880. 11583.]
   [ 7680.  9225. 10920. 12771.]
   [ 8448. 10125. 11960. 13959.]]

  [[ 7168.  8613. 10200. 11935.]
   [ 8064.  9657. 11400. 13299.]
   [ 8960. 10701. 12600. 14663.]
   [ 9856. 11745. 13800. 16027.]]]


 [[[ 6144.  7293.  8568.  9975.]
   [ 6912.  8177.  9576. 11115.]
   [ 7680.  9061. 10584. 12255.]
   [ 8448.  9945. 11592. 13395.]]

  [[ 7680.  9009. 10472. 12075.]
   [ 8640. 10101. 11704. 13455.]
   [ 9600. 11193. 12936. 14835.]
   [10560. 12285. 14168. 16215.]]

  [[ 9216. 10725. 12376. 14175.]
   [10368. 12025. 13832. 15795.]
   [11520. 13325. 15288. 17415.]
   [12672. 14625. 16744. 19035.]]

  [[10752. 12441. 14280. 16275.]
   [12096. 13949. 15960. 18135.]
   [13440. 15457. 17640. 19995.]
   [14784. 16965. 19320. 21855.]]]]

问题是类别数组始终相等,三角矩阵。也许拥有一个三角形阵列并对其进行乘法就足够了。最后,我想给出一个索引列表数组,例如[[0,0,0,1],[0,0,0,2]],以获得该乘法的两个布尔值。

The thing is that the category arrays are always equal, the triangular matrix. Maybe it would be sufficient to have one triangular array and do the multiplication with it. At the end i want to give a array of indices lists like [[0,0,0,1],[0,0,0,2]] to get the two bool values for that multiplication.

推荐答案

您需要展开第二个轴以创建两个 4D 版本,并使其乘以彼此-

You need to spread out that second axis to create two 4D versions and let them multiply against each other -

matrix[:,None,:,:]*matrix[:,:,None,:]

或者简单地--

matrix[:,None]*matrix[...,None,:]



外部操作



示意图说明:

Outer-operation

Explanation with schematic :

我们希望沿彼此进行外元素相乘第二轴。因此,我们需要扩展轴并创建两个4D数组版本,以使单例(length = 1的轴)对应于另一个的全轴长版本。我们使用 np.newaxis / None 进行尺寸扩展。

We are looking to perform outer-elementwise multiplication against each other along the second axis. So, we need to extend axes and create two 4D array versions such that there's singleton(axis with length=1) corresponding to a full-axis-length version in another. We are doing this dimension-extension with np.newaxis/None.

考虑形状为<$的2D情况c $ c>(3,5):

matrix : 3 x 5

让我们沿着第二个轴进行外元素乘法。因此,数组的扩展将是-

Let's do outer-elementwise multiplication along the second axis. So, the extension of arrays would be -

matrix-version1 : 3 x 1 x 5
matrix-version2 : 3 x 5 x 1

类似地,为了沿第一个轴执行外元素乘法, -

Similarly, for performing outer-elementwise multiplication along the first axis, it would be -

matrix-version1 : 1 x 3 x 5
matrix-version2 : 3 x 1 x 5

因此,将其扩展到我们的 3D 情况下,沿第二轴进行元素逐次乘法,并假设形状为(m,n,r),则应为-

Thus, extending this to our 3D case for outer-elementwise multiplication along the second axis and assuming a shape of (m,n,r), it would be -

matrix-version1 : m x 1 x n x r # [:,None,:,:]
matrix-version2 : m x n x 1 x r # [:,:,None,:]

因此,在元素相乘后得出:

Hence, after elementwise multiplication resulting in :

output          : m x n x n x r

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆