how to randomly sample in 2D matrix in numpy

10,978

Just use a random index (in your case 2 because you have 3 dimensions):

import numpy as np

Space_Position = np.array(Space_Position)

random_index1 = np.random.randint(0, Space_Position.shape[0])
random_index2 = np.random.randint(0, Space_Position.shape[1])

Space_Position[random_index1, random_index2]  # get the random element.

The alternative is to actually make it 2D:

Space_Position = np.array(Space_Position).reshape(-1, 2)

and then use one random index:

Space_Position = np.array(Space_Position).reshape(-1, 2)      # make it 2D
random_index = np.random.randint(0, Space_Position.shape[0])  # generate a random index
Space_Position[random_index]                                  # get the random element.

If you want N samples with replacement:

N = 5

Space_Position = np.array(Space_Position).reshape(-1, 2)                # make it 2D
random_indices = np.random.randint(0, Space_Position.shape[0], size=N)  # generate N random indices
Space_Position[random_indices]  # get N samples with replacement

or without replacement:

Space_Position = np.array(Space_Position).reshape(-1, 2)  # make it 2D
random_indices = np.arange(0, Space_Position.shape[0])    # array of all indices
np.random.shuffle(random_indices)                         # shuffle the array
Space_Position[random_indices[:N]]  # get N samples without replacement
Share:
10,978
user824624
Author by

user824624

Updated on June 12, 2022

Comments

  • user824624
    user824624 almost 2 years

    I have a 2d array/matrix like this, how would I randomly pick the value from this 2D matrix, for example getting value like [-62, 29.23]. I looked at the numpy.choice but it is built for 1d array.

    The following is my example with 4 rows and 8 columns

    Space_Position=[
          [[-62,29.23],[-49.73,29.23],[-31.82,29.23],[-14.2,29.23],[3.51,29.23],[21.21,29.23],[39.04,29.23],[57.1,29.23]],
    
          [[-62,11.28],[-49.73,11.28],[-31.82,11.28],[-14.2,11.28],[3.51,11.28],[21.21,11.28] ,[39.04,11.28],[57.1,11.8]],
    
          [[-62,-5.54],[-49.73,-5.54],[-31.82,-5.54] ,[-14.2,-5.54],[3.51,-5.54],[21.21,-5.54],[39.04,-5.54],[57.1,-5.54]],
    
          [[-62,-23.1],[-49.73,-23.1],[-31.82,-23.1],[-14.2,-23.1],[3.51,-23.1],[21.21,-23.1],[39.04,-23.1] ,[57.1,-23.1]]
          ]
    

    In the answers the following solution was given:

    random_index1 = np.random.randint(0, Space_Position.shape[0])
    random_index2 = np.random.randint(0, Space_Position.shape[1])
    Space_Position[random_index1][random_index2]
    

    this indeed works to give me one sample, how about more than one sample like what np.choice() does?

    Another way I am thinking is to tranform the matrix into a array instead of matrix like,

    Space_Position=[
          [-62,29.23],[-49.73,29.23],[-31.82,29.23],[-14.2,29.23],[3.51,29.23],[21.21,29.23],[39.04,29.23],[57.1,29.23], .....   ]
    

    and at last use np.choice(), however I could not find the ways to do the transformation, np.flatten() makes the array like

    Space_Position=[-62,29.23,-49.73,29.2, ....]
    
  • user824624
    user824624 almost 7 years
    Thanks, is there any way to sample N (N>1) if not using for loop
  • MSeifert
    MSeifert almost 7 years
    @user824624 Sample with replacement or without?
  • user824624
    user824624 almost 7 years
    without replacement
  • MSeifert
    MSeifert almost 7 years
    @user824624 I updated the answer, the last approach should be want to you need then :)
  • user824624
    user824624 almost 7 years
    ,the last approach still give me a 3 dimension array, I hope to get a randomly 2 dimension array samples like [ [-62,29.23],[-49.73,29.23],[-31.82,29.23],[-14.2,29.23],[3.5‌​1,29.23],[21.21,29.2‌​3],[39.04,29.23],[57‌​.1,29.23], ..... ]
  • MSeifert
    MSeifert almost 7 years
    @user824624 did you use the Space_Position = np.array(Space_Position).reshape(-1, 2) I mentioned?
  • user824624
    user824624 almost 7 years
    oh, yes, that is it. I missed it.