简体   繁体   English

将2D数组中的每列与另一个2D数组中的每列相乘

[英]Multiply each column from 2D array with each column from another 2D array

I have two Numpy arrays x with shape (m, i) and y with shape (m, j) (so the number of rows is the same). 我有两个Numpy数组x ,形状(m, i)y形状(m, j) (因此行数相同)。 I would like to multiply each column of x with each column of y element-wise so that the result is of shape (m, i*j) . 我想将x的每一列与y元素的每一列相乘,以便结果具有形状(m, i*j)

Example: 例:

import numpy as np

np.random.seed(1)
x = np.random.randint(0, 2, (10, 3))
y = np.random.randint(0, 2, (10, 2))

This creates the following two arrays x : 这将创建以下两个数组x

array([[1, 1, 0],
       [0, 1, 1],
       [1, 1, 1],
       [0, 0, 1],
       [0, 1, 1],
       [0, 0, 1],
       [0, 0, 0],
       [1, 0, 0],
       [1, 0, 0],
       [0, 1, 0]])

and y : y

array([[0, 0],
       [1, 1],
       [1, 1],
       [1, 0],
       [0, 0],
       [1, 1],
       [1, 1],
       [1, 1],
       [0, 1],
       [1, 0]])

Now the result should be: 现在的结果应该是:

array([[0, 0, 0, 0, 0, 0],
       [0, 0, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1],
       [0, 0, 0, 0, 1, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 1, 1],
       [0, 0, 0, 0, 0, 0],
       [1, 1, 0, 0, 0, 0],
       [0, 1, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 0]])

Currently, I've implemented this operation with two nested loops over the columns of x and y : 目前,我已经在xy的列上使用两个嵌套循环实现了此操作:

def _mult(x, y):
    r = []
    for xc in x.T:
        for yc in y.T:
            r.append(xc * yc)
    return np.array(r).T

However, I'm pretty sure that there must be a more elegant solution that I can't seem to come up with. 但是,我很确定必须有一个更优雅的解决方案,我似乎无法想出。

Use NumPy broadcasting - 使用NumPy broadcasting -

(y[:,None]*x[...,None]).reshape(x.shape[0],-1)

Explanation 说明

As inputs, we have - 作为投入,我们有 -

y : 10 x 2
x : 10 x 3

With y[:,None] , we are introducing a new axis between the existing two dims, thus creating a 3D array version of it. 使用y[:,None] ,我们在现有的两个dims之间引入了一个新轴,从而创建了它的3D数组版本。 This keeps the first axis as the first one in 3D version and pushes out the second axis as the third one. 这使第一个轴保持为3D版本中的第一个轴,并将第二个轴作为第三个轴推出。

With x[...,None] , we are introducing a new axis as the last one by pushing up the two existing dims as the first two dims to result in a 3D array version. 使用x[...,None] ,我们引入一个新轴作为最后一个轴,通过将两个现有的dims作为前两个dims推高以产生3D阵列版本。

To summarize, with the introduction of new axes, we have - 总而言之,随着新轴的引入,我们有 -

y : 10 x 1 x 2
x : 10 x 3 x 1

With y[:,None]*x[...,None] , there would be broadcasting for both y and x , resulting in an output array with a shape of (10,3,2) . 使用y[:,None]*x[...,None]yx都会有broadcasting ,从而产生一个形状为(10,3,2)的输出数组。 To get to the final output array of shape (10,6) , we just need to merge the last two axes with that reshape. 要获得形状(10,6)的最终输出数组,我们只需要将最后两个轴与该重塑合并。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python / Numpy - 矩阵乘以2D阵列和另一个2D阵列的每一行 - Python/Numpy - Matrix Multiply a 2D Array and Each Row of another 2D Array 2D 到 1D numpy 数组,每行的列索引 - 2D to 1D numpy array with indices of column for each row 如何从二维数组+最大值索引中获取每一列的最大值 - how to get the max of each column from an 2d array + index of the max value 2d数组的每列的第一个和最后一个数字1的索引 - Index of the first and last numbers 1 of each column of an 2d array Python-使用每列对2D数组排序 - Python - Sorting a 2D array using each column 在2D NumPy数组中将每列岛屿缩放到它们的长度 - Scale each column of islands to their lengths in 2D NumPy array 从二维数组中选择行而不是在另一个二维数组中 - selecting rows from 2D array not in another 2D array 从更大的2D数组中提取每个2D正方形子数组的对角线 - Extract diagonals of each 2D square sub-array from a larger 2D array 通过从每个数组中选取 1 列来堆叠多个二维数组 - Stacking multiple 2d arrays by picking 1 column from each of the arrays 针对lexsort的2D数组广播1D数组:当考虑另一个向量时,用于独立地对每列进行排序的排列 - Broadcast 1D array against 2D array for lexsort : Permutation for sorting each column independently when considering yet another vector
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM