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)随机数生成的概念,我建议你看看维基百科上的。