Setting seed on train_test_split sklearn python
Asked Answered
S

2

15

is there any way to set seed on train_test_split on python sklearn. I have set the parameter random_state to an integer, but I still can not reproduce the result.

Thanks in advance.

Summerlin answered 16/5, 2019 at 10:12 Comment(0)
D
21
from sklearn.model_selection import train_test_split
x = [k for k in range(0, 10)]
y = [k for k in range(0, 10)]
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.4, random_state=11)
print (x_train)
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.4, random_state=11)
print (x_train)
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.4, random_state=11)
print (x_train)

The above code will produce the same result for x_train every time I split the data. It is possible that the randomness is in your dataframe, not train_test_split.

Disjunction answered 16/5, 2019 at 15:7 Comment(0)
V
2

simply in train_test_split, specify the parameter random_state=some_number_you_wan to use, like random_state=42

Venge answered 23/2, 2022 at 8:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.