itjcms18
itjcms18

Reputation: 4333

Selecting random arrays from multidimensional numpy array with replacement

I have a multidimensional ndarray and I'm looking to randomly select 1000 arrays WITH replacement. This seem to me to be simple, but the with replacement part I'm struggling to incorporate.

There are 3065 arrays in this ndarray.

np.shape(train_spam)
(3065L, 58L)

I tried to use np.random.shuffle() but this does not take into account the with replacement.

np.random.shuffle(train_spam)
X_train = train_spam[:1000,1:57]

My final output would have ea shape of (1000L, 58L).

I suppose I could run a loop with a ndarray with

X_train = train_spam[0:57]

and then append but I can't figure out how to append correctly, so it looks the same. Any help would be greatly appreciated

Upvotes: 2

Views: 2310

Answers (3)

jo pramod
jo pramod

Reputation: 1

You can obtain multiple arrays randomly from ndarray, Using Random Generator.

a = np.array([[1,2,3],[2,3,4], [5,6,7]])
rng = np.random.default_rng()
rng.choice(a, 2)

Upvotes: 0

Michael Laszlo
Michael Laszlo

Reputation: 12239

You can also build a list of indices with [random.randrange(n) for i in range(k)].

k = 1000                                           # Number of elements to select.
n = train_spam.shape[0]                            # Number of elements in array.
indices = [random.randrange(n) for i in range(k)]  # A plain Python list.
selected = train_spam[np.array(indices)]           # Convert indices to ndarray.

If you have a plain Python list from which you want to select elements with replacement, you can do this:

pets = ['ant', 'bear', 'cat', 'dog', 'elephant', 'flamingo', 'gorilla', 'horse']
n = len(pets)
k = 10
selected = [pets[random.randrange(n)] for i in range(k)]

Upvotes: 0

Warren Weckesser
Warren Weckesser

Reputation: 114781

You could use

selected = train_spam[np.random.randint(train_spam.shape[0], size=1000)]

Upvotes: 3

Related Questions