How To Sort An Array And A Matrix Together Using Numpy
I have an array y and a matrix X that is CSR sparse. I need to do a random sort of y and X where each row of y corresponds to a row in X. Using NumPy, how do I do that?
Solution 1:
In order to get a random sort, you could try to use the np.random.shuffle
function.
# Create an array of indices
indices = np.arange(y.size)
# Randomize it
np.random.shuffle(indices)
You can now use indices
to randomize y
with fancy indexing y_new = y[indices]
.
You could use the same indices
to reorder your matrix, but be careful, CSR matrices don't support fancy indexing. You'll have to transform it to LIL, reorder it, then retransform it to CSR.
Post a Comment for "How To Sort An Array And A Matrix Together Using Numpy"