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

PREVIOUS NEXT
Code Example
Python :: Split a list based on a condition 
Python :: save jupyter notebook session 
Python :: how to use pyttsx3 
Python :: * pattern by python 
Python :: parse invoice python 
Python :: create QAction with icon in pyqt 
Python :: how to add array and array in python 
Python :: how to create dictionary in python 
Python :: print("hello world") 
Python :: how to know the version of python 
Python :: Python enumerate Using enumerate() 
Python :: nltk python how to tokenize text 
Python :: add columns not in place 
Python :: django MESSAGE_TAGS 
Python :: define event on socketio python 
Python :: subplots 
Python :: draw bipartite graph networkx 
Python :: how to create a subset of two columns in a dataframe 
Python :: seaborn stripplot min max 
Python :: IntegerChoices django 
Python :: how to create fastapi 
Python :: jupyter notebook not opening 
Python :: python local nosql database 
Python :: matlab .* operator in python 
Python :: how to generate python code 
Python :: map function to change type of element in python 
Python :: How to Access Items in a Set in Python 
Python :: re.sub 
Python :: Tree Traversals inorder,preorder and postorder 
Python :: python call function that need args with decorator 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =