Training, Validation, and Testing
How you split and test data decides whether your results can be trusted. This topic explains the purpose of each data split and shows how to avoid common mistakes that make models look better than they really are.
Why We Split Data
A model can memorize its training data and still fail on new data. To catch this, we test it on data it has never seen. The split gives us an honest picture of how it will behave in the real world.
If you evaluate a model on the same data you trained it on, the score will almost always look great. This is misleading, because it only shows that the model remembers, not that it learned.
Splitting is a simple idea with a big impact. It is the foundation of reliable model evaluation, and every project should use it from the start.
Train, Validation, and Test Sets
The training set is used to teach the model. The validation set is used to compare models and tune settings. The test set is used once at the end to measure final performance.
A common split is 60 percent training, 20 percent validation, and 20 percent testing. Smaller datasets may use different splits or cross-validation instead.
X_temp, X_test, y_temp, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
X_train, X_val, y_train, y_val = train_test_split(X_temp, y_temp, test_size=0.25, random_state=42)Never use the test set to make decisions about the model. If you do, it stops being a fair test. Keep it locked away until your final check.
Avoiding Data Leakage
Data leakage happens when information from outside the training data sneaks into the model. It makes results look great during testing but fail in real life. It is one of the most common and costly mistakes.
A typical example is scaling the whole dataset before splitting. The test data then influences the scaling values. Always fit transformations on the training data only, and then apply them to the test data.
Another example is using a feature that would not be known at prediction time, like a column recorded after the event. Review each feature and ask if it would really be available when the model is used.
Cross-Validation Basics
Cross-validation gives a more reliable estimate of performance, especially with small datasets. In K-fold cross-validation, the data is split into K parts. The model trains on K minus 1 parts and tests on the remaining part, and this repeats K times.
from sklearn.model_selection import cross_val_score
scores = cross_val_score(model, X_train, y_train, cv=5)
print(scores.mean())The average of the scores shows how well the model performs overall, and the spread shows how stable it is. A large spread means the model is sensitive to the data it sees.
Use stratified K-fold for classification, so each fold keeps the same class balance. For time series data, use time-based splits instead of random ones.
Data Science Lifecycle Tutorial
Wesline Rinisha








