title: how to randomly sample in 2D matrix in numpy
body: <p>I have a 2d array/matrix like this, how would I randomly pick the value from this 2D matrix, for example getting value like <code>[-62, 29.23]</code>. I looked at the <code>numpy.choice</code> but it is built for 1d array.</p>
  
  <p>The following is my example with 4 rows and 8 columns</p>
  
  <pre><code>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]]
        ]
  </code></pre>
  
  <p>In the answers the following solution was given:</p>
  
  <pre><code>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]
  </code></pre>
  
  <p>this indeed works to give me one sample, how about more than one sample like what <code>np.choice()</code> does?</p>
  
  <p>Another way I am thinking is to tranform the matrix into a array instead of matrix like,  </p>
  
  <pre><code>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], .....   ]
  </code></pre>
  
  <p>and at last use <code>np.choice()</code>, however I could not find the ways to do the transformation, <code>np.flatten()</code> makes the array like </p>
  
  <pre><code>Space_Position=[-62,29.23,-49.73,29.2, ....]
  </code></pre>