Home > database >  How to use np.random.choice on arrays?
How to use np.random.choice on arrays?

Time:04-26

How to use np.random.choice on array ? I would like the random selection to be of all the array Together, and not from each array individually.

Thank you

import pandas as pd
import numpy as np
df = pd.read_csv('test.csv', usecols=['a','b','c','d'])
df1 = np.array(df.iloc[0:6])
df1
# print(np.random.choice(df1,(5, 4))) # ???

array([['7', '9', '9', '9'],
       ['K', '10', '8', 'A'],
       ['K', '10', '10', 'Q'],
       ['7', 'Q', 'A', '10'],
       ['Q', '10', 'A', 'K'],
       ['9', '9', '7', 'Q']], dtype=object)

CodePudding user response:

Just flatten the array before using np.random.choice:

n = 10 # target size
np.random.choice(array.flatten(), size=n)

If you'd like to shuffle the array, use np.random.shuffle instead.

  • Related