Sklearn的train_test_split

https://blog.csdn.net/fxlou/article/details/79189106			

在机器学习中,该函数可按照用户设定的比例,随机将样本集合划分为训练集测试集,并返回划分好的训练集和测试集数据。

语法

X_train,X_test, y_train, y_test =cross_validation.train_test_split(X,y,test_size, random_state)
 
 
  • 1

参数说明

CodeText
X待划分的样本特征集合
y待划分的样本标签
test_size若在0~1之间,为测试集样本数目与原始样本数目之比;若为整数,则是测试集样本的数目。
random_state随机数种子
X_train划分出的训练集数据(返回值)
X_test划分出的测试集数据(返回值)
y_train划分出的训练集标签(返回值)
y_test划分出的测试集标签(返回值)

代码示例
输入:

import numpy as np
from sklearn.model_selection import train_test_split

#创建一个数据集X和相应的标签y,X中样本数目为100
X, y = np.arange(200).reshape((100, 2)), range(100)

#用train_test_split函数划分出训练集和测试集,测试集占比0.33
X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.33, random_state=42)

#打印出原始样本集、训练集和测试集的数目
print("The length of original data X is:", X.shape[0])
print("The length of train Data is:", X_train.shape[0])
print("The length of test Data is:", X_test.shape[0])

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

输出:

The length of original data X is: 100
The length of train Data is: 67
The length of test Data is: 33
 
 
    • 0
      点赞
    • 0
      收藏
      觉得还不错? 一键收藏
    • 0
      评论
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值