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

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

sklearn中的train_test_split函数的简介


官方文档:https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html?highlight=train_test_split#sklearn.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、基础用法


>>> 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]

>>>

>>> train_test_split(y, shuffle=False)

[[0, 1, 2], [3, 4]]




相关文章
|
2天前
|
数据挖掘 数据处理 索引
python常用pandas函数nlargest / nsmallest及其手动实现
python常用pandas函数nlargest / nsmallest及其手动实现
18 0
|
5天前
|
算法 Python
请解释Python中的关联规则挖掘以及如何使用Sklearn库实现它。
使用Python的mlxtend库,可以通过Apriori算法进行关联规则挖掘。首先导入TransactionEncoder和apriori等模块,然后准备数据集(如购买行为列表)。对数据集编码并转换后,应用Apriori算法找到频繁项集(设置最小支持度)。最后,生成关联规则并计算置信度(设定最小置信度阈值)。通过调整这些参数可以优化结果。
25 9
|
7天前
|
机器学习/深度学习 数据采集 算法
请解释Python中的Sklearn库以及它的主要用途。
Sklearn是Python的机器学习库,提供数据预处理、特征选择、分类回归、聚类、模型评估和参数调优等工具。包含监督和无监督学习算法,如SVM、决策树、K-means等,并提供样例数据集便于实践。它是进行机器学习项目的重要资源。
14 1
|
7天前
|
Serverless 开发者 Python
《Python 简易速速上手小册》第3章:Python 的函数和模块(2024 最新版)
《Python 简易速速上手小册》第3章:Python 的函数和模块(2024 最新版)
40 1
|
8天前
|
索引 Python
Python高维变量选择:SCAD平滑剪切绝对偏差惩罚、Lasso惩罚函数比较
Python高维变量选择:SCAD平滑剪切绝对偏差惩罚、Lasso惩罚函数比较
10 0
|
9天前
|
Python
python学习-函数模块,数据结构,字符串和列表(下)
python学习-函数模块,数据结构,字符串和列表
49 0
|
9天前
05-python之函数-函数的定义/函数的参数/函数返回值/函数说明文档/函数的嵌套使用/函数变量的作用域
05-python之函数-函数的定义/函数的参数/函数返回值/函数说明文档/函数的嵌套使用/函数变量的作用域
|
11天前
|
Python
python学习10-函数
python学习10-函数
|
15天前
|
安全 Java 数据处理
Python网络编程基础(Socket编程)多线程/多进程服务器编程
【4月更文挑战第11天】在网络编程中,随着客户端数量的增加,服务器的处理能力成为了一个重要的考量因素。为了处理多个客户端的并发请求,我们通常需要采用多线程或多进程的方式。在本章中,我们将探讨多线程/多进程服务器编程的概念,并通过一个多线程服务器的示例来演示其实现。
|
15天前
|
程序员 开发者 Python
Python网络编程基础(Socket编程) 错误处理和异常处理的最佳实践
【4月更文挑战第11天】在网络编程中,错误处理和异常管理不仅是为了程序的健壮性,也是为了提供清晰的用户反馈以及优雅的故障恢复。在前面的章节中,我们讨论了如何使用`try-except`语句来处理网络错误。现在,我们将深入探讨错误处理和异常处理的最佳实践。