This code snippet demonstrates how to split a dataset into training and testing data.

'idx = np.random.rand(len(data))' generates a random array of the same length as the dataset using the 'np.random.rand()' function from the NumPy library. Each element in this array represents the probability of including the corresponding data point in the training data.

'train_data = data.iloc[idx > 0.1].reset_index(drop=True)' uses boolean indexing to select rows where the probability is greater than 0.1, assigning them to the 'train_data' variable. 'reset_index(drop=True)' resets the index of these selected rows and removes the old index.

'test_data = data.iloc[idx <= 0.1].reset_index(drop=True)' selects rows where the probability is less than or equal to 0.1, assigning them to 'test_data'. The index is reset again using 'reset_index(drop=True)'.

Essentially, this code randomly splits the data into training and testing sets, with approximately 90% of the data assigned to the training set ('train_data') and the remaining 10% assigned to the testing set ('test_data').

Splitting Datasets into Training and Testing Sets Using NumPy

原文地址: https://www.cveoy.top/t/topic/i33Z 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录