random-sample

Random Sample of a subset of a dataframe in Pandas

Random Sample of a subset of a dataframe in Pandas Question: I have a pandas DataFrame with 100,000 rows and want to split it into 100 sections with 1000 rows in each of them. How do I draw a random sample of certain size (e.g. 50 rows) of just one of the 100 sections? The …

Total answers: 5

Binary random array with a specific proportion of ones?

Binary random array with a specific proportion of ones? Question: What is the efficient(probably vectorized with Matlab terminology) way to generate random number of zeros and ones with a specific proportion? Specially with Numpy? As my case is special for 1/3, my code is: import numpy as np a=np.mod(np.multiply(np.random.randomintegers(0,2,size)),3) But is there any built-in function …

Total answers: 6

How to do weighted random sample of categories in python

How to do weighted random sample of categories in python Question: Given a list of tuples where each tuple consists of a probability and an item I’d like to sample an item according to its probability. For example, give the list [ (.3, ‘a’), (.4, ‘b’), (.3, ‘c’)] I’d like to sample ‘b’ 40% of …

Total answers: 9

Weighted random selection with and without replacement

Weighted random selection with and without replacement Question: Recently I needed to do weighted random selection of elements from a list, both with and without replacement. While there are well known and good algorithms for unweighted selection, and some for weighted selection without replacement (such as modifications of the resevoir algorithm), I couldn’t find any …

Total answers: 9