random-sample

Randomly sample a percentage of rows within a data frame

烈酒焚心 提交于 2019-12-03 15:41:47
问题 Related to this question. gender <- c("F", "M", "M", "F", "F", "M", "F", "F") age <- c(23, 25, 27, 29, 31, 33, 35, 37) mydf <- data.frame(gender, age) mydf[ sample( which(mydf$gender=='F'), 3 ), ] Instead of selecting a number of rows (3 in above case), how can I randomly select 20% of rows with "F"? So of the five rows with "F", how do I randomly sample 20% of those rows. 回答1: How about this: mydf[ sample( which(mydf$gender=='F'), round(0.2*length(which(mydf$gender=='F')))), ] Where 0.2 is

Difference between runif and sample in R?

ぃ、小莉子 提交于 2019-12-03 15:24:55
In terms of probability distribution they use? I know that runif gives fractional numbers and sample gives whole numbers, but what I am interested in is if sample also use the 'uniform probability distribution'? Consider the following code and output: > set.seed(1) > round(runif(10,1,100)) [1] 27 38 58 91 21 90 95 66 63 7 > set.seed(1) > sample(1:100, 10, replace=TRUE) [1] 27 38 58 91 21 90 95 67 63 7 This strongly suggests that when asked to do the same thing, the 2 functions give pretty much the same output (though interestingly it is round that gives the same output rather than floor or

Generate string for Regex pattern in Ruby

痴心易碎 提交于 2019-12-03 11:33:53
In Python language I find rstr that can generate a string for a regex pattern. Or in Python we have this method that can return range of string: re.sre_parse.parse(pattern) #..... ('range', (97, 122)) .... But In Ruby I didn't find any thing. So how to generate string for a regex pattern in Ruby(reverse regex)? I wanna to some thing like this: "/[a-z0-9]+/".example #tvvd "/[a-z0-9]+/".example #yt "/[a-z0-9]+/".example #bgdf6 "/[a-z0-9]+/".example #564fb "/[a-z0-9]+/" is my input. The outputs must be correct string that available in my regex pattern. Here outputs were: tvvd , yt , bgdf6 , 564fb

How to do weighted random sample of categories in python

房东的猫 提交于 2019-12-03 04:14:18
问题 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 the time. What's the canonical way of doing this in python? I've looked at the random module which doesn't seem to have an appropriate function and at numpy.random which although it has a multinomial function doesn't seem to return the results in a nice form for this

How to do weighted random sample of categories in python

孤者浪人 提交于 2019-12-02 17:32:08
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 the time. What's the canonical way of doing this in python? I've looked at the random module which doesn't seem to have an appropriate function and at numpy.random which although it has a multinomial function doesn't seem to return the results in a nice form for this problem. I'm basically looking for something like mnrnd in matlab. Many thanks. Thanks for all the

Generate Unique Random Matlab Numbers with a range

有些话、适合烂在心里 提交于 2019-12-02 16:51:52
问题 Say I want 5 numbers between 1 to 10. However, I do not want any number to be repeated. How do I do this? I thought of doing randi([1,length(a)]) Or this : (10-1).*rand(5,1) + 1 But then, this only gives me one number at a time! I want unique numbers and this will nto guarantee it. 回答1: One way to do it is by using randperm : N = 10; % Numbers from 1 to N will be permuted n = 5; % Numbers to be extracted x = randperm(N); % Permute numbers between 1 and N x = x(1:n); % Retain first n This can

Randomly associate elements of two vectors given conditions

徘徊边缘 提交于 2019-12-02 06:24:28
I have a data.table of capitals capitals<-data.table(capital=c(100,50,25,5)) capitals capital 1: 100 2: 50 3: 25 4: 5 and a data.table of losses losses<-data.table(loss=c(45,10,5,1)) losses loss 1: 45 2: 10 3: 5 4: 1 I would like to randomly associate each capital with a loss (without replacement) such that the loss is less than or equal to the capital. In pseudo code one possible implementation would be Set all capitalLoss to NA (i.e. capitals[, capitalLoss:=NA]) Order losses from largest to smallest For each loss in losses randomly pick from capitals where capital>=loss and is.na(capitalLoss

Returning items randomly from a collection

我的梦境 提交于 2019-12-01 22:55:37
问题 I've a method which returns a generic list collection(List) from the database. This collection has got order details i.e., Order Id, order name, product details etc. Also, method the method returns a collection having only the top 5 orders sorted by order date descending. My requirement is that each time the client calls this method, I need to return collection which has got 5 random orders. How do I achieve this using C#? 回答1: I wrote a TakeRandom extension method a while back which does

Returning items randomly from a collection

旧城冷巷雨未停 提交于 2019-12-01 21:59:25
I've a method which returns a generic list collection(List) from the database. This collection has got order details i.e., Order Id, order name, product details etc. Also, method the method returns a collection having only the top 5 orders sorted by order date descending. My requirement is that each time the client calls this method, I need to return collection which has got 5 random orders. How do I achieve this using C#? I wrote a TakeRandom extension method a while back which does this using a Fisher-Yates shuffle . It's pretty efficient as it only bothers to randomise the number of items

How to return sample row from database one by one

别等时光非礼了梦想. 提交于 2019-12-01 14:03:17
Web page should show one product image for specific product category from PostgreSql database. This image should changed automatically to other image after every 25 seconds. Returned product may be random or in some sequence. Some product may be missing and some repeated but most of the products in criteria should returned. Total available image count may change slightly between sample retrieval Currently code below is used which is executed after every 25 seconds. This requires two queries to database: one for count which may be slwo and second for single image retrieval. In both cases where