简体   繁体   English

Python列表未正确附加排列值

[英]Python list not appending values of permutations not correctly

I am running a permutation test by randomly shuffling rows and columns of a matrix array and storing the resulting shuffled matrix into a python list. 我正在通过随机对矩阵数组的行和列进行混洗并将结果经过混洗的矩阵存储到python列表中来运行置换测试。 The idea is that I could use the shuffled matrix for all my other permutation tests (other programs). 我的想法是,我可以将混洗后的矩阵用于所有其他排列测试(其他程序)。 The below is the code highlighting the issue 以下是突出显示该问题的代码

#Debug the issues with permutation tests
import numpy as np
temp=[[0.11101831,0.444,0.555,0.6666],[.1,.2,.3,.4],[.10,.20,.30,.40],[.9,.8,.7,.6],[.4,.5,.6,.7]]
a=np.array(temp)
saved=[None for i in range(2)]
for i in range(2):
    np.random.shuffle(a)
    np.random.shuffle(a.T)
    saved[i]=a
    print ("-----------------------")
    print (a)
    print (saved)

The output of this is below: 输出如下:

[[ 0.3         0.2         0.1         0.4       ]
 [ 0.7         0.8         0.9         0.6       ]
 [ 0.3         0.2         0.1         0.4       ]
 [ 0.555       0.444       0.11101831  0.6666    ]
 [ 0.6         0.5         0.4         0.7       ]]
[array([[ 0.3       ,  0.2       ,  0.1       ,  0.4       ],
       [ 0.7       ,  0.8       ,  0.9       ,  0.6       ],
       [ 0.3       ,  0.2       ,  0.1       ,  0.4       ],
       [ 0.555     ,  0.444     ,  0.11101831,  0.6666    ],
       [ 0.6       ,  0.5       ,  0.4       ,  0.7       ]]), []]
[[ 0.3         0.2         0.4         0.1       ]
 [ 0.555       0.444       0.6666      0.11101831]
 [ 0.3         0.2         0.4         0.1       ]
 [ 0.6         0.5         0.7         0.4       ]
 [ 0.7         0.8         0.6         0.9       ]]
[array([[ 0.3       ,  0.2       ,  0.4       ,  0.1       ],
       [ 0.555     ,  0.444     ,  0.6666    ,  0.11101831],
       [ 0.3       ,  0.2       ,  0.4       ,  0.1       ],
       [ 0.6       ,  0.5       ,  0.7       ,  0.4       ],
       [ 0.7       ,  0.8       ,  0.6       ,  0.9       ]]), array([[ 0.3       ,  0.2       ,  0.4       ,  0.1       ],
       [ 0.555     ,  0.444     ,  0.6666    ,  0.11101831],
       [ 0.3       ,  0.2       ,  0.4       ,  0.1       ],
       [ 0.6       ,  0.5       ,  0.7       ,  0.4       ],
       [ 0.7       ,  0.8       ,  0.6       ,  0.9       ]])]

As you can see that for i=0, the value of shuffle matrix a and first index of the list (saved) are the same. 如您所见,对于i = 0,混洗矩阵a的值和列表的第一个索引(保存的)相同。 But when i=1, both saved[0] and saved[1] becomes the same. 但是,当i = 1时,保存的[0]和保存的[1]都相同。 This should not happen since I am using the index of the list to assign the shuffled matrix. 因为我正在使用列表的索引来分配经过改组的矩阵,所以这应该不会发生。 Is there anything here that I am missing? 这里有我想念的东西吗?

You need to copy the array, not just a reference. 您需要复制数组,而不仅仅是引用。

In your code above, saved[i]=a refer to a same array a for all i . 在上面的代码中, saved[i]=a a为所有i引用相同的数组a

Change it to saved[i]=np.copy(a) . 将其更改为saved[i]=np.copy(a)

np.random.shuffle is an in-place operation, so you need to either use copies, as explained by @liliscent ; np.random.shuffle是一个就地操作,因此您需要使用副本, 如@liliscent所述 or, my preference, just use a function which does not operate in place. 或者,我偏爱使用一个无法正常运行的函数。

np.random.permutation is sufficient for your task: np.random.permutation足以完成您的任务:

import numpy as np

a = np.array([[0.11101831,0.444,0.555,0.6666],
             [.1,.2,.3,.4],
             [.10,.20,.30,.40],
             [.9,.8,.7,.6],
             [.4,.5,.6,.7]])

saved = []

for i in range(2):
    x = np.random.permutation(np.random.permutation(a).T)
    saved.append(x)

Even better, you can convert this into a list comprehension: 更好的是,您可以将其转换为列表理解:

saved = [np.random.permutation(np.random.permutation(a).T) for _ in range(2)]

Example result: 结果示例:

# [array([[ 0.8       ,  0.444     ,  0.5       ,  0.2       ,  0.2       ],
#        [ 0.9       ,  0.11101831,  0.4       ,  0.1       ,  0.1       ],
#        [ 0.7       ,  0.555     ,  0.6       ,  0.3       ,  0.3       ],
#        [ 0.6       ,  0.6666    ,  0.7       ,  0.4       ,  0.4       ]]),
#  array([[ 0.4       ,  0.11101831,  0.1       ,  0.9       ,  0.1       ],
#        [ 0.6       ,  0.555     ,  0.3       ,  0.7       ,  0.3       ],
#        [ 0.7       ,  0.6666    ,  0.4       ,  0.6       ,  0.4       ],
#        [ 0.5       ,  0.444     ,  0.2       ,  0.8       ,  0.2       ]])]

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

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