2014-01-09 87 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