Skip to content

Instantly share code, notes, and snippets.

@ycanerol
Created April 16, 2019 07:54
Show Gist options
  • Save ycanerol/2f13271d7ef243a67574e4cce7f0f0ac to your computer and use it in GitHub Desktop.
Save ycanerol/2f13271d7ef243a67574e4cce7f0f0ac to your computer and use it in GitHub Desktop.
Shuffle 2D numpy array within each row
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