python将数据集按比例随机切分

# -*- coding: utf-8 -*-
"""
    将数据按比例切分
"""

from sklearn import model_selection

c = []
j = 0
#filename = r'E:\NER\CCKS2020\Data\ccks2020_2_task1_train\task1_train.txt'
filename = open(r'task1_train.txt','r',encoding='utf-8')
out_train = open(r'train.txt', 'w',encoding='utf-8')
out_test = open(r'temp.txt', 'w',encoding='utf-8')
for line in filename:
    #    items = line.strip().split()
    c.append(line)

c_train, c_test = model_selection.train_test_split(c, test_size=0.2)
for i in c_train:
    out_train.write(i)
for i in c_test:
    out_test.write(i)

你可能感兴趣的:(python将数据集按比例随机切分)