Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Numpy:随机分割数组_Python_Arrays_Numpy - Fatal编程技术网

Python Numpy:随机分割数组

Python Numpy:随机分割数组,python,arrays,numpy,Python,Arrays,Numpy,我有一个numpy形状阵列,其中包含许多(本例中为200个)单色64x64像素图像,因此具有以下形状: >>> a.shape (200L, 1L, 64L, 64L) 我想将这些图像拆分为3个新数组,a1、a2、a3,其中它们将分别包含80%、10%和10%的图像,我将按以下方式进行操作(我不希望它们在a中是连续的): 将numpy导入为np 随机输入 a=--从文件中读取图像-- a1=numpy.empty((0,1,64,64)) a2=numpy.empty((0,

我有一个numpy形状阵列,其中包含许多(本例中为200个)单色64x64像素图像,因此具有以下形状:

>>> a.shape
(200L, 1L, 64L, 64L)
我想将这些图像拆分为3个新数组,
a1
a2
a3
,其中它们将分别包含80%、10%和10%的图像,我将按以下方式进行操作(我不希望它们在
a
中是连续的):

将numpy导入为np
随机输入
a=--从文件中读取图像--
a1=numpy.empty((0,1,64,64))
a2=numpy.empty((0,1,64,64))
a3=numpy.空((0,1,64,64))
对于范围(200)中的i:#200是图像数
温度=a[-1]
a=np.delete(a,-1,0)
rand=random.random()
如果兰德<0.8:
a1=np.append(a1[temp],0)
elsif rand<0.9:
a2=np.append(a2[temp],0)
其他:
a3=np.追加(a3[temp],0)
我尝试在列表上模拟在
O(1)
时间执行的
pop
append
,但对于numpy数组是否也适用? 有什么方法可以更高效(更快)地处理大量(数千)图像吗?

这里有一个使用-

1) 形状检查:

In [205]: a = np.random.rand(200,1,64,64)

In [206]: a1,a2,a3 = np.vsplit(a[np.random.permutation(a.shape[0])],(160,180))

In [207]: a.shape
Out[207]: (200, 1, 64, 64)

In [208]: a1.shape
Out[208]: (160, 1, 64, 64)

In [209]: a2.shape
Out[209]: (20, 1, 64, 64)

In [210]: a3.shape
Out[210]: (20, 1, 64, 64)
2) 对玩具数据进行值检查,以确保我们拾取的是随机图像,而不是用于分割的连续图像:

In [212]: a
Out[212]: 
array([[5, 8, 4],
       [7, 7, 6],
       [3, 2, 7],
       [1, 4, 8],
       [4, 1, 0],
       [2, 1, 3],
       [6, 5, 2],
       [2, 4, 5],
       [6, 6, 5],
       [5, 2, 5]])

In [213]: a1,a2,a3 = np.vsplit(a[np.random.permutation(a.shape[0])],(6,8))

In [214]: a1
Out[214]: 
array([[1, 4, 8],
       [7, 7, 6],
       [6, 6, 5],
       [2, 4, 5],
       [4, 1, 0],
       [5, 2, 5]])

In [215]: a2
Out[215]: 
array([[3, 2, 7],
       [2, 1, 3]])

In [216]: a3
Out[216]: 
array([[6, 5, 2],
       [5, 8, 4]])

很好,简单的解决方案,谢谢!在我的简单测试中,O(n)复杂度似乎足够快。顺便说一句,我知道这不是我最初的问题,但记忆呢?这会同时导致
a
和内存中的分割块,有没有办法不复制
a
In [205]: a = np.random.rand(200,1,64,64)

In [206]: a1,a2,a3 = np.vsplit(a[np.random.permutation(a.shape[0])],(160,180))

In [207]: a.shape
Out[207]: (200, 1, 64, 64)

In [208]: a1.shape
Out[208]: (160, 1, 64, 64)

In [209]: a2.shape
Out[209]: (20, 1, 64, 64)

In [210]: a3.shape
Out[210]: (20, 1, 64, 64)
In [212]: a
Out[212]: 
array([[5, 8, 4],
       [7, 7, 6],
       [3, 2, 7],
       [1, 4, 8],
       [4, 1, 0],
       [2, 1, 3],
       [6, 5, 2],
       [2, 4, 5],
       [6, 6, 5],
       [5, 2, 5]])

In [213]: a1,a2,a3 = np.vsplit(a[np.random.permutation(a.shape[0])],(6,8))

In [214]: a1
Out[214]: 
array([[1, 4, 8],
       [7, 7, 6],
       [6, 6, 5],
       [2, 4, 5],
       [4, 1, 0],
       [5, 2, 5]])

In [215]: a2
Out[215]: 
array([[3, 2, 7],
       [2, 1, 3]])

In [216]: a3
Out[216]: 
array([[6, 5, 2],
       [5, 8, 4]])