Randomly select a group of doubles
By : benyamin .p.m
Date : March 29 2020, 07:55 AM
this will help I'd use a Collection of Double, say an ArrayList rather than an array of double, and then call Collections.shuffle(myArrayList) on the ArrayList. Bingo, randomized. To get 10 of these values, simply call remove(0) on the list (after checking to make sure that the size is not 0). This is not too different from shuffling a deck of cards and selecting 10 random cards.
|
randomly select a fixed number of rows in each group in SQL server table
By : Murat Ipek
Date : March 29 2020, 07:55 AM
like below fixes the issue You can use ROW_NUMBER and use NEWID() to generate a random ORDER: EDIT: I replaced CHECKSUM(NEWID()) with NEWID() since I cannot prove which is faster and NEWID() is I think the most used. code :
WITH CTE AS(
SELECT *,
RN = ROW_NUMBER() OVER(PARTITION BY id ORDER BY NEWID())
FROM tbl
)
SELECT
id, value1, value2
FROM Cte
WHERE RN <= 2
INSERT INTO yourNewTable(id, value1, value2)
SELECT
id, value1, value2
FROM (
SELECT *,
RN = ROW_NUMBER() OVER(PARTITION BY id ORDER BY NEWID())
FROM tbl
)t
WHERE RN <= 2
|
Flag randomly selected N rows by group in data.table
By : toli
Date : March 29 2020, 07:55 AM
like below fixes the issue At the data.table in column C3 I want to flag N randomly selected rows by each group (C1). There are several similar questions have already been asked on SO here, here and here. But based on the answers still cannot figure out how to find a solution for my task. code :
dt[, C3 := 1:.N %in% sample(.N, min(.N, 2)), by = C1]
dt[, C3 := 1:.N %in% head(sample(.N), 2) , by = C1]
flagsz <- c(2, 1, 2, 3)
dt[, C3 := 1:.N %in% sample(.N, min(.N, flagsz[.GRP])), by = C1]
|
randomly ordering across groups (not within group) in data.table
By : wayne
Date : March 29 2020, 07:55 AM
may help you . Let's say I want to order the iris dataset (as a data.table) by Species, keeping observations grouped by species and randomly ordering across species. , Alternatively you could do: code :
e <- d[, .N, Species]
e[, g2 := runif(.N)]
d <- e[, .(Species, g2)][d, on = 'Species']
|
Randomly select a row from each group using pandas
By : user1840460
Date : March 29 2020, 07:55 AM
it helps some times I have a pandas dataframe df which appears as following: , Use groupby with apply to select a row at random per group. code :
np.random.seed(0)
df.groupby(['Month', 'Day'])['mnthShape'].apply(np.random.choice).reset_index()
Month Day mnthShape
0 1 1 1.016754
1 1 2 0.963912
2 1 3 1.099451
np.random.seed(0)
(df.groupby(['Month', 'Day'])['mnthShape']
.apply(pd.Series.sample, n=1)
.reset_index(level=[0, 1]))
Month Day mnthShape
2 1 1 0.963912
3 1 2 1.016754
6 1 3 1.016754
|