Python之 sklearn:sklearn中的train_test_split函数的简介及使用方法之详细攻略


火钳刘明
火钳刘明 2022-09-19 11:58:45 49025
分类专栏: 资讯

Python之 sklearn:sklearn中的train_test_split函数的简介及使用方法之详细攻略

目录

sklearn中的train_test_split函数的简介

train_test_split使用方法

1、基础用法


sklearn中的train_test_split函数的简介

官方文档https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html?highlight=train_test_splitsklearn.model_selection.train_test_split

sklearn.model_selection.train_test_split(*arrays, **options)[source]
Split arrays or matrices into random train and test subsets
Quick utility that wraps input validation and next(ShuffleSplit().split(X, y)) and application to input data into a single call for splitting (and optionally subsampling) data in a oneliner.

sklearn.model_selection.train_test_split(*数组,* *选项)[源]
将数组或矩阵分割成随机的序列和测试子集
包装输入验证和next的快速实用程序(ShuffleSplit())。拆分(X, y))和应用程序将数据输入到单个调用中,以便在oneliner中拆分(和可选的子采样)数据。

Parameters
*arrays:sequence of indexables with same length / shape[0]
Allowed inputs are lists, numpy arrays, scipy-sparse matrices or pandas dataframes.

test_size:float or int, default=None
If float, should be between 0.0 and 1.0 and represent the proportion of the dataset to include in the test split. If int, represents the absolute number of test samples. If None, the value is set to the complement of the train size. If train_size is also None, it will be set to 0.25.

train_size:float or int, default=None
If float, should be between 0.0 and 1.0 and represent the proportion of the dataset to include in the train split. If int, represents the absolute number of train samples. If None, the value is automatically set to the complement of the test size.

random_state:int or RandomState instance, default=None
Controls the shuffling applied to the data before applying the split. Pass an int for reproducible output across multiple function calls. See Glossary.

shuffle:bool, default=True
Whether or not to shuffle the data before splitting. If shuffle=False then stratify must be None.

stratify:array-like, default=None
If not None, data is split in a stratified fashion, using this as the class labels.

参数
*arrays:相同长度/形状的索引表的序列
允许的输入是列表、numpy数组、scipy稀疏矩阵或panda数据矩阵。

test_size:float或int,默认=无
如果是浮动的,则应该在0.0和1.0之间,并表示要包含在测试分割中的数据集的比例。如果int,表示测试样本的绝对数量。如果没有,则将该值设置为列车大小的补充。如果train_size也是None,那么它将被设置为0.25。

train_size:float或int,默认为无
如果是浮点数,则应该在0.0和1.0之间,并表示要包含在分割序列中的数据集的比例。如果int,表示列车样本的绝对数量。如果没有,该值将自动设置为测试大小的补充。

random_state:int或RandomState实例,默认为None
控制在应用分割之前应用于数据的变换。在多个函数调用之间传递可重复输出的int。看到术语表。

shuffle:bool,默认= True
是否在拆分前打乱数据。如果shuffle=False,则层必须为None。

stratify:array-like默认=没有
如果没有,则以分层的方式分割数据,并将其用作类标签。

Returns
splitting:list, length=2 * len(arrays)
List containing train-test split of inputs.

New in version 0.16: If the input is sparse, the output will be a scipy.sparse.csr_matrix. Else, output type is the same as the input type.

返回
splitting:list, length=2 *
列表包含训练测试的输入分割。

版本0.16中的新内容:如果输入是稀疏的,则输出将是scipy.sparse.csr_matrix.。否则,输出类型与输入类型相同。

train_test_split使用方法

1、基础用法

  1. -meta">>>> import numpy as np
  2. -meta">>>> from sklearn.model_selection import train_test_split
  3. -meta">>>> X, y = np.arange(10).reshape((5, 2)), range(5)
  4. -meta">>>> X
  5. array([[0, 1],
  6. [2, 3],
  7. [4, 5],
  8. [6, 7],
  9. [8, 9]])
  10. -meta">>>> list(y)
  11. [0, 1, 2, 3, 4]
  12. >>>
  13. -meta">>>> X_train, X_test, y_train, y_test = train_test_split(
  14. -meta">... X, y, test_size=0.33, random_state=42)
  15. ...
  16. -meta">>>> X_train
  17. array([[4, 5],
  18. [0, 1],
  19. [6, 7]])
  20. -meta">>>> y_train
  21. [2, 0, 3]
  22. -meta">>>> X_test
  23. array([[2, 3],
  24. [8, 9]])
  25. -meta">>>> y_test
  26. [1, 4]
  27. >>>
  28. -meta">>>> train_test_split(y, shuffle=False)
  29. [[0, 1, 2], [3, 4]]
文章知识点与官方知识档案匹配,可进一步学习相关知识

网站声明:如果转载,请联系本站管理员。否则一切后果自行承担。

本文链接:https://www.xckfsq.com/news/show.html?id=2210
赞同 0
评论 0 条
火钳刘明L0
粉丝 0 发表 4 + 关注 私信
上周热门
如何使用 StarRocks 管理和优化数据湖中的数据?  2951
【软件正版化】软件正版化工作要点  2872
统信UOS试玩黑神话:悟空  2833
信刻光盘安全隔离与信息交换系统  2728
镜舟科技与中启乘数科技达成战略合作,共筑数据服务新生态  1261
grub引导程序无法找到指定设备和分区  1226
华为全联接大会2024丨软通动力分论坛精彩议程抢先看!  165
2024海洋能源产业融合发展论坛暨博览会同期活动-海洋能源与数字化智能化论坛成功举办  163
点击报名 | 京东2025校招进校行程预告  163
华为纯血鸿蒙正式版9月底见!但Mate 70的内情还得接着挖...  159
本周热议
我的信创开放社区兼职赚钱历程 40
今天你签到了吗? 27
如何玩转信创开放社区—从小白进阶到专家 15
信创开放社区邀请他人注册的具体步骤如下 15
方德桌面操作系统 14
用抖音玩法闯信创开放社区——用平台宣传企业产品服务 13
我有15积分有什么用? 13
如何让你先人一步获得悬赏问题信息?(创作者必看) 12
2024中国信创产业发展大会暨中国信息科技创新与应用博览会 9
中央国家机关政府采购中心:应当将CPU、操作系统符合安全可靠测评要求纳入采购需求 8

加入交流群

请使用微信扫一扫!