我想遍历10列和170行以选择5个随机数

问题描述 投票:0回答:1

我的数组数据集的形状为(10,170)。

Name: id_matrix

array([[   1,  171,  341, ..., 1191, 1361, 1531],
       [   2,  172,  342, ..., 1192, 1362, 1532],
       [   3,  173,  343, ..., 1193, 1363, 1533],
       ...,
       [ 168,  338,  508, ..., 1358, 1528, 1698],
       [ 169,  339,  509, ..., 1359, 1529, 1699],
       [ 170,  340,  510, ..., 1360, 1530, 1700]])

我想遍历170列中的每一列,每列也包含170个数字,并随机选择五个数字。然后,我将它们作为一个组打印到屏幕上,在此概述中,当代码正常工作时,我将相应地设置格式。

Group 1: [ 92  73 139  54 147]
Group 2: [182 333 219 292 214]

我还需要设置np.random.seed(489)来保留复制和可重复性。我试图捕获这些值并陷入困境。

col=0
data=[row[col] for row in id_matrix]
print(data)

或此版本:

import pandas as pd
df[df.columns.to_series().sample(5)]

[这些方法似乎都不像我想要的...我运行了Google搜索,但似乎没有找到有关如何生成需要从这些列创建随机集的循环的线索。

请告知...

python pandas random
1个回答
0
投票

检查此:https://pynative.com/python-random-choice/您可以执行以下操作:

import random
#sampling with replacement
id_matrix = [20, 30, 40, 50 ,60, 70, 80]
sampling = random.choices(id_matrix, k=5)
print("Randomly selected multiple choices using random.choices() ", sampling)
© www.soinside.com 2019 - 2024. All rights reserved.