sklearn.model_selection.train_test_split

sklearn.model_selection.train_test_split

1、函数原型

  sklearn.model_selection.train_test_split(*arrays, **options)

2、函数功能

   train_test_split()是sklearn.cross_validation模块中用来随机划分训练集和测试集将数组或矩阵拆分为随机训练子集和测试子集

3、参数说明

  • *arrays:具有相同长度或者形状的可索引序列。 允许的输入是list,numpy数组,scipy-sparse matrices 或者pandas dataframes。
  • test_size:浮点数,整数或无,可选(默认=无)(test_size和train_size是互补和为1的一对值 )
    如果为float,则应在0.0到1.0之间,并且代表测试拆分中测试集test所占的比例。如果为int,则表示测试样本的绝对数量。如果为None,则将值设置为train_size的补码()。如果train_size也为None,则将其设置为0.25。
  • train_size:浮点数,整数或无,(默认=无)
    如果为float,则应在0.0到1.0之间,并表训练集所占比例。如果为int,则表示训练集的绝对数量。如果为空,则该值将自动设置为测试集的补码。
  • random_state:int,RandomState实例或空,可选(默认=无)
    如果为int,则random_state是随机数生成器使用的种子;否则为false。如果是RandomState实例,则random_state是随机数生成器;如果为None,则随机数生成器是np.random使用的RandomState实例。
  • shuffle:布尔值,可选(默认= True)
    拆分前是否对数据进行混洗。如果shuffle = False,则分层必须为None。
    *stratify :类数组或无(默认=无)
    如果不为None,则将数据用作类标签以分层方式拆分。

代码实例:

import numpy as np
from sklearn import model_selection
x,y = np.arange(25).reshape(5,5),range(5)
print("x = \n",x)
y = list(y)
print("y = \n",y)
X_train, X_test, y_train, y_test =model_selection.train_test_split(x, y, test_size=0.2, random_state = 42)

print("X_train\n",X_train)
print("y_train\n",y_train)
print("X_test\n",X_test)
print("y_test\n",y_test)
x = 
 [[ 0  1  2  3  4]
  [ 5  6  7  8  9]
  [10 11 12 13 14]
  [15 16 17 18 19]
  [20 21 22 23 24]]
y = 
 [0, 1, 2, 3, 4]
X_train
 [[20 21 22 23 24]
  [10 11 12 13 14]
  [ 0  1  2  3  4]
  [15 16 17 18 19]]
y_train
 [4, 2, 0, 3]
X_test
 [[5 6 7 8 9]]
y_test
 [1]

你可能感兴趣的:(python,ML&DL)