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 select li element in selenium python 
Python :: print a string with spaces between characters python 
Python :: check number of elements in list python 
Python :: how split string in python by size 
Python :: flask flash 
Python :: scikit learn roc curve 
Python :: randint() 
Python :: drop na pandas 
Python :: double variable for loop python 
Python :: increase recursion depth google colab 
Python :: reversed function python 
Python :: subprocess.popen no output 
Python :: if condition dataframe python 
Python :: standardscaler 
Python :: python get 2d array output as matrix 
Python :: concatenate string and int python 
Python :: os.getcwd() python 3 
Python :: pathlib path forward or back slahses 
Python :: multiple arguments with multiprocessing python 
Python :: decode vnc hash 
Python :: how to detect the reaction to a message discord.py 
Python :: find word position in string python 
Python :: django create superuser from script 
Python :: regex_2/_regex.c:50:10: fatal error: Python.h: No such file or directory 
Python :: hash with python 
Python :: Python round to only two decimal 
Python :: how to change values of dictionary in python 
Python :: django pagination class based views 
Python :: python elapsed time in milliseconds 
Python :: test with python 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =