lundi 13 novembre 2023

Replace numpy.array values with randomly selected values from other rows in the array only

I have the following array of shape (A, B) where A=4 and B=2 here: [[1 1] [2 2] [3 3] [4 4]] I want to modify the array so each row consists of randomly selected values from the other rows, with no repetition within the same row. An example result would be: [[3 2] [1 3] [2 4] [3 1]]

I tried to use np.random.shuffle and np.random.choice but I can't figure out how to exclude the row itself and how to replace values per value instead of per row. np.random.shuffle leads to: [[4 4] [2 2] [3 3] [1 1]] and np.random.choice gives me errors because my array is 2D and not 1D
I'm a beginner and I feel like this should be obvious but I've been racking my brain all day... Any help would be very much appreciated




Aucun commentaire:

Enregistrer un commentaire