Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python sklearn中的随机状态意义_Python_Scikit Learn - Fatal编程技术网

Python sklearn中的随机状态意义

Python sklearn中的随机状态意义,python,scikit-learn,Python,Scikit Learn,我正在sklearn中处理train\u test\u split,我就是不理解random\u state参数。它的功能到底是什么?我们为什么使用它 请提供一个必要的例子 提前感谢。随机状态在训练测试中的参数可帮助您在每次运行该代码时重现相同的结果 “随机状态”可确保生成的拆分是可复制的。Scikit learn使用随机排列来生成拆分。您提供的随机状态用作随机数生成器的种子。这确保了随机数以相同的顺序生成 不使用随机_状态参数 from sklearn.model_selection impo

我正在
sklearn
中处理
train\u test\u split
,我就是不理解
random\u state
参数。它的功能到底是什么?我们为什么使用它

请提供一个必要的例子


提前感谢。

随机状态
训练测试
中的参数可帮助您在每次运行该代码时重现相同的结果

“随机状态”可确保生成的拆分是可复制的。Scikit learn使用随机排列来生成拆分。您提供的随机状态用作随机数生成器的种子。这确保了随机数以相同的顺序生成

不使用随机_状态参数

from sklearn.model_selection import train_test_split

a = [1,5,6,7,8,6]
b = [2,3,5,2,1,4]

x1, x2, y1, y2 = train_test_split(a,b,test_size=0.25)

print(x1)
# output: [1, 6, 8, 7]

## run the code again

x1, x2, y1, y2 = train_test_split(a,b,test_size=0.25)

print(x1)
# output: [6, 8, 6, 7]
x1, x2, y1, y2 = train_test_split(a,b,test_size=0.25, random_state=42)

print(x1)
# output: [6, 6, 8, 7]

## run the code again
x1, x2, y1, y2 = train_test_split(a,b,test_size=0.25, random_state=42)

print(x1)
# output: [6, 6, 8, 7]
每次运行代码时,这些值都会更改

使用随机_状态参数

from sklearn.model_selection import train_test_split

a = [1,5,6,7,8,6]
b = [2,3,5,2,1,4]

x1, x2, y1, y2 = train_test_split(a,b,test_size=0.25)

print(x1)
# output: [1, 6, 8, 7]

## run the code again

x1, x2, y1, y2 = train_test_split(a,b,test_size=0.25)

print(x1)
# output: [6, 8, 6, 7]
x1, x2, y1, y2 = train_test_split(a,b,test_size=0.25, random_state=42)

print(x1)
# output: [6, 6, 8, 7]

## run the code again
x1, x2, y1, y2 = train_test_split(a,b,test_size=0.25, random_state=42)

print(x1)
# output: [6, 6, 8, 7]

正如您所见,相同的值已被复制,并且每次运行代码时都会创建相同的拆分。

下面@Akshay答案的可能重复非常好。另外,如果你不熟悉(psuedo)随机数生成的概念,我建议你看看维基百科上的。