arrayspython-3.xnumpyrandomoversampling

How to oversample an array of n string elements into an array of m string elements


l would like to oversample an array of n element into an array of m elements such that m > n.

For instance let's take n=3

colors=['red','blue','green']

set m =7

What l'm looking for ?

 oversampled_colors=['green','blue','red','red','blue','green','blue']

Solution

  • np.random.choice seems to be what you are looking for

    >>> colors=['red','blue','green']
    >>> np.random.choice(colors, 7)
    array(['red', 'red', 'green', 'red', 'blue', 'red', 'green'], dtype='<U5')