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

前端 未结 4 786
攒了一身酷
攒了一身酷 2021-02-05 16:00
  • 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 Machin
4条回答
  •  花落未央
    2021-02-05 16:26

    A complement to HYRY's answer if you want to shuffle consistently several arrays x, y, z with same first dimension: x.shape[0] == y.shape[0] == z.shape[0] == n_samples.

    You can do:

    rng = np.random.RandomState(42)  # reproducible results with a fixed seed
    indices = np.arange(n_samples)
    rng.shuffle(indices)
    x_shuffled = x[indices]
    y_shuffled = y[indices]
    z_shuffled = z[indices]
    

    And then proceed with the split of each shuffled array as in HYRY's answer.

提交回复
热议问题