numpy的:拆分随机数组 [英] Numpy: split array randomly

查看:1177
本文介绍了numpy的:拆分随机数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拿着形状的numpy的阵列多(200在这个例子中)单色64×64像素的图像,从而具有形状:

I have a numpy array of shape holding many (200 in this example) monochromatic 64x64 pixel images, thus having the shape:

>>> a.shape
(200L, 1L, 64L, 64L)

我要在3个新的阵列这些图像分割, A1 A2 A3 在那里他们将包含80%,10%,分别为图像的10%,而我在下面的方法做(我不希望他们在连续一个

I want to split these images in 3 new arrays, a1, a2, a3 where they will contain 80%, 10%, 10% of the images respectively, and I am doing it in the following way (I do not want them to be consecutive in a):

import numpy as np
import random

a = --read images from file--

a1 = numpy.empty((0,1,64,64))
a2 = numpy.empty((0,1,64,64))
a3 = numpy.empty((0,1,64,64))

for i in range(200): #200 is the number of images
    temp = a[-1]
    a = np.delete(a,-1,0)
    rand = random.random()
    if rand < 0.8:
        a1 = np.append(a1,[temp],0)
    elsif rand < 0.9:
        a2 = np.append(a2,[temp],0)
    else:
        a3 = np.append(a3,[temp],0)

我试图模仿弹出追加这是在做O(1) 时间列表,但确实为numpy的阵列相同的持有?
是否有某种方式为图像的大量(千)?

I try to emulate pop and append which are done at O(1) time on lists, but does the same hold for numpy arrays? Is there some way to do this more efficiently (faster) for a large number (thousands) of images?

推荐答案

下面是一个使用一行代码的 np.vsplit -

Here's a one-liner using np.vsplit -

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

1)形状检查:

1) Shape check :

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)玩具数据值检查,以确保我们随机挑选的图片和不连续的一个分裂:

2) Value check on a toy data to make sure we are picking random images and not consecutive ones for splitting :

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]])

这篇关于numpy的:拆分随机数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆