mardi 18 décembre 2018

Strange numpy random shuffle and seed

I have a question about random of numpy, especially shuffle and seed.

'seed' is used for generating a same random sequence.

'shuffle' is used for shuffling something.

To shuffle two lists in the same order, this code works :

idx = [1, 2, 3, 4, 5, 6]  
idx2 = [1, 2, 3, 4, 5, 6]  

seed = np.random.randint(0, 100000)  

np.random.seed(seed)  
np.random.shuffle(idx)  
np.random.seed(seed)  
np.random.shuffle(idx2)  

results :

[1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6]  
[5, 3, 1, 2, 4, 6] [5, 3, 1, 2, 4, 6]  
[1, 5, 3, 2, 4, 6] [1, 5, 3, 2, 4, 6]  
[2, 5, 3, 4, 6, 1] [2, 5, 3, 4, 6, 1]  
[2, 5, 6, 3, 4, 1] [2, 5, 6, 3, 4, 1]  
[4, 5, 6, 1, 2, 3] [4, 5, 6, 1, 2, 3]  

I can check that this code works well.

And also, it works for two lists containing different elements.

idx2 = ['./r/x/a1.png', './r/x/a2.png', './r/x/a3.png', './r/x/a4.png', './r/x/a5.png', './r/x/ac.png']  

[1, 2, 3, 4, 5, 6] ['./r/x/a1.png', './r/x/a2.png', './r/x/a3.png', './r/x/a4.png', './r/x/a5.png', './r/x/ac.png']
[1, 4, 3, 5, 6, 2] ['./r/x/a1.png', './r/x/a4.png', './r/x/a3.png', './r/x/a5.png', './r/x/ac.png', './r/x/a2.png']
[6, 3, 4, 5, 2, 1] ['./r/x/ac.png', './r/x/a3.png', './r/x/a4.png', './r/x/a5.png', './r/x/a2.png', './r/x/a1.png']
[6, 3, 1, 4, 5, 2] ['./r/x/ac.png', './r/x/a3.png', './r/x/a1.png', './r/x/a4.png', './r/x/a5.png', './r/x/a2.png']
[4, 3, 6, 2, 5, 1] ['./r/x/a4.png', './r/x/a3.png', './r/x/ac.png', './r/x/a2.png', './r/x/a5.png', './r/x/a1.png']
[5, 2, 1, 4, 6, 3] ['./r/x/a5.png', './r/x/a2.png', './r/x/a1.png', './r/x/a4.png', './r/x/ac.png', './r/x/a3.png']

Everything is good.

BUT I found an error :

If idx2 is changed to :

['./results/x2/0007.png_team_1.png', './results/x2/0007.png_team_2.png', './results/x2/0007.png_team_3.png', './results/x2/0007.png_team_4.png', './results/x2/0007.png_team_5.png', './results/x2/0007.png_team_comp.png']

Then,

[1, 2, 3, 4, 5, 6] ['./results/x2/0007.png_team_1.png', './results/x2/0007.png_team_2.png', './results/x2/0007.png_team_3.png', './results/x2/0007.png_team_4.png', './results/x2/0007.png_team_5.png', './results/x2/0007.png_team_comp.png']
[1, 5, 2, 3, 4, 6] ['./results/x2/0007.png_team_1.png', './results/x2/0007.png_team_5.png', './results/x2/0007.png_team_2.png', './results/x2/0007.png_team_3.png', './results/x2/0007.png_team_4.png', './results/x2/0007.png_team_comp.png']
[5, 1, 6, 2, 3, 4] ['./results/x2/0055.png_team_2.png', './results/x2/0055.png_team_1.png', './results/x2/0055.png_team_comp.png', './results/x2/0055.png_team_3.png', './results/x2/0055.png_team_4.png', './results/x2/0055.png_team_5.png']
[3, 4, 5, 1, 6, 2] ['./results/x2/0121.png_team_5.png', './results/x2/0121.png_team_comp.png', './results/x2/0121.png_team_1.png', './results/x2/0121.png_team_2.png', './results/x2/0121.png_team_3.png', './results/x2/0121.png_team_4.png']
[1, 3, 6, 2, 5, 4] ['./results/x2/0129.png_team_4.png', './results/x2/0129.png_team_1.png', './results/x2/0129.png_team_5.png', './results/x2/0129.png_team_comp.png', './results/x2/0129.png_team_3.png', './results/x2/0129.png_team_2.png']
[5, 6, 4, 1, 3, 2] ['./results/x2/0147.png_team_5.png', './results/x2/0147.png_team_3.png', './results/x2/0147.png_team_comp.png', './results/x2/0147.png_team_1.png', './results/x2/0147.png_team_2.png', './results/x2/0147.png_team_4.png']

It works well "ONLY FIRST SHUFFLE".

Why does this awkward behavior happen?

I cannot understand this strange behavior of np.random.shuffle and seed.




Aucun commentaire:

Enregistrer un commentaire