Answers for "numpy random choice from list"

33

python choose random element from list

import random

#1.A single element
random.choice(list)

#2.Multiple elements with replacement
random.choices(list, k = 4)

#3.Multiple elements without replacement
random.sample(list, 4)
Posted by: Guest on May-25-2020
2

python random choice from list

import random
list = [20, 30, 40, 50 ,60, 70, 80]
sampling = random.choices(list, k=4)      # Choices with repetition
sampling = random.sample(list, k=4)       # Choices without repetition
Posted by: Guest on November-26-2020
1

python select random subset from numpy array

fruits = ['apple', 'banana', 'orange', 'grape']
subset_size = int(0.7 * len(fruits))
np.random.choice(fruits, subset_size, replace=False)
# array(['grape', 'banana'], dtype='<U6')
Posted by: Guest on March-03-2020

Code answers related to "numpy random choice from list"

Python Answers by Framework

Browse Popular Code Answers by Language