如何对不同形状的矩阵进行vstack或连接?

mfuanj7w  于 2021-08-25  发布在  Java
关注(0)|答案(2)|浏览(281)

在下面这样的情况下,如何对这两个矩阵进行vstack?

import numpy as np 

a = np.array([[3,3,3],[3,3,3],[3,3,3]])
b = np.array([[2,2],[2,2],[2,2]])

a = np.vstack([a, b])

Output:   
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 3 and the array at index 1 has size 2

我想要的输出如下所示:

a = array([[[3, 3, 3],
            [3, 3, 3],
            [3, 3, 3]],
           [[2, 2],
            [2, 2],
            [2, 2]]])

我的目标是循环遍历堆叠矩阵的内容,为每个矩阵编制索引,并调用特定行上的函数。

for matrix in a:
   row = matrix[1]
   print(row)

Output: 
[3, 3, 3]
[2, 2]
enyaitl3

enyaitl31#

小心那些“numpy更快”的说法。如果您已经有了数组,并且充分利用了数组方法, numpy 速度确实更快。但是如果您从列表开始,或者必须使用python级别的迭代(就像您在 Pack... ),即 numpy 版本可能会慢一些。
只是在电脑上做时间测试 Pack 步骤:

In [12]: timeit Pack_Matrices_with_NaN([a,b,c],5)
221 µs ± 9.02 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

将其与使用简单列表获取每个数组的第一行进行比较:

In [13]: [row[1] for row in [a,b,c]]
Out[13]: [array([3., 3., 3.]), array([2., 2.]), array([4., 4., 4., 4.])]
In [14]: timeit [row[1] for row in [a,b,c]]
808 ns ± 2.17 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

200µs而不是1µs!
和你的时间 Unpack :

In [21]: [Unpack_Matrix_with_NaN(packed_matrices.reshape(3,3,5),i)[1,:] for i in range(3)]
    ...: 
Out[21]: [array([3., 3., 3.]), array([2., 2.]), array([4., 4., 4., 4.])]
In [22]: timeit [Unpack_Matrix_with_NaN(packed_matrices.reshape(3,3,5),i)[1,:] for i in ra
    ...: nge(3)]
199 µs ± 10.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
pepwfjgg

pepwfjgg2#

我只能用numpy解决这个问题。因为numpy比python的list函数快得多(https://towardsdatascience.com/how-fast-numpy-really-is-e9111df44347)我想分享我的答案,因为它可能对其他人有用。
我从添加np.nan开始,使两个数组的形状相同。

import numpy as np 

a = np.array([[3,3,3],[3,3,3],[3,3,3]]).astype(float)
b = np.array([[2,2],[2,2],[2,2]]).astype(float)

# Extend each vector in array with Nan to reach same shape

b = np.insert(b, 2, np.nan, axis=1)

# Now vstack the arrays

a = np.vstack([[a], [b]])
print(a)

Output: 
[[[ 3.  3.  3.]
  [ 3.  3.  3.]
  [ 3.  3.  3.]]

 [[ 2.  2. nan]
  [ 2.  2. nan]
  [ 2.  2. nan]]]

然后我编写了一个函数,将每个数组解包到一个数组中,并删除nan。

def Unpack_Matrix_with_NaN(Matrix_with_nan, matrix_of_interest):
    for first_row in Matrix_with_nan[matrix_of_interest,:1]:
        # find shape of matrix row without nan 
        first_row_without_nan = first_row[~np.isnan(first_row)]
        shape = first_row_without_nan.shape[0]
        matrix_without_nan = np.arange(shape)
        for row in Matrix_with_nan[matrix_of_interest]:
            row_without_nan = row[~np.isnan(row)]
            matrix_without_nan = np.vstack([matrix_without_nan, row_without_nan])
        # Remove vector specifying shape 
        matrix_without_nan = matrix_without_nan[1:]
        return matrix_without_nan

然后我可以在矩阵中循环,找到我想要的行,然后打印出来。

Matrix_with_nan = a

for matrix in range(len(Matrix_with_nan)):
    matrix_of_interest = Unpack_Matrix_with_NaN(a, matrix)
    row = matrix_of_interest[1]
    print(row)

Output: 
[3. 3. 3.]
[2. 2.]

我还创建了一个函数,用于在每行需要添加多个nan时打包矩阵:

import numpy as np 

a = np.array([[3,3,3],[3,3,3],[3,3,3]]).astype(float)
b = np.array([[2,2],[2,2],[2,2]]).astype(float)
c = np.array([[4,4,4,4],[4,4,4,4],[4,4,4,4]]).astype(float)

# Extend each vector in array with Nan to reach same shape

def Pack_Matrices_with_NaN(List_of_matrices, Matrix_size):
    Matrix_with_nan = np.arange(Matrix_size)
    for array in List_of_matrices:
        start_position = len(array[0])
        for x in range(start_position,Matrix_size):
            array = np.insert(array, (x), np.nan, axis=1)
        Matrix_with_nan = np.vstack([Matrix_with_nan, array])
    Matrix_with_nan = Matrix_with_nan[1:]
    return Matrix_with_nan

arrays = [a,b,c]
packed_matrices = Pack_Matrices_with_NaN(arrays, 5)
print(packed_matrices) 

Output:
[[ 3.  3.  3. nan nan]
 [ 3.  3.  3. nan nan]
 [ 3.  3.  3. nan nan]
 [ 2.  2. nan nan nan]
 [ 2.  2. nan nan nan]
 [ 2.  2. nan nan nan]
 [ 4.  4.  4.  4. nan]
 [ 4.  4.  4.  4. nan]
 [ 4.  4.  4.  4. nan]]

相关问题