简体   繁体   English

遍历NumPy矩阵列表

[英]Iterate over list of NumPy matrices

I've got a list with row matrices: 我有一个带有行矩阵的列表:

rows = [ matrix([[1, 0, 0]]), matrix([[0, 1, 0]]), matrix([[0, 0, 1]]) ]

and I attempted to loop over these using for (a, b, c) in rows: , but instead of this working, I got an error: 并且我试图for (a, b, c) in rows:使用for (a, b, c) in rows:循环遍历这些,但是没有正常工作,却出现了一个错误:

ValueError: not enough values to unpack (expected 3, got 1)

The expected behaviour would be to unpack the three elements in the row to a, b, c : 预期的行为是将行中的三个元素解压缩为a, b, c

for (a, b, c) in rows:
    print(f"{a} {b} {c}")
> 1 0 0
> 0 1 0
> 0 0 1

Unfortunately, this would work on [1, 0, 0] , but not on [[1, 0, 0]] . 不幸的是,这在[1, 0, 0] ,但在[[1, 0, 0]]上无效。

I realized this is because they're [[doubly packed]] , but I was wondering if there was a simple solution to this issue? 我意识到这是因为它们被[[doubly packed]] ,但是我想知道是否有针对此问题的简单解决方案?

It seems like you want to unpack each matrix itself. 似乎您想解压缩每个矩阵本身。 In that case I would advise: Don't use matrix or NumPy. 在这种情况下,我建议:不要使用matrix或NumPy。 There is also no reason to use NumPy matrix at all. 也完全没有理由使用NumPy矩阵。 Even the NumPy documentation states that you should prefer arrays over matrices. 甚至NumPy文档都指出,您应该比矩阵更喜欢数组。 But in this case you should use plain lists, they are faster if you iterate over them or unpack them. 但是在这种情况下,您应该使用普通列表,如果对它们进行迭代或解压缩,它们会更快。

So you first want to iterate over each matrix and then you want to unpack each one of these. 因此,您首先要遍历每个矩阵,然后再解压缩每个矩阵。 But you have to do it in two steps: 但是您必须分两个步骤进行操作:

for mat in rows:      # this iterates over the list and gives you each matrix
    # this unpacks the matrix
    # to make it work faster I converted it to a list first using "tolist"
    # note that I needed to index it because it's a 2D matrix!
    a, b, c = mat.tolist()[0]
    print(a, b, c)  

It's also possible to do the two steps in one line: 也可以一行完成两个步骤:

for a, b, c in (mat.tolist()[0] for mat in rows):
    print(a, b, c)

A problem is that indexing a row of a matrix still gives a matrix. 问题在于,索引矩阵的行仍然会给出矩阵。 A way around that is to turn the matrix into a 1d array: 一种解决方法是将矩阵转换为一维数组:

In [368]: for r in rows:
     ...:     a,b,c=r.A1
     ...:     print(a,b,c)
     ...:     
1 0 0
0 1 0
0 0 1

Numpy matrix to array 块矩阵到数组


In [370]: for r in rows:
     ...:     print(r.shape,r[0].shape)
     ...:     
(1, 3) (1, 3)
(1, 3) (1, 3)
(1, 3) (1, 3)

I don't usually use or recommend unpacking values from an array. 我通常不使用或建议从数组中解包值。 That works against the generality of arrays. 这与数组的一般性背道而驰。 unpacking (unless using '*' terms) fixes the dimensions. 打开包装(除非使用“ *”术语)会固定尺寸。 But in general array can have several dimensions, and large sizes. 但是一般来说数组可以有多个尺寸,而且尺寸很大。

Also np.matrix is discouraged. 不建议使用np.matrix If these elements were np.array instead there wouldn't be this persistent 2d problem. 如果这些元素是np.array那么就不会存在这个持久的2d问题。

Since, we are using a,b,c to extract elements from each matrix's first row, it seems we are guaranteed to have 3 elements per row and it would be one per row for each of those matrices. 由于我们使用a,b,c从每个矩阵的第一行提取元素,因此似乎可以保证每行有3个元素,并且每个矩阵每个行将有3个元素。 So, one simple solution (as asked) would be to use the array constructor when using the loop and squeeze out the singleton nested level, like so - 因此,一种简单的解决方案(根据要求)是在使用循环时使用数组构造函数并挤出单例嵌套级别,如下所示:

for a,b,c in np.array(rows)[:,0]:

Or simpler np.squeeze() - 或更简单的np.squeeze() -

for a,b,c in np.squeeze(rows):

Note that this won't be the most memory efficient way but works as a simple way to extract those required scalar values. 请注意,这不是内存效率最高的方法,而是作为提取那些所需标量值的简单方法。

Here's a sample run - 这是一个示例运行-

In [375]: for a,b,c in np.squeeze(rows):
     ...:     print(a,b,c)
     ...:     
(1, 0, 0)
(0, 1, 0)
(0, 0, 1)

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

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