sample function r in python code example

Example 1: R sample() funciton in python

import numpy as np
np.random.choice(values, size=1000,  replace=True, p=probability)

# values is the input values that correspond to the weights
# size is the number of samples to generate
# Replace specifies if it's with or without replacement
# p is the probability of choosing each corresponding value in values

Example 2: python equivalent of R sample function

lstDemand = [0, 1000, 2000, 3000, 4000, 5000, 6000]
lstProbability = [.02, .03, .05, .08, .33, .29, .20]

# Python Equivalent of R sample() function
numpy.random.choice(lstDemand, size=1000, replace=True, p=lstProbability)

# lstDemand = the values or options
# size = how many values the output list should have (None = 1)
# replace = sampling with or without replacement
# p = the probability of choosing each corresponding value in lstDemand