14
  • 我有一个形状为 (4601, 58) 的 numpy 矩阵。
  • 我想根据行数将矩阵随机拆分为 60%、20%、20%
  • 这是我需要的机器学习任务
  • 是否有一个随机选择行的 numpy 函数?
4

4 回答 4

20

你可以使用 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):]
于 2012-02-01T02:21:43.007 回答
8

如果您想一致地混洗多个具有相同第一维的数组 x、y、z,则对 HYRY 的回答进行补充:x.shape[0] == y.shape[0] == z.shape[0] == n_samples

你可以做:

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]

然后按照 HYRY 的回答继续拆分每个洗牌的数组。

于 2012-02-01T08:18:21.123 回答
4

如果你想随机选择行,你可以random.sample从标准 Python 库中使用:

import random

population = range(4601) # Your number of rows
choice = random.sample(population, k) # k being the number of samples you require

random.sample无需替换的样本,因此您不必担心重复的行最终会出现在choice. 给定一个名为 的 numpy 数组matrix,您可以通过切片来选择行,如下所示matrix[choice]

当然,k可以等于总体中的总元素数,然后choice将包含行索引的随机排序。然后,您可以根据需要进行分区choice,如果这就是您所需要的。

于 2012-02-01T00:49:00.630 回答
2

既然机器学习需要它,这是我写的一个方法:

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__)
  • 培训 (80, 2)
  • 测试 (10, 2)
  • 验证 (10, 2)
于 2017-02-04T19:57:48.397 回答