将数据拆分为测试和训练,并转换为 csv 或 excel 文件

huy*_*huy 1 python

我有一个很大的数据集(大约 200k 行),我想将数据集随机分成两部分,70% 作为训练数据,30% 作为测试数据。有没有办法在 python 中做到这一点?注意我还想将这些数据集保存为我的计算机中的 Excel 或 csv 文件。谢谢!

小智 5

from sklearn.model_selection import train_test_split
#split the data into train and test set
train,test = train_test_split(data, test_size=0.30, random_state=0)
#save the data
train.to_csv('train.csv',index=False)
test.to_csv('test.csv',index=False)
Run Code Online (Sandbox Code Playgroud)