如何根据百分比将列表分成3个部分?

时间:2018-03-29 13:00:37

标签: python

我有一个文件列表,我想将其分为3个部分:培训,验证和测试。 我试过这段代码而且我不知道它是否正确。

files = glob.glob("/dataset/%s/*" % emotion)
training = files[:int(len(files)*0.8)] #get first 80% of file list
validation = files[-int(len(files)*0.1):] #get middle 10% of file list
testing = files[-int(len(files)*0.1):] #get last 10% of file list

我不确定测试列表是否重复,或者文件列表的最后10%是否正确。

3 个答案:

答案 0 :(得分:3)

你可以利用numpy split:

train, validate, test = np.split(files, [int(len(files)*0.8), int(len(files)*0.9)])

答案 1 :(得分:2)

testing脚本是否与validation重复?是的,您以完全相同的方式创建它们,您要提取最后10%用于验证和测试:

files = [1,2,3,4,5,6,7,8,9,10]
training = files[:int(len(files)*0.8)] #[1, 2, 3, 4, 5, 6, 7, 8]
validation = files[-int(len(files)*0.1):] #[10]
testing = files[-int(len(files)*0.1):] #[10]

如果你想坚持原来的方法,我建议你做这样的事情(但是np方法更优雅):

files = [1,2,3,4,5,6,7,8,9,10]
training = files[:int(len(files)*0.8)] #[1, 2, 3, 4, 5, 6, 7, 8]
validation = files[int(len(files)*0.8):int(len(files)*0.9)] #[9]
testing = files[int(len(files)*0.9):] #[10]

答案 2 :(得分:-1)

与 zipa 的答案相同,但有一个独立的示例:

# splitting list of files into 3 train, val, test

import numpy as np

def split_two(lst, ratio=[0.5, 0.5]):
    assert(np.sum(ratio) == 1.0)  # makes sure the splits make sense
    train_ratio = ratio[0]
    # note this function needs only the "middle" index to split, the remaining is the rest of the split
    indices_for_splittin = [int(len(lst) * train_ratio)]
    train, test = np.split(lst, indices_for_splittin)
    return train, test

def split_three(lst, ratio=[0.8, 0.1, 0.1]):
    import numpy as np

    train_r, val_r, test_r = ratio
    assert(np.sum(ratio) == 1.0)  # makes sure the splits make sense
    # note we only need to give the first 2 indices to split, the last one it returns the rest of the list or empty
    indicies_for_splitting = [int(len(lst) * train_r), int(len(lst) * (train_r+val_r))]
    train, val, test = np.split(lst, indicies_for_splitting)
    return train, val, test

files = list(range(10))
train, test = split_two(files)
print(train, test)
train, val, test = split_three(files)
print(train, val, test)

输出:

[0 1 2 3 4] [5 6 7 8 9]
[0 1 2 3 4 5 6 7] [8] [9]

np.split docs.