Created
April 16, 2019 07:54
-
-
Save ycanerol/2f13271d7ef243a67574e4cce7f0f0ac to your computer and use it in GitHub Desktop.
Shuffle 2D numpy array within each row
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def shufflebyrow(X): | |
""" | |
Shuffle each row of a given array independently. Can be useful for | |
randomizing binned spikes from all cells, for statistical testing. | |
Parameters | |
-------- | |
X: np.array with two dimensions | |
Rows should correspond to cells, columns to time bins. | |
Example | |
------ | |
>>> allspikes.shape | |
(37, 10000) | |
>>> shuffledspikes = shufflebyrow(allspikes) | |
>>> np.allclose(allspikes.sum(axis=1), | |
shuffledspikes.sum(axis=1)) | |
True | |
Notes | |
----- | |
Adopted from https://github.com/numpy/numpy/issues/5173#issuecomment-467625388 | |
""" | |
return np.apply_along_axis(np.random.permutation, axis=1, arr=X) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment