简体   繁体   English

在python列表中展平数组

[英]flatten arrays in a list in python

I have multiple numpy masked arrays arr0, arr1, ..., arrn . 我有多个numpy蒙版数组arr0, arr1, ..., arrn

I put them in a list arrs = [arr0, ..., arrn] . 我将它们放在列表中arrs = [arr0, ..., arrn]

I want to flatten these arrays et put a mask on them. 我想弄平这些阵列并在它们上面放一个面具。 I did something like: 我做了类似的事情:

for arr in arrs:
    arr = np.ravel(arr)
    arr[mask] = ma.masked

I do not understand when Python make copies and when it is just a pointer. 我不明白什么时候Python制作副本,什么时候它只是一个指针。 This for loop does not flatten the arr0, ..., arrn , (whereas ravel outputs a view and not a copy) it just flattens the variable arr , although it does change their mask ! 这个for循环不会使arr0, ..., arrn变平(而arr0, ..., arrn输出一个视图而不是一个副本),它只是变平了变量arr ,尽管它确实改变了它们的掩码!

As I understand it, arr is a view of the elements in the list arrs , so when I change elements of arr it changes the elements of the corresponding array in the list. 据我了解, arr是列表arrs元素的视图,因此当我更改arr的元素时,它将更改列表中相应数组的元素。 But when I assign a new value to arr it does not change the original array, even if the assignement is supposed to be a view of this array. 但是,当我为arr分配一个新值时,即使分配被认为是该数组的视图,它也不会更改原始数组。 Why ? 为什么呢

Edit with an example : 用示例编辑

Arrays to flatten: 要展平的数组:

arr0 = masked_array(data=[[1,2],[3,4]], mask=False)
arr1 = masked_array(data=[[5,6],[7,8]], mask=False)
mask = [[False,True],[True,False]]

Expected output: 预期产量:

arr0 = masked_array(data=[[1,--],[--,4]], mask=[[False,True],[True,False]])
arr1 = masked_array(data=[[5,--],[--,8]], mask=[[False,True],[True,False]])

I'd like to do this in a loop because I have a lot of arrays (15 more or less), and I want to use the arrays name in the code. 我想循环执行此操作,因为我有很多数组(多于或少于15个),并且我想在代码中使用数组名称。 Is there no other way than do to: 除了:

arr0 = np.ravel(arr0)
...
arrn = np.ravel(arrn)
In [1032]: arr0 = np.ma.masked_array(data=[[1,2],[3,4]], mask=False)    
In [1033]: arr1 = np.ma.masked_array(data=[[5,6],[7,8]], mask=False)

This is the basic way of iterating over a list, applying some action to each element, and collecting the results in another list: 这是遍历列表,对每个元素执行一些操作以及将结果收集到另一个列表中的基本方法:

In [1037]: ll=[arr0,arr1]    
In [1038]: ll1=[]
In [1047]: for a in ll:
    a1=a.flatten()   # makes a copy
    a1.mask=mask
    ll1.append(a1)

In [1049]: ll1
Out[1049]: 
[masked_array(data = [1 -- -- 4], mask = [False  True  True False],
        fill_value = 999999), 
        masked_array(data = [5 -- -- 8], mask = [False  True  True False],
        fill_value = 999999)]

Often that can be writen a list comprehension 通常可以写成列表理解

 [foo(a) for a in alist]

but the action here isn't a neat function 但是这里的动作不是一个整洁的功能

If I use ravel instead, a1 is a view (not a copy), and applying mask to it changes the mask of a as well - the result is changed masks for arr0 , but no change in shape: 如果我使用ravel代替, a1是一个视图(不是副本),并应用口罩,它改变了面具a ,以及-结果被改变口罩arr0 ,但在形状上的变化:

In [1051]: for a in ll:
   ......:     a1=a.ravel()
   ......:     a1.mask=mask

(the same happens with your a=a.ravel() . The a= assigns a new value to a , breaking the link to the iteration value. That's true for any Python iteration. It's best to use new variable names inside the iteration like a1 so you don't confuse yourself.) (你在同样的情况, a=a.ravel()a=赋新值,以a ,打破了链接到迭代值。这对任何Python的迭代如此。这是最好的使用迭代内部等新变量名a1这样您就不会混淆自己。)

Essentially the same as 基本上与

In [1054]: for a in ll:
   ......:     a.mask=mask

I can change the shape in the same in-place way 我可以用同样的方式改变形状

In [1055]: for a in ll:
   ......:     a.shape=[-1]   # short hand for inplace ravel
   ......:     a.mask=mask   

In [1056]: arr0
Out[1056]: 
masked_array(data = [1 -- -- 4],
             mask = [False  True  True False],
       fill_value = 999999)

Here's a functional way of creating new arrays with new shape and mask, and using it in a list comprehension (and no change to arr0 ) 这是一种创建具有新形状和遮罩的新数组并将其用于列表arr0 (而不更改arr0 )的功能方法

[np.ma.masked_array(a,mask=mask).ravel() for a in [arr0,arr1]]

Understanding these alternatives does require understanding how Python assigns iterative variables, and how numpy makes copies and views. 了解这些替代方案确实需要了解Python如何分配迭代变量,以及numpy如何制作副本和视图。

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

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