美文网首页
python 使用 random, np 从数组中取随机子序列

python 使用 random, np 从数组中取随机子序列

作者: 谢小帅 | 来源:发表于2019-10-26 09:52 被阅读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]

相关文章

  • python 使用 random, np 从数组中取随机子序列

  • python生成随机数组

    从已有数组中提取随机数组 要求:从两个不同数组中随机抽取数组,用到函数np.random.choice impor...

  • random

    random 取随机数 取一个随机浮点数。 在1-3取一个随机数数(整型)。 在序列中随机取一个元素。 在序列中随...

  • NumPy记录

    1.对数据进行shuffle 直接shuffle数组:1⃣️np.random.permutation 2⃣️np...

  • NumPy的随机数函数字库

    np.random的随机数函数(1) 通过设定和重复使用随机树种seed,可以得到相同的随机数数组 np.rand...

  • python基础知识点笔记

    1.random random库是python的标准库,random是使用梅森旋转算法来生成随机序列元素。其基本流...

  • python基础 numpy库

    ndarray数组的创建方法: 1.从python中的列表、元组等类型创建ndarray数组:x=np.array...

  • numpy常用函数

    1 np.newaxis 为 numpy.ndarray(多维数组)增加一个轴 2 np.random.randn...

  • np.random的随机数函数

    np.random的随机数函数(1) np.random.rand np.random.randn np.rand...

  • numpy基本使用

    numpy是python中科学计算不可或缺的库 np.ceil():对数组元素向上取整 np.modf:返回浮点数...

网友评论

      本文标题:python 使用 random, np 从数组中取随机子序列

      本文链接:https://www.haomeiwen.com/subject/nxsyvctx.html