Menu

Splitting the Dataset

Splitting the Dataset

To evaluate how well the model performs on unseen data, we divide the dataset into two parts:

  • Training Set: Used to train the model.
  • Testing Set: Used to evaluate the model.

A common split is:

  • 80% Training
  • 20% Testing

Split the Dataset

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

Explanation

  • test_size=0.2 reserves 20% of the data for testing.
  • random_state=42 ensures reproducible results.

Check the Shapes

print("Training Features:", X_train.shape)
print("Testing Features:", X_test.shape)
print("Training Labels:", y_train.shape)
print("Testing Labels:", y_test.shape)