Discussion:
[Numpy-discussion] Remove a random sample from array
Florian Lindner
2016-05-16 14:08:07 UTC
Permalink
Hello,

I have an array of shape (n, 2) from which I want to extract a random sample
of 20% of rows. The choosen samples should be removed the original array and
moved to a new array of the same shape (n, 2).

What is the most clever way to do with numpy?

Thanks,
Florian
Elliot Hallmark
2016-05-16 15:01:38 UTC
Permalink
What do you mean remove them from the array? Replace with zero or NaN?
Post by Florian Lindner
Hello,
I have an array of shape (n, 2) from which I want to extract a random sample
of 20% of rows. The choosen samples should be removed the original array and
moved to a new array of the same shape (n, 2).
What is the most clever way to do with numpy?
Thanks,
Florian
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
Florian Lindner
2016-05-16 15:45:04 UTC
Permalink
Post by Elliot Hallmark
What do you mean remove them from the array? Replace with zero or NaN?
Removed like when 10 samples are taken from a (100, 2) array it becomes a (90,
2) array. Copying the array is no problem, it removing inplace is not
possible.

Best,
Florian
Post by Elliot Hallmark
Post by Florian Lindner
Hello,
I have an array of shape (n, 2) from which I want to extract a random sample
of 20% of rows. The choosen samples should be removed the original array and
moved to a new array of the same shape (n, 2).
What is the most clever way to do with numpy?
Thanks,
Florian
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
Martin Noblia
2016-05-16 16:04:41 UTC
Permalink
I think with `np.random.choice`

http://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.random.choice.html
Post by Florian Lindner
Hello,
I have an array of shape (n, 2) from which I want to extract a random sample
of 20% of rows. The choosen samples should be removed the original array and
moved to a new array of the same shape (n, 2).
What is the most clever way to do with numpy?
Thanks,
Florian
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
--
*Martin Noblia*
Elliot Hallmark
2016-05-16 16:24:55 UTC
Permalink
Use `random.shuffle(range(len(arr))` to make a list of indices. Use a
slices to get your 20/80. Convert to integer arrays and index your
original array with them. Use sorted on the 80% list if you need to
preserve the order.

-Elliot

On Mon, May 16, 2016 at 11:04 AM, Martin Noblia <
Post by Martin Noblia
I think with `np.random.choice`
http://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.random.choice.html
Hello,
I have an array of shape (n, 2) from which I want to extract a random sample
of 20% of rows. The choosen samples should be removed the original array and
moved to a new array of the same shape (n, 2).
What is the most clever way to do with numpy?
Thanks,
Florian
_______________________________________________
--
*Martin Noblia*
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
j***@gmail.com
2016-05-17 03:00:07 UTC
Permalink
Post by Elliot Hallmark
Use `random.shuffle(range(len(arr))` to make a list of indices. Use a
slices to get your 20/80. Convert to integer arrays and index your
original array with them. Use sorted on the 80% list if you need to
preserve the order.
similar but simpler

You can just random permute/shuffle an array with 20% ones (True) and 80%
zeros (False) and use it as a mask to select from the original array.

Josef
Post by Elliot Hallmark
-Elliot
On Mon, May 16, 2016 at 11:04 AM, Martin Noblia <
Post by Martin Noblia
I think with `np.random.choice`
http://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.random.choice.html
Hello,
I have an array of shape (n, 2) from which I want to extract a random sample
of 20% of rows. The choosen samples should be removed the original array and
moved to a new array of the same shape (n, 2).
What is the most clever way to do with numpy?
Thanks,
Florian
_______________________________________________
--
*Martin Noblia*
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
Loading...