python equivalent of r sample function

Solutions on MaxInterview for python equivalent of r sample function by the best coders in the world

showing results for - "python equivalent of r sample function"
Meryem
22 Jun 2016
1lstDemand = [0, 1000, 2000, 3000, 4000, 5000, 6000]
2lstProbability = [.02, .03, .05, .08, .33, .29, .20]
3
4# Python Equivalent of R sample() function
5numpy.random.choice(lstDemand, size=1000, replace=True, p=lstProbability)
6
7# lstDemand = the values or options
8# size = how many values the output list should have (None = 1)
9# replace = sampling with or without replacement
10# p = the probability of choosing each corresponding value in lstDemand