python 使用 random, np 从数组中取随机子序列
2019-10-26 本文已影响0人
谢小帅
import numpy as np
import random
# randrange 从 range 定义的序列中 随机取 1个数
a = random.randrange(0, len(img_paths), step=int(len(img_paths) / samples))
print(a)
# 随机正序抽样,bin 有序 每个 bin 内随机
step = int(len(img_paths) / samples)
a = [random.randint(i * step, (i + 1) * step) for i in range(samples)]
print(a)
# 随机无序抽样,可 sort 一下变成有序
a = random.sample(range(len(img_paths)), samples)
print(a)
a = sorted(random.sample(range(len(img_paths)), samples))
print(a)
a = np.random.randint(0, len(img_paths), size=samples)
print(a)
a = np.random.permutation(len(img_paths))[:samples]
print(a)
234
[3, 30, 54, 86, 105, 143, 181, 186, 226, 252]
[82, 67, 112, 225, 260, 70, 141, 183, 38, 60]
[3, 9, 29, 61, 95, 104, 139, 148, 157, 188]
[ 60 208 141 17 142 92 122 160 132 132]
[224 77 236 184 28 88 129 235 75 216]