2016-11-17 24 views
0

我有一些數據在[[sample1], [sample2], ... , [sampleN]]形式的numpy數組中。我有一些形式爲[l1, l2, ..., lN]的標籤,其中li最多可以使用4個不同的值(即我的樣本包含在4組中)。我想從我的數據數組中選擇一些M < N樣本,並用它們訓練我的預測模型。然後,剩下的數據將被用作測試數據來檢查我的預測模型的準確性。預測標記數據(numpy)的最佳實踐

我對構建和測試這種預測模型的標準實踐並不是很熟悉。但是,我聽到了將數據集分爲兩部分的情況,一部分包含9/10數據作爲訓練數據,另一部分包含1/10數據作爲測試數據。我的問題是,這是正確的嗎?有沒有一些「最佳實踐」呢?我的另一個問題是,我如何從數據集數組中隨機選擇這兩組數據?

非常感謝您的幫助。

+1

檢查術語交叉驗證和使用scikit學習(這是建立在numpy的/ SciPy的)。例如[train_test_split](http://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html)函數。 – sascha

回答

1

事實上,你需要分割你的數據。你甚至需要將它分成3個,分別爲validation purposes

關於隨機拆分,您需要確保標籤和數據保持對齊,否則您將只學習數據中的隨機性。例如(Python的代碼,因爲你沒有提供任何代碼...)

from random import sample 

indices = sample(xrange(N), M) # generate M non repeating indices between 0 and N 
remaining_indices = list(set(xrange(N)) - set(indices)) # Use sets to easily get the indices you left behind 
train_set = data_set[indices] 
train_labels = labels[indices] 
test_set = data_set[remaining_indices] 
test_labels = labels[remaining_indices] 

您可以重複該過程以測試數據拆分成測試+驗證。也看看cross validation

正如mentionned由@Sascha它也全部建在Scikit-learn,一個非常有用的機器學習Python包