在像下面这样的情况下,我如何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]发布于 2021-07-09 02:05:44
当心那些"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)发布于 2021-07-08 23:41:17
我只能使用NumPy解决这个问题。因为NumPy比python的列表函数(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]]]然后,我编写了一个函数来解压a中的每个数组,并删除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]]https://stackoverflow.com/questions/68301270
复制相似问题