1

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']
eric lardon
  • 351
  • 1
  • 6
  • 21
  • You should provide any example of you trying this. Anyways, if you just want random oversampling in `oversampled` use `for` loop and set the `range` to your `m` value, then use `randint` from `random` module to randomly pick values from `colors`. – Sqoshu Feb 28 '18 at 12:59
  • How it works for strings (in my case) . randint for int values – eric lardon Feb 28 '18 at 13:01
  • Your values are stings, but their indexes are ints and those are the things you use. – Sqoshu Feb 28 '18 at 13:02
  • Ah yes l do understand thanks – eric lardon Feb 28 '18 at 13:04

3 Answers3

2

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')
Paul Panzer
  • 51,835
  • 3
  • 54
  • 99
0
import random
def fun(colors,n,m):
  colors1=[]
  while(len(colors1)<n):
      colors1.append(colors[random.randint(0,m-1)])
  return colors1
colors=['red','blue','green']
oversampled_colors=fun(colors,7,len(colors))
print(oversampled_colors)
Jay Shankar Gupta
  • 5,918
  • 1
  • 10
  • 27
0

To sample WITH replacement (values can be repeated):

>>> random.choices(['red','blue','green'], k=5)
['green', 'green', 'red', 'blue', 'red']

Also, to sample WITHOUT replacement (values cannot be repeated):

>>> random.sample(['red','blue','green'], k=3)
['blue', 'green', 'red']
Salva Carrión
  • 510
  • 6
  • 16