python按比例拆分csv数据

1.引入所需包

import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split  # 划分数据集

2.读取数据

train_data = pd.read_csv("messages.csv")

3.进行分割

# 拆分数据
print("拆分数据中。。。")
split_ratio = 0.75  # 75%的训练数据
seed = 5  # 随机种子

# 分割训练集与测试集
xtrain, xtest, ytrain, ytest = train_test_split(
    train_data, range(train_data.shape[0]), test_size=split_ratio, random_state=seed)

print(xtest)
print("------------------")
print(xtrain)

 

你可能感兴趣的:(python按比例拆分csv数据)