sklearn的train_test_split函数的random_state

  我们使用sklearn进行机器学习之前,一般使用train_test_split来进行数据集的分割,其参数random_state代表什么呢?

>>>from sklearn.model_selection import train_test_split

>>> x = [1,2,3,4,5,6,7,8,9,10]
>>> y = [1,2,3,4,5,6,7,8,9,10]
x_train, x_test, y_train, y_test = train_test_split(
...     x, y, test_size=0.3)# 测试集比例为30%, random_state默认为None
>>> x_train, x_test
([7, 8, 3, 1, 9, 5, 2], [10, 6, 4])

#重新分割
>>> x_train, x_test, y_train, y_test = train_test_split(
...		x, y, test_size=0.3)
>>> x_train, x_test
([7, 8, 5, 4, 9, 1, 2], [6, 10, 3])
>>>

  可以看到,random_state默认状态下,两次分割的结果不一样

>>> x_train, x_test, y_train, y_test = train_test_split(
...		x, y, test_size=0.3, random_state=1)
>>> x_train, x_test
([5, 1, 4, 2, 8, 9, 6], [3, 10, 7])
>>> x_train, x_test, y_train, y_test = train_test_split(
...		x, y, test_size=0.3, random_state=1)
>>> x_train, x_test
([5, 1, 4, 2, 8, 9, 6], [3, 10, 7])

  设置了random_state之后,两次分割的结果一样

  结论: random_state的值相当于一种规则,通过设定为相同的数,每次分割的结果都是相同的

猜你喜欢

转载自blog.csdn.net/zhu_1997/article/details/89214966