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

前端 未结 4 785
攒了一身酷
攒了一身酷 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:20

    Since you need it for machine learning, here is a method I wrote:

    import numpy as np
    
    def split_random(matrix, percent_train=70, percent_test=15):
        """
        Splits matrix data into randomly ordered sets 
        grouped by provided percentages.
    
        Usage:
        rows = 100
        columns = 2
        matrix = np.random.rand(rows, columns)
        training, testing, validation = \
        split_random(matrix, percent_train=80, percent_test=10)
    
        percent_validation 10
        training (80, 2)
        testing (10, 2)
        validation (10, 2)
    
        Returns:
        - training_data: percentage_train e.g. 70%
        - testing_data: percent_test e.g. 15%
        - validation_data: reminder from 100% e.g. 15%
        Created by Uki D. Lucas on Feb. 4, 2017
        """
    
        percent_validation = 100 - percent_train - percent_test
    
        if percent_validation < 0:
            print("Make sure that the provided sum of " + \
            "training and testing percentages is equal, " + \
            "or less than 100%.")
            percent_validation = 0
        else:
            print("percent_validation", percent_validation)
    
        #print(matrix)  
        rows = matrix.shape[0]
        np.random.shuffle(matrix)
    
        end_training = int(rows*percent_train/100)    
        end_testing = end_training + int((rows * percent_test/100))
    
        training = matrix[:end_training]
        testing = matrix[end_training:end_testing]
        validation = matrix[end_testing:]
        return training, testing, validation
    
    # TEST:
    rows = 100
    columns = 2
    matrix = np.random.rand(rows, columns)
    training, testing, validation = split_random(matrix, percent_train=80, percent_test=10) 
    
    print("training",training.shape)
    print("testing",testing.shape)
    print("validation",validation.shape)
    
    print(split_random.__doc__)
    
    • training (80, 2)
    • testing (10, 2)
    • validation (10, 2)

提交回复
热议问题