how to split dataset into train and test code example

Example 1: code for test and train split

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
 X, y, test_size=0.33, random_state=42)

Example 2: pandas split dataframe to train and test

train=df.sample(frac=0.8,random_state=200) #random state is a seed value
test=df.drop(train.index)

Example 3: train test split python

from sklearn.model_selection import train_test_split
				
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)

Example 4: train dev test split sklearn

train, validate, test = np.split(df.sample(frac=1), [int(.6*len(df)), int(.8*len(df))])

Example 5: 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 6: train_size

You have to specify this parameter only if you’re not specifying the test_size. This is the same as test_size, but instead you tell the class what percent of the dataset you want to split as the training set.