11

I have a numpy array as

[[  5.80084178e-05   1.20779787e-02  -2.65970238e-02]
 [ -1.36810406e-02   6.85722519e-02  -2.60280724e-01]
 [  4.21996519e-01  -1.43644036e-01   2.12904690e-01]
 [  3.03098198e-02   1.50170659e-02  -1.09683402e-01]
 [ -1.50776089e-03   7.22369575e-03  -3.71181228e-02]
 [ -3.04448275e-01  -3.66987035e-01   1.44618682e-01]
 [ -1.46744916e-01   3.47112167e-01   3.09550267e-01]
 [  1.16567762e-03   1.72858807e-02  -9.39297514e-02]
 [  1.25896836e-04   1.61310167e-02  -6.00253128e-02]
 [  1.65062798e-02   1.96933143e-02  -4.26540031e-02]
 [ -3.78020965e-03   7.51770012e-03  -3.67852984e-02]]

And I want to select any two random rows from these so the output will be-

[[ -1.36810406e-02   6.85722519e-02  -2.60280724e-01]
[  1.16567762e-03   1.72858807e-02  -9.39297514e-02]]
Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
DummyGuy
  • 425
  • 1
  • 8
  • 20

3 Answers3

10

I believe you are simply looking for:

#Create a random array
>>> a = np.random.random((5,3))
>>> a
array([[ 0.26070423,  0.85704248,  0.82956827],
       [ 0.26840489,  0.75970263,  0.88660498],
       [ 0.5572771 ,  0.29934986,  0.04507683],
       [ 0.78377012,  0.66445244,  0.08831775],
       [ 0.75533819,  0.05128844,  0.49477196]])

#Select random rows based on the rows in your array
#The 2 value can be changed to select any number of rows
>>> b = np.random.randint(0,a.shape[0],2)
>>> b
array([1, 2])

#Slice array a
>>> a[b]
array([[ 0.26840489,  0.75970263,  0.88660498],
       [ 0.5572771 ,  0.29934986,  0.04507683]])

Or simply:

a[np.random.randint(0,a.shape[0],2)]
Daniel
  • 19,179
  • 7
  • 60
  • 74
2

Do you mean 'array'? That looks like a list to me.

try:

import random
out_list = random.sample(your_numpy_list,2)

This will give you a list containing two distinct members of your list (see documentation for random)

FarmerGedden
  • 1,162
  • 10
  • 23
  • Its giving me `TypeError: object of type 'int' has no len()` – DummyGuy Apr 03 '14 at 11:47
  • Hmm. Could you post the entire error, and the code you ran to get it? If you go [here](http://pythonfiddle.com/) and paste in the code below you'll be able to see it working: `import random` `numpy_list = [[6,5,4],[8,7,6],[4,3,2]]` `out_list = random.sample(numpy_list,2)` `print out_list` – FarmerGedden Apr 03 '14 at 11:55
  • `Traceback (most recent call last): File "C:\Python27\PCA.py", line 113, in out_list = random.sample(pcar,2) File "C:\Python27\lib\random.py", line 318, in sample n = len(population) TypeError: object of type 'int' has no len()` – DummyGuy Apr 03 '14 at 12:03
  • What is `pcar` ? It should be the array you've printed at the top of your post. – FarmerGedden Apr 03 '14 at 12:14
  • Yes it's array printed above. – DummyGuy Apr 03 '14 at 14:24
  • Its giving me output as [array([ 0.00116568, 0.01728588, -0.09392975]), array([ 0.01650628, 0.01969331, -0.042654 ])] which is different from actual result. – DummyGuy Apr 03 '14 at 14:30
1

Say your array is:

import numpy as np
a=np.random.random((11,3))

Get two random rows like this:

a[randint(0,11,size=2)]

To get two random columns:

[a[:,x] for x in randint(0,3,size=2)]

You may also want to have a look at random

Lee
  • 29,398
  • 28
  • 117
  • 170
  • I have tried `randint` but I was unable to understand where to put the list name. My list name is `pcar`. Can u tell me how will it work? – DummyGuy Apr 03 '14 at 12:07
  • @atomh33ls This selects columns and creates a list of numpy arrays. I believe you want `a[:,x]` if you would like to select columns from x. – Daniel Apr 03 '14 at 12:41
  • @Ophion correct, thanks - I have switched it from column wise to row wise to better match the question. – Lee Apr 03 '14 at 12:47
  • @DummyGuy with [randint](http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.randint.html#numpy.random.randint) you define the range from which an integer will be randomly chosen. The `size` parameter defines the shape of the output array of random integers (in your case you just want 2). – Lee Apr 03 '14 at 12:51