Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

numpy random choice

from numpy import random

# weight random choices
# 10% chance of 2
# 30% chance of 4
# 60% chance of 6
# 0% chance of 8
# return array of 5 items of random choice from a list
x = random.choice([2, 4, 6, 8], p=[0.1, 0.3, 0.6, 0.0], size=(5))
print(x)
# output [4 6 4 6 4]

# 10% chance of 3
# 30% chance of 5
# 60% chance of 7
# return 2d array 3 * 5 of random chocies
x = random.choice([3, 5, 7, 9], p=[0.1, 0.3, 0.6, 0.0], size=(3, 5))
print(x)
# output
# [[7 7 7 7 7]
#  [7 3 3 5 3]
#  [7 7 7 7 7]]
Comment

np.random.choice

aa_milne_arr = ['pooh', 'rabbit', 'piglet', 'Christopher']
>>> np.random.choice(aa_milne_arr, 5, p=[0.5, 0.1, 0.1, 0.3])
array(['pooh', 'pooh', 'pooh', 'Christopher', 'piglet'], # random
      dtype='<U11')
Comment

np.random.choice

np.random.choice(5, 3)
array([0, 3, 4]) # random
>>> #This is equivalent to np.random.randint(0,5,3)
Comment

numpy generator choice

numpy.random.Generator.choice
method

random.Generator.choice(a, size=None, replace=True, p=None, axis=0, shuffle=True)
Generates a random sample from a given array
Comment

PREVIOUS NEXT
Code Example
Python :: how to download packages using pip 
Python :: install python3.6 in linux 
Python :: how to iterate over a list in python 
Python :: how to swap two variables without using third variable python 
Python :: file uploads django 
Python :: seaborn barplot remove error bars 
Python :: inplace pandas 
Python :: overriding update in serializer django 
Python :: python add comma each 3 digits format 
Python :: python opencv measure distance two shapes 
Python :: print input in python 
Python :: openpyxl read sheet row by row 
Python :: fibonacci number 
Python :: python binary search 
Python :: How to Adjust Title Position in Matplotlib 
Python :: fastapi upload file save 
Python :: list in list python 
Python :: python how to make a movement controler 
Python :: even numbers in python 
Python :: I have string index in pandas DataFrame how can I select by startswith? 
Python :: importing python module from different directory 
Python :: get key(s) for min value in dict python 
Python :: deleting a file using python 
Python :: access myultiple dict values with list pythojn 
Python :: sha256 python 
Python :: python find string in list 
Python :: pip install opencv 
Python :: python autoclicker 
Python :: get file parent directory python 
Python :: python how to draw a circle 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =