Search code examples
pythonrandomnumpyscipyscikits

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


  • 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?

Solution

  • 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):]