Menu

Train-Test Split Explained

Train-Test Split Explained

A train-test split divides your dataset into two parts: one for training the model and one for testing it, so you can check how well the model performs on data it has never seen before.

Why train-test split matters

  • It prevents overfitting by testing on unseen data.
  • It gives an honest measure of how the model will perform in the real world.
  • It is a required step before evaluating any machine learning model.

Splitting 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)

test_size=0.2 means 20% of the data is used for testing, while the remaining 80% is used for training. random_state=42 ensures the split is the same every time you run the code, making results reproducible.

A proper train-test split ensures your model is evaluated fairly and honestly, rather than judged on data it already memorised.


Machine Learning Tutorial

AP

Abhishek Pati



Learn via Video Course

Artificial Intelligence and Machine Learning Course Logo

Artificial Intelligence and Machine Learning Course

55 hrs



Get in Touch For Details