random_state python code example
Example 1: train test split sklearn
from sklearn.model_selection import train_test_split
X = df.drop(['target'],axis=1).values # independant features
y = df['target'].values # dependant variable
# Choose your test size to split between training and testing sets:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)
Example 2: random_state
Here you pass an integer, which will act as the seed for the random number generator during the split. Or, you can also pass an instance of the RandomState class, which will become the number generator. If you don’t pass anything, the RandomState instance used by np.random will be used instead.
Example 3: sklearn train test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
Example 4: what is random state
The random state is simply the lot number of the set generated randomly in any operation. We can specify this lot number whenever we want the same set again.