Asked By – Elizabeth Susan Joseph
I want to implement a machine learning algorithm in scikit learn, but I don’t understand what this parameter
random_state does? Why should I use it?
I also could not understand what is a Pseudo-random number.
Now we will see solution for issue: Random state (Pseudo-random number) in Scikit learn
train_test_split splits arrays or matrices into random train and test subsets. That means that everytime you run it without specifying
random_state, you will get a different result, this is expected behavior. For example:
>>> a, b = np.arange(10).reshape((5, 2)), range(5) >>> train_test_split(a, b) [array([[6, 7], [8, 9], [4, 5]]), array([[2, 3], [0, 1]]), [3, 4, 2], [1, 0]]
>>> train_test_split(a, b) [array([[8, 9], [4, 5], [0, 1]]), array([[6, 7], [2, 3]]), [4, 2, 0], [3, 1]]
It changes. On the other hand if you use
random_state=some_number, then you can guarantee that the output of Run 1 will be equal to the output of Run 2, i.e. your split will be always the same.
It doesn’t matter what the actual
random_state number is 42, 0, 21, … The important thing is that everytime you use 42, you will always get the same output the first time you make the split.
This is useful if you want reproducible results, for example in the documentation, so that everybody can consistently see the same numbers when they run the examples.
In practice I would say, you should set the
random_state to some fixed number while you test stuff, but then remove it in production if you really need a random (and not a fixed) split.
Regarding your second question, a pseudo-random number generator is a number generator that generates almost truly random numbers. Why they are not truly random is out of the scope of this question and probably won’t matter in your case, you can take a look here form more details.