A fundamental question in machine learning!
In machine learning, we typically split our dataset into two parts:
- Training set (Train): This is the portion of the dataset used to train the model, i.e., to learn the relationships between the input features and the target variable. The model sees this data, learns from it, and adjusts its parameters to minimize the error.
- Testing set (Test): This is the portion of the dataset used to evaluate the trained model, i.e., to assess how well the model generalizes to unseen data. The model does not see this data during training.
We calculate scores (also known as metrics or performance measures) on both the training and testing sets to understand the model's performance. Here's what they mean:
Train score (e.g., Train Accuracy, Train F1-score, Train R-squared):
- Reflects how well the model fits the training data.
- Measures the model's ability to memorize the training examples.
- A high train score indicates that the model has learned the patterns, relationships, and noise in the training data.
- Problem: A very high train score (e.g., 99%) might indicate overfitting: the model is too complex and has memorized the training data, including its noise and outliers, rather than learning generalizable patterns.
Test score (e.g., Test Accuracy, Test F1-score, Test R-squared):
- Reflects how well the model generalizes to unseen data.
- Measures the model's ability to predict new, unseen examples.
- A high test score indicates that the model has learned generalizable patterns from the training data and can apply them to new data.
- Goal: We want the test score to be as close as possible to the train score, indicating that the model has learned well and generalizes well.
Ideal scenario:
- High train score (≈ 90% or higher): The model has learned the training data well.
- Test score ≈ Train score (e.g., Train: 92%, Test: 90%): The model generalizes well to unseen data.
Common issues:
- Overfitting:
- Train score: very high (e.g., 98%)
- Test score: significantly lower (e.g., 80%)
- Fix: Regularization techniques (e.g., L1, L2, dropout), reduce model complexity, or collect more data.
- Underfitting:
- Train score: relatively low (e.g., 70%)
- Test score: similar to train score (e.g., 68%)
- Fix: Increase model complexity, add more features, or use a different algorithm.
- Good fit:
- Train score: high (e.g., 90%)
- Test score: close to train score (e.g., 88%)
Here's a simple Python example using scikit-learn to illustrate this:
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
# Load iris dataset
iris = load_iris()
X = iris.data
y = iris.target
# Split data into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Train a logistic regression model
logreg = LogisticRegression(max_iter=1000)
logreg.fit(X_train, y_train)
# Predict on train and test sets
y_pred_train = logreg.predict(X_train)
y_pred_test = logreg.predict(X_test)
# Calculate scores
train_accuracy = accuracy_score(y_train, y_pred_train)
test_accuracy = accuracy_score(y_test, y_pred_test)
print(f"Train Accuracy: {train_accuracy:.2f}") # e.g., 0.97
print(f"Test Accuracy: {test_accuracy:.2f}") # e.g., 0.93
In this example:
- The train accuracy (0.97) indicates the model has learned the training data well.
- The test accuracy (0.93) is close to the train accuracy, indicating the model generalizes well to unseen data.
Now, go ahead and check your own model's train and test scores!
Key takeaways:
- Monitor both train and test scores to diagnose overfitting, underfitting, or a good fit.
- Aim for a high train score and a test score that's close to the train score.
- Use various techniques (regularization, model selection, feature engineering) to improve your model's performance and generalizability.
Happy modeling!