Python之 sklearn:sklearn中的train_test_split函数的简介及使用方法之详细攻略
目录
sklearn中的train_test_split函数的简介
train_test_split使用方法
1、基础用法
sklearn中的train_test_split函数的简介
官方文档:https://scikit-learn/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(*数组,* *选项)[源] |
Parameters test_size:float or int, default=None train_size:float or int, default=None random_state:int or RandomState instance, default=None shuffle:bool, default=True stratify:array-like, default=None | 参数 test_size:float或int,默认=无 train_size:float或int,默认为无 random_state:int或RandomState实例,默认为None shuffle:bool,默认= True stratify:array-like默认=没有 |
Returns 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. | 返回 版本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]]
更多推荐
Python之 sklearn:sklearn中的train_test_split函数的简介及使用方法之详细攻略
发布评论