2017-08-14 25 views
0

我有一個數據集,我不知道其中的記錄數。 我想手動實現交叉驗證,長話短說我想將我的數據劃分爲10倍並將每個摺疊保存在數組或列表中。 我該怎麼辦? 我感謝任何幫助。如何將數據劃分爲10倍並保存在Python數組中

+0

所以你基本上問如何將一個數據集,也許是一個列表,分成10個平等的子列表? – Miket25

+0

@ Miket25是的。而已。 –

回答

2

您可以使用這樣的事情:

length = int(len(data)/10) #length of each fold 
folds = [] 
for i in range(9): 
    folds += [data[i*length:(i+1)*length]] 
folds += [data[9*length:len(data)]] 

要獲得包含數組或列表的1/10日列出了一個名單,與上次包含剩餘部分。

1

假設你的數據集是numpy的陣列data_set,其中列有訓練的特點,行num_rows是不同的訓練實例(樣品),你可以使用:

import numpy as np 

N_folds = 10 
# Determine the correct indices to split the data. 
limits = np.linspace(0, num_rows+1, N_fold+1, dtype=int) 

for i in range(len(limits) - 1): 
    # Split the data at the correct indices and save it. 
    np.save('arr_{}.npy'.format(i), data_set[limits[i] : limits[i+1]]) 
相關問題