用sklearn.model_selection.train_test_split进行机器学习数据及划分

sklearn.model_selection.train_test_split(*arrays**options):将数组或矩阵随机划分为训练集和测试集两部分

参数:

*arrays : sequence of indexables with same length / shape[0]

允许输入lists, numpy arrays, scipy-sparse matrices 或者 pandas dataframes.

test_size : 小数、整数或者为空(默认为空)

如果是小数,需要介于0.0和1.0之间,代表测试集占输入数据集的百分比。如果是整数,代表测试集样本数量。如果为空,这个值自动设置为训练集补集的大小;如果训练集大小也设置为空,则默认为0.25.

train_size : 小数、整数或者为空(默认为空)

如果是小数,需要介于0.0和1.0之间,代表训练集占输入数据集的百分比。如果是整数,代表训练集样本数量。如果为空,这个值自动设置为测试集大小的补集.

random_state : 整数或者 RandomState

伪随机数种子用于随机抽样。

stratify : array-like or None (default is None)

If not None, data is split in a stratified fashion, using this as the class labels.

返回值:

splitting : 列表,长度为两倍数组大小

列表包含划分好的训练集和测试集.

0.16版本中新特性:如果输入的数据稀疏,输出将是一个scipy.sparse.csr_matrix. 另外,输出类型与输入类型一致.  

示例

>>>
>>> import numpy as np
>>> from sklearn.model_selection import train_test_split
>>> X, y = np.arange(10).reshape((5, 2)), range(5)
>>> X
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7],
       [8, 9]])
>>> list(y)
[0, 1, 2, 3, 4]
>>>
>>> X_train, X_test, y_train, y_test = train_test_split(
...     X, y, test_size=0.33, random_state=42)
...
>>> X_train
array([[4, 5],
       [0, 1],
       [6, 7]])
>>> y_train
[2, 0, 3]
>>> X_test
array([[2, 3],
       [8, 9]])
>>> y_test
[1, 4]


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值