-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
function to choose element at random from an array #3075
Comments
Using |
Sampling with replacement already exists in the Stats package as the |
Yes I think a good way to do this is just |
I think you mean a[1:rand(1:end)] |
I've seen a handful of @JeffBezanson coding slip ups. This is not one of them ;-) |
|
Since this issue is very old, I just wanted to updated @johnmyleswhite's comment for 2014: sampling is in |
How does StatsBase.sample work with dataframe? I know I can convert it to On Fri, Sep 12, 2014 at 3:03 PM, Iain Dunning notifications@github.com
|
Just ask for a subset of rows generated by sampling from 1:size(df, 1). -- John On Sep 12, 2014, at 1:12 PM, arshak notifications@github.com wrote:
|
@SamChill This does not work for non-indexable collections, like a Set. What's an efficient way to get a random element out of a Set? |
An efficient implementation for |
choose(xs, n) = xs[randperm(end)][1:n] |
@undwad That seems inefficient, particularly when |
Please don't necropost on old, resolved issues. |
I think a useful utility function is to chose a random element from an array. Python has this in random.choice. Here is what a Julia implementation might look like:
An option would be to have an additional argument for drawing some number of samples. This would be sampling with replacement from an array with uniform probability.
Another useful function could be to have sampling without replacement.
The text was updated successfully, but these errors were encountered: