简体   繁体   English

Numpy 将二维 arrays 附加在一起

[英]Numpy appending two-dimensional arrays together

I am trying to create a function which exponentiates a 2-D matrix and keeps the result in a 3D array, where the first dimension is indexing the exponent.我正在尝试创建一个 function 对二维矩阵求幂并将结果保存在 3D 数组中,其中第一个维度是指数的索引。 This is important because the rows of the matrix I am exponentiating represent information about different vertices on a graph.这很重要,因为我正在求幂的矩阵的行表示有关图上不同顶点的信息。 So for example if we have A, A^2, A^3, each is shape (50,50) and I want a matrix D = (3,50,50) so that I can go D[:,1,:] to retrieve all the information about node 1 and be able to do matrix multiplication with that.因此,例如,如果我们有 A、A^2、A^3,每个都是形状 (50,50),我想要一个矩阵 D = (3,50,50),这样我就可以 go D[:,1,: ] 检索有关节点 1 的所有信息,并能够对其进行矩阵乘法。 My code is currently as我的代码目前是

def expo(times,A,n):
    temp = A;
    result = csr_matrix.toarray(temp)
    for i in range(0,times):
        temp = np.dot(temp,A)
        if i == 0:
            result = np.array([result,csr_matrix.toarray(temp)]) # this creates a (2,50,50) array
        if i > 0:
            result = np.append(result,csr_matrix.toarray(temp),axis=0) # this does not work
    return result

However, this is not working because in the "i>0" case the temp array is of the shape (50,50) and cannot be appended.但是,这不起作用,因为在“i>0”的情况下,临时数组的形状为 (50,50) 并且无法附加。 I am not sure how to make this work and I am rather confused by the dimensionality in Numpy, eg why thinks are (50,1) sometimes and just (50,) other times.我不确定如何完成这项工作,而且我对 Numpy 中的维度感到困惑,例如为什么有时认为是 (50,1) 而其他时候只是 (50,)。 Would anyone be able to help me make this code work and explain generally how these things should be done in Numpy?任何人都可以帮助我使此代码正常工作并大致解释这些事情应该如何在 Numpy 中完成?

Documentation reference文档参考

If you want to stack matrices in numpy, you can use the stack function .如果要在 numpy 中堆叠矩阵,可以使用stack function If you also want the index to correspond to the exponent, you might want to add a unity matrix to the beginning of your output:如果您还希望索引对应于指数,您可能需要在 output 的开头添加一个单位矩阵:

MWE MWE

import numpy as np

def expo(A, n):
    result =[np.eye(len(A)), A,]
    for _ in range(n-1):
        result.append(result[-1].dot(A))

    return np.stack(result, axis=0) 
    # If you do not really need the 3D array, 
    # you could also just return the list


result = expo(np.array([[1,-2],[-2,1]]), 3)
print(result)
# [[[  1.   0.]
#   [  0.   1.]]
#
#  [[  1.  -2.]
#   [ -2.   1.]]
#
#  [[  5.  -4.]
#   [ -4.   5.]]
#
#  [[ 13. -14.]
#   [-14.  13.]]]

print(result[1])
# [[ 1. -2.]
#  [-2.  1.]]

Comments注释

As you can see, we first simply create the list of matrices, and then convert them to an array at the end.如您所见,我们首先简单地创建矩阵列表,然后在最后将它们转换为数组。 I am not sure if you really need the 3D array though, as you could also just index the list that was created, but that depends on your use case, if that is convenient or not.我不确定您是否真的需要 3D 数组,因为您也可以只索引创建的列表,但这取决于您的用例,如果方便与否。

I guess the axis keyword argument for a lot of numpy functions can be confusing at first, but the documentation usually has good examples that combined with same trial and error, should get you pretty far.我猜想很多 numpy 函数的axis关键字参数一开始可能会令人困惑,但文档通常有很好的例子,结合相同的试验和错误,应该会让你走得很远。 For example for numpy.stack , the very first example is indeed exactly what you want to do.例如对于numpy.stack ,第一个示例确实正是您想要做的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM