数据集:一个中文的酒店评论数据集(一万多条数据,正负向情感标注)
任务:切分为训练和发展集
数据集示例:
代码:
import os
import pandas as pd
from sklearn.utils import shuffle
if __name__ == '__main__':
path = "temp/"
pd_all = pd.read_csv(os.path.join(path, "zh_senti_dataset.tsv"),sep='\t')
pd_all = shuffle(pd_all)
dev_set = pd_all.iloc[0:int(pd_all.shape[0]/10)]
train_set = pd_all.iloc[int(pd_all.shape[0]/10): int(pd_all.shape[0])]
dev_set.to_csv("temp/dev.tsv", index=False, sep='\t')
train_set.to_csv("temp/train.tsv", index=False, sep='\t')
解释:主要用到了pandas的read_csv()、to_csv()方法实现数据集文件的读写,iloc函数实现对数据行的提取,用shuffle()方法实现打乱顺序。