当前位置: 首页 > 工具软件 > HTML5test > 使用案例 >

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

郑锦
2023-12-01

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 类似资料: