-
2020-11-20 09:47:43更多相关内容
-
在Keras中利用np.random.shuffle()打乱数据集实例
2020-12-17 07:35:32np.random.shuffle(index) print(index[0:20]) X_train=X_train[index,:,:,:]#X_train是训练集,y_train是训练标签 y_train=y_train[index] 补充知识:Keras中shuffle和validation_split的顺序 模型的fit函数有两个... -
Numpy中np.random.rand()和np.random.randn() 用法和区别详解
2020-12-16 21:20:33np.random.rand(d0,d1,d2……dn) 注:使用方法与np.random.randn()函数相同 作用: 通过本函数可以返回一个或一组服从“0~1”均匀分布的随机样本值。随机样本取值范围是[0,1),不包括1。 应用:在深度学习的Dropout... -
np.random.shuffle np.random.permutation的速度差异
2021-03-10 15:59:11我们发现np.random.permutation要比np.random.shuffle快很多 x = np.random.rand(50000, 2) # 933 µs %timeit x.take(np.random.permutation(x.shape[0]), axis=0) # 1.41 ms %timeit x[np.random.permutation(x....现象
我们发现np.random.permutation要比np.random.shuffle快很多
x = np.random.rand(50000, 2) # 933 µs %timeit x.take(np.random.permutation(x.shape[0]), axis=0) # 1.41 ms %timeit x[np.random.permutation(x.shape[0])] # 1.41 ms %timeit np.random.permutation(x) # 46.3 ms %timeit np.random.shuffle(x) 933 µs ± 2.74 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each) 1.41 ms ± 5.87 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each) 1.41 ms ± 4.22 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each) 46.3 ms ± 413 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
原因
np.random.shuffle 是原地修改数组,因此需要开辟一个buffer作为交换空间
buf = np.empty_like(x[0])
,而且需要for循环反复交换# https://github.com/numpy/numpy/blob/18f2385b29bdd62701a1a82d7bf33fd87430a05e/numpy/random/mtrand/mtrand.pyx#L4841 # Shuffling and permutations: def shuffle(self, object x): """ shuffle(x) Modify a sequence in-place by shuffling its contents. This function only shuffles the array along the first axis of a multi-dimensional array. The order of sub-arrays is changed but their contents remains the same. """ # ...... elif isinstance(x, np.ndarray) and x.ndim > 1 and x.size: # Multidimensional ndarrays require a bounce buffer. buf = np.empty_like(x[0]) with self.lock: for i in reversed(range(1, n)): j = rk_interval(i, self.internal_state) buf[...] = x[j] x[j] = x[i] x[i] = buf
np.random.permutation 是返回一个新数组,因此只用shuffle idx即可,比较快
# https://github.com/numpy/numpy/blob/18f2385b29bdd62701a1a82d7bf33fd87430a05e/numpy/random/mtrand/mtrand.pyx#L4917 def permutation(self, object x): """ permutation(x) Randomly permute a sequence, or return a permuted range. If `x` is a multi-dimensional array, it is only shuffled along its first index. """ # ...... # Shuffle index array, dtype to ensure fast path idx = np.arange(arr.shape[0], dtype=np.intp) self.shuffle(idx) return arr[idx]
-
【Numpy】中np.random.shuffle()与np.random.permutation()的用法和区别
2020-10-23 16:00:30np.random.shuffle(x) :在原数组上进行,改变自身序列,无返回值。 np.random…permutation(x) :不在原数组上进行,返回新的数组,不改变自身数组。 1. np.random.shuffle(x) 1. 对一维数组重新排序: import ...- 对给定的数组进行重新排列的方式主要有两种:
- np.random.shuffle(x) :在原数组上进行,改变自身序列,无返回值。
- np.random…permutation(x) :不在原数组上进行,返回新的数组,不改变自身数组。
1. np.random.shuffle(x)
1. 对一维数组重新排序:
import numpy as np arr = np.arange(10) print(arr) np.random.shuffle(arr) print(arr)
2. 对多维数组重新排序:
arr = np.arange(12).reshape(3,4) print(arr) np.random.shuffle(arr) print(arr) np.random.shuffle(arr) print(arr)
通过上述例子可以看出,对于一个多维的输入,只是在第一维上进行了随机排序。例如对这个3×4的矩阵来说,只是对行进行随机排序。2. np.random.permutation(x)
1. 可直接生成一个随机排列的数组:
np.random.permutation(10)
2. 对一维数组随机排序:
>>> import numpy as np >>> a=np.random.permutation([0,1,2,3,4,5,6,7,8,9]) >>> print(a)
3. 对多维数组随机排序:
arr = np.arange(9).reshape((3, 3)) print(arr) arr2 = np.random.permutation(arr) print(arr) print(arr2)
通过上述例子可以看出,对于一个多维的输入,只是在第一维上进行了随机排序。例如对这个3×3的矩阵来说,只是对行进行随机排序。3. np.random.shuffle(x) 与 np.random.permutation(x) 的区别:
- 从代码可以看出,两个函数的功能类似。np.random.shuffle(x)改变自身数组,np.random.permutation(x)不改变自身数组。
-
numpy.random.shuffle打乱顺序函数的实现
2021-01-21 18:26:36numpy.random.shuffle 在做将caffe模型和预训练的参数转化为tensorflow的模型和预训练的参数,以便微调,遇到如下函数: def gen_data(source): while True: indices = range(len(source.images)) # indices = ... -
np.random.shuffle与np.swapaxis或transpose一起时要慎用
2022-07-05 23:06:46shuffle# y = np.transpose(y,(1,0,2)) # y = np.random.shuffle(y) # y = np.transpose(y,(1,0,2))
以上这三行代码会导致
ValueError: axes don't match array
的错误,
无独有偶,如果使用swapaxis–shuffle–swapaxis也会出现类似的错误。
要避开这一问题。
而要想对某一维度随机排列,可以使用permutation+切片的方案,shuffle仅仅只对axis 0随机。参考文献
https://www.cnblogs.com/zmbreathing/p/random_shuffle.html
-
np.random.shuffle(x)与np.random.permutation(x)
2019-11-25 19:05:43np.random.shuffle(x)2. np.random.permutation(x)3. 区别 将数组打乱随机排列 两种方法: np.random.shuffle(x):在原数组上进行,改变自身序列,无返回值。 np.random.permutation(x):不在原数组上进行,返回新... -
np.random.shuffle和np.random.permutation区别
2019-02-24 10:43:03import numpy as np arr=np.arange(10) print (arr) mat=np.arange(9).reshape(3,3) ...mat_per=np.random.permutation(mat) print(mat_per) 结果为: [0 1 2 3 4 5 6 7 8 9] [[0 1 2] [3 4 5] [6 7 8]] [[... -
np.random.shuffle()
2020-10-28 13:27:22np.random.shuffle(): 洗牌,生成随机列表,打乱原有的顺序 import random import numpy as np data = np.arange(6) print(data) np.random.shuffle(data) print(data) 输出结果: [0 1 2 3 4 5] [2 3 4 5 0 1] -
np.random.shuffle与np.random.permutation的区别
2018-11-21 10:42:00shuffle与permutation的区别 函数shuffle与permutation都是对原来的数组进行重新洗牌(即随机打乱原来的元素顺序)。区别在于shuffle直接在原来的数组上进行操作,改变原来数组的顺序...import numpy as np a = n... -
numpy的np.random.shuffle
2020-02-26 12:35:28np.random,shuffle作用就是重新排序返回一个随机序列作用类似洗牌,我自己也写过一个洗牌小程序 下图是我做的一个尝试 -
【python-numpy 】中的随机打乱数据方法np.random.shuffle
2021-04-20 23:16:59描述: shuffle() 方法将序列的所有元素随机排序。 #实验可得每次shuffle后数据都被打乱,这个方法可以在机器学习训练 #的时候在每个epoch结束后将数据重新洗牌进入下一个epoch的学习 ...np.random.shuf -
【numpy】np.random.shuffle()
2020-08-26 14:49:08功能:生成随机列表 x = np.arange(10) print(x) np.random.shuffle(x) print(x) 输出: [0 1 2 3 4 5 6 7 8 9] [2 1 8 4 3 5 0 9 7 6] -
tensorflow常用函数:np.random.shuffle
2020-04-17 17:20:55代码: np.random.shuffle(x_data) x_data是一个张量,该函数用于将x——data自身打乱,注意是在原数组上进行的,不是返回一个新数组,而原张量不变。 -
【记录】np.random.shuffle () 对多维tensor, 随机打乱其中一维的顺序代码
2022-01-28 14:05:13np.random.shuffle () 的用法记录 对多维tensor, 随机打乱其中一维的顺序: import numpy as np import random X=np.random.rand(30,2,3)#创建多维数组 time_index_list=[n for n in range(len(X))]#提取X第一维度... -
numpy 中的随机打乱数据方法np.random.shuffle
2020-05-14 11:55:04import numpy as np #实验可得每次shuffle后数据都被打乱,这个方法...np.random.shuffle(num) print(num) num1 = np.arange(20) print(num1) np.random.shuffle(num1) print(num1) np.random.shuffle(num1) print(n -
np.random.permutation()函数和np.shuffle()函数的使用
2022-02-14 13:17:21Permutation:(一组事物可能的一种) 序列,排列,排列中的任一组数字或文字; 这个函数的使用来随机排列一个数组的,第一个例子如图1所示: ...permutation = list(np.random.permutation(m)) #m为样本数 s -
(Numpy)np.random.shuffle生产随机列表
2019-10-24 19:08:41最近进行建模时,需要经常自己打乱数据集进行,numpy提供了一个函数可以对已有列表进行打乱 可以很方便的根据ID自己划分数据,当然为了使得每次划分数据相同,最好自己设定numy的随机...numpy.random.seed(num) ... -
np.random.shuffle(x)的用法
2019-02-22 19:18:00此函数主要是通过改变序列的内容来修改序列的位置。此函数只沿多维数组的第一个轴移动数组。子数组的顺序已更改,但其内容保持不变。 参数 x:即将被打乱顺序的list 返回值 无 ... -
random、np.random、torch.random总结
2022-03-28 18:52:13np.random random() 生成一个[0, 1.)之间的随机浮点数 random([3, 2]) 生成形状为3×2的[0, 1.)之间的随机浮点数 rand(3, 2) 和random([3, 2])相同 randint(a ,b, size) 生成[a,b)之间的随机整数... -
python numpy之np.random的随机数函数使用介绍
2020-09-18 14:40:11主要介绍了python numpy之np.random的随机数函数使用介绍,需要的朋友可以参考下 -
DCGAN项目总结:主要是对DCGAN代码中几处比较难懂的代码进行分析之 np.random.shuffle np.arange(10) 函数...
2019-01-26 10:37:32第一: get_random_batch函数讲解 # 我解释函数的特点是通过测试函数的输入和输出 进而了解函数...# 测试np.arange() 和 np.random.shuffle函数 def get_random_batch(): data_index = np.arange(10) print("dat... -
Python numpy包 np.random.shuffle(x) 数据集 batch 预处理
2018-01-16 11:24:33numpy.random.shuffle(x) 的重要因素就是仅仅打乱第一层的数据,一个元组中首先默认行为第一纬度,列是第二个纬度,所以打乱机器学习和深度学习常用标签时,一般使用 M*(N+1) 结构(M代表数据总量,N代表每个数据... -
Keras中利用np.random.shuffle()打乱数据集
2018-01-11 21:16:24from numpy as np index=np.arange(2000) np.random.shuffle(index) print(index[0:20]) X_train=X_train[index,:,:,:]#X_train是训练集,y_train是训练标签 y_train=y_train[index]
收藏数
35,165
精华内容
14,066