Numpy: How to randomly split/select an matrix into n-different matrices

daydreamer picture daydreamer · Feb 1, 2012 · Viewed 9.5k times · Source
  • I have a numpy matrix with shape of (4601, 58).
  • I want to split the matrix randomly as per 60%, 20%, 20% split based on number of rows
  • This is for Machine Learning task I need
  • Is there a numpy function that randomly selects rows?

Answer

HYRY picture HYRY · Feb 1, 2012

you can use numpy.random.shuffle

import numpy as np

N = 4601
data = np.arange(N*58).reshape(-1, 58)
np.random.shuffle(data)

a = data[:int(N*0.6)]
b = data[int(N*0.6):int(N*0.8)]
c = data[int(N*0.8):]