2014-01-09 91 views
0

的Python 2.7.3 numpy的1.8.0使用劈裂一个numpy的ndarray片

大家好, 我使用numpy的几个月,我需要一些基本的东西帮助。下面的代码应该工作和位我需要被突出显示(#< < < < < < <)帮助:

import numpy as np 

rng = np.random.RandomState(12345) 

samples = np.array(np.arange(400).reshape(50, 8)) 
nSamples = samples.shape[0] 
FOLDS = 15 

foldSize = nSamples/FOLDS 

indices = np.arange(nSamples) 
rng.shuffle(indices) 

slices = [slice(i * foldSize , 
       (i + 1) * foldSize, 1) for i in xrange(FOLDS + 1)] 

for i in xrange(len(slices)): 
    y = samples[indices[slices[i]]] 
    x = np.array([x for x in samples if x not in samples[slices[i]]]) # <<<<<<< 
    #do some processing with x and y  

基本上随机切片的二维数组行明智地,使用全阵列处理和测试在切片位中,然后重复执行另一个切片,一切都完成了(它称为交叉验证实验)。

我的问题是:是否有更好的方法来选择ndarray中的所有行,但切片?我错过了什么吗?如果x不在采样[索引] [0:3]中,[x对于样本中的x是什么]?

在此先感谢。

ps:蒙面阵列不能解决我的问题。 ps1:我知道它已经在其他地方实施过了,我只需要学习。

回答

0

您可以创建一个布尔值数组的行选择如下:

indices_to_ignore = [1, 2, 3] 
mask = np.ones(samples.shape[:1], dtype=np.bool) 
mask[indices_to_ignore] = 0 
samples[mask].shape 
+0

很抱歉,但屏蔽不解决这个问题对我来说,我在PS说明。还有其他解决方案吗? – tbrittoborges