2017-01-22 20 views
0

我正在學習神經網絡,並試圖自動化一些過程。 現在,我有代碼隨機分割數據集,一個284807x31塊。然後,我需要分離輸入和輸出,這意味着我需要選擇整個數組直到最後一列,而另一方面,只選擇最後一列。出於某種原因,我無法弄清楚如何正確地做到這一點,並且我堅持按照上面的解釋分割和分離集合。這裏是我到目前爲止的代碼(指代這一特定問題的部分):NumPy - 獲取列和行號來重新整形數組

train, test, cv = np.vsplit(data[np.random.permutation(data.shape[0])], (6,8)) 

# Should select entire array except the last column 
train_inputs = np.resize(train, len(train[:,1]), -1) 
test_inputs = np.resize(test, len(test[:,1]), -1) 
cv_inputs = np.resize(cv, len(cv[:,1]), -1) 

# Should select **only** the last column. 
train_outs = train[:, 30] 
test_outs = test[:, 30] 
cv_outs = test[:, 30] 

的想法是,我想在機器中找到相應的數據集的列數和做打算調整大小。第二部分將只選擇最後一列 - 我不確定這是否有效,因爲腳本在此之前停止。該錯誤是,順便說一句:

Traceback (most recent call last): 
    File "src/model.py", line 43, in <module> 
    train_inputs = np.resize(train, len(train[:,1]), -1) 
TypeError: resize() takes exactly 2 arguments (3 given) 

PS:現在我在看文件,我可以看到我很遠解決方案,但我真的無法弄清楚。這是我第一次使用NumPy。

在此先感謝。

+0

'np.resize'是一個很少使用的函數。 'reshape'更有用(如http://stackoverflow.com/questions/41795638/collapsing-all-dimensions-of-numpy-array-except-the-first-two)。如答案中所示的索引切片非常常見。也許你習慣於在'VBA'或其他語言中使用'resize'。 – hpaulj

+0

我明白了。謝謝!我不習慣任何語言。只是一個試圖學習新技能的人,我正在尋找問題。我發現解決其中一個問題,但顯然不是一個好的解決方案:) –

回答

2

一些切片應該有所幫助:

如果選擇除最後一列

train_inputs = train[:,:-1] 
test_inputs = test[:,:-1] 
cv_inputs = cv[:,:-1] 

和整個數組:

選送最後一列。

train_outs = train[:,-1] 
test_outs = test[:, -1] 
cv_outs = test[:, -1] 
+0

我認爲這解決了這個問題,即使我得到另一個問題,但這是與代碼的其他部分。謝謝! –