函数原型
sklearn.model_selection.
train_test_split
(*arrays,**options)
函数功能
将数组或矩阵随机的分割成训练集和测试集,注意这里是随机的。假定某元素A第一次划分到测试集中,对数据集进行新增操作之后,再进行划分,则A可能划分到训练集中。
函数允许一次传入多个数据集,所以我们也可以把行数相同的多个数据集添加为*arrays参数,它会根据相同索引将其拆分,当我们有一个单独的dataframe用于标记时,这种做法是非常有用的。
参数说明
例子
>>> import numpy as np>>> from sklearn.model_selection import train_test_split>>> X, y = np.arange(10).reshape((5, 2)), range(5)>>> Xarray([[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_trainarray([[4, 5],[0, 1],[6, 7]])>>> y_train[2, 0, 3]>>> X_testarray([[2, 3],[8, 9]])>>> y_test[1, 4]
如果觉得《Scikit-learn API:train_test_split函数 将数据分割为训练集和测试集》对你有帮助,请点赞、收藏,并留下你的观点哦!