从二维矩阵中随机选取样本,并将索引保存在python中

2024-10-04 09:17:36 发布

您现在位置:Python中文网/ 问答频道 /正文

通过保留25%的原始数据,对python进行二维采样。为了做到这一点,我使用以下方法随机.randint功能:

reduced_train_face = face_train[np.random.randint(face_train.shape[0], size=300), :]

但是,我有一个第二个矩阵,它包含了与面相关的标签,我想用同样的方法减少。如何,我可以保留约化矩阵中的索引并将其应用于train-ulls矩阵?在


Tags: 方法功能size原始数据nptrain矩阵random
2条回答

可以在应用提取之前修复种子:

import numpy as np

# Each labels correspond to the first element of each line of face_train
labels_train =  np.array(range(0,15,3))
face_train = np.array(range(15)).reshape(5,3)
np.random.seed(0)
reduced_train_face = face_train[np.random.randint(face_train.shape[0], size=3), :]
np.random.seed(0)
reduced_train_labels = labels_train[np.random.randint(labels_train.shape[0], size=3)]

print(reduced_train_face, reduced_train_labels)
# [[12, 13, 14], [ 0,  1,  2], [ 9, 10, 11]], [12,  0,  9]

同样的种子,也会以同样的方式减少。在

编辑:我建议您使用^{},以确保每个数据只选择一次而不是两次相同的数据

为什么不保留所选索引并使用它们从两个矩阵中选择数据?在

import numpy as np

# setting up matrices
np.random.seed(1234)  # make example repeatable 
                      # the seeding is optional, only for the showing the
                      # same results as below!
face_train = np.random.rand(8,3)
train_lbls= np.random.rand(8)

print('face_train:\n', face_train)
print('labels:\n', train_lbls)

# Setting the random indexes
random_idxs= np.random.randint(face_train.shape[0], size=4)
print('random_idxs:\n', random_idxs)

# Using the indexes to slice the matrixes
reduced_train_face = face_train[random_idxs, :]
reduced_labels = train_lbls[random_idxs]
print('reduced_train_face:\n', reduced_train_face)
print('reduced_labels:\n', reduced_labels)

作为输出:

^{pr2}$

相关问题 更多 >