Bird
Raised Fist0
ML Pythonml~8 mins

Train-test split in ML Python - Model Metrics & Evaluation

Choose your learning style10 modes available
Metrics & Evaluation - Train-test split
Which metric matters for Train-test split and WHY

When we split data into train and test sets, the main goal is to check how well our model works on new data. So, the key metrics are test accuracy, precision, recall, and F1 score. These tell us if the model learned well and can predict correctly on unseen data.

We focus on test metrics because training metrics alone can be misleading. A model might do great on training data but fail on new data if it memorizes instead of learning patterns.

Confusion matrix example
      Actual \ Predicted | Positive | Negative
      -------------------|----------|---------
      Positive           |    40    |   10    
      Negative           |    5     |   45    

      Total samples = 40 + 10 + 5 + 45 = 100
    

From this matrix:

  • True Positives (TP) = 40
  • False Positives (FP) = 5
  • True Negatives (TN) = 45
  • False Negatives (FN) = 10

Precision = TP / (TP + FP) = 40 / (40 + 5) = 0.89

Recall = TP / (TP + FN) = 40 / (40 + 10) = 0.80

F1 Score = 2 * (Precision * Recall) / (Precision + Recall) = 2 * (0.89 * 0.80) / (0.89 + 0.80) ≈ 0.84

Precision vs Recall tradeoff with examples

When using train-test split, we want to balance precision and recall on the test set.

Example 1: Spam filter
High precision is important. We want to avoid marking good emails as spam (false positives). So, we accept missing some spam (lower recall) to keep precision high.

Example 2: Disease detection
High recall is critical. We want to catch as many sick patients as possible (true positives). Missing a sick patient (false negative) is worse than a false alarm.

Train-test split helps us see how well the model balances these on new data.

Good vs Bad metric values for train-test split

Good: Test accuracy close to training accuracy, and balanced precision and recall (e.g., both above 0.8). This means the model learned well and generalizes.

Bad: Very high training accuracy but low test accuracy or very low recall or precision on test data. This shows overfitting or poor generalization.

Common pitfalls with train-test split metrics
  • Accuracy paradox: High accuracy can be misleading if data is imbalanced (e.g., 95% negative cases).
  • Data leakage: If test data leaks into training, test metrics look too good but are false.
  • Overfitting: Big gap between train and test metrics means model memorizes training data.
  • Small test set: Too small test data can give unstable or misleading metrics.
Self-check question

Your model has 98% accuracy but only 12% recall on fraud cases in the test set. Is it good for production?

Answer: No. Even though accuracy is high, the model misses most fraud cases (low recall). For fraud detection, catching fraud (high recall) is critical. This model is not reliable in real use.

Key Result
Test metrics like precision, recall, and F1 on the test set show if the model learned well and generalizes beyond training data.

Practice

(1/5)
1. What is the main purpose of using a train-test split in machine learning?
easy
A. To check how well the model performs on new, unseen data
B. To increase the size of the training data
C. To reduce the number of features in the dataset
D. To speed up the training process by using less data

Solution

  1. Step 1: Understand the purpose of train-test split

    Train-test split divides data into training and testing parts to evaluate model generalization.
  2. Step 2: Identify the correct purpose

    The test set simulates new data to check model performance outside training data.
  3. Final Answer:

    To check how well the model performs on new, unseen data -> Option A
  4. Quick Check:

    Train-test split = Check model on new data [OK]
Hint: Train-test split tests model on unseen data [OK]
Common Mistakes:
  • Thinking it increases training data size
  • Confusing it with feature selection
  • Believing it speeds up training by reducing data
2. Which of the following is the correct way to import the train_test_split function from scikit-learn?
easy
A. from sklearn import train_test_split
B. import train_test_split from sklearn
C. from sklearn.model_selection import train_test_split
D. import train_test_split from sklearn.model_selection

Solution

  1. Step 1: Recall correct import syntax in Python

    Python uses 'from module import function' to import specific functions.
  2. Step 2: Match with sklearn structure

    The function is in sklearn.model_selection, so correct import is from sklearn.model_selection import train_test_split.
  3. Final Answer:

    from sklearn.model_selection import train_test_split -> Option C
  4. Quick Check:

    Correct import = from sklearn.model_selection import train_test_split [OK]
Hint: Use 'from sklearn.model_selection import train_test_split' [OK]
Common Mistakes:
  • Using wrong import order
  • Trying to import directly from sklearn
  • Using 'import' instead of 'from ... import'
3. What will be the output shape of X_train and X_test after this code?
from sklearn.model_selection import train_test_split
X = [[1], [2], [3], [4], [5]]
X_train, X_test = train_test_split(X, test_size=0.4, random_state=42)
print(len(X_train), len(X_test))
medium
A. 2 3
B. 5 0
C. 4 1
D. 3 2

Solution

  1. Step 1: Calculate test set size

    Test size is 40% of 5 samples = 2 samples in test set.
  2. Step 2: Calculate train set size

    Remaining samples go to train set: 5 - 2 = 3 samples.
  3. Final Answer:

    3 2 -> Option D
  4. Quick Check:

    Train size = 3, Test size = 2 [OK]
Hint: Multiply total by test_size for test count [OK]
Common Mistakes:
  • Rounding test size incorrectly
  • Swapping train and test sizes
  • Ignoring random_state effect on split size
4. Identify the error in this code snippet:
from sklearn.model_selection import train_test_split
X = [1, 2, 3, 4, 5]
X_train, X_test = train_test_split(X, test=0.3)
print(X_train, X_test)
medium
A. The parameter name should be test_size, not test
B. train_test_split cannot split lists, only arrays
C. random_state parameter is missing
D. X should be a 2D list, not 1D

Solution

  1. Step 1: Check parameter names for train_test_split

    The correct parameter to set test set size is 'test_size', not 'test'.
  2. Step 2: Confirm other parameters

    train_test_split accepts lists and random_state is optional; 1D list is valid input.
  3. Final Answer:

    The parameter name should be test_size, not test -> Option A
  4. Quick Check:

    Correct parameter = test_size [OK]
Hint: Use 'test_size' parameter name exactly [OK]
Common Mistakes:
  • Using 'test' instead of 'test_size'
  • Thinking random_state is mandatory
  • Believing input must be 2D
5. You have a dataset with 1000 samples. You want to split it so that the test set has exactly 250 samples. Which test_size value should you use in train_test_split?
hard
A. 0.3
B. 0.25
C. 0.2
D. 0.4

Solution

  1. Step 1: Calculate test_size fraction

    Test samples desired = 250 out of 1000 total, so fraction = 250 / 1000 = 0.25.
  2. Step 2: Match fraction to test_size parameter

    Set test_size=0.25 to get 250 samples in test set.
  3. Final Answer:

    0.25 -> Option B
  4. Quick Check:

    Test size fraction = 250/1000 = 0.25 [OK]
Hint: Divide test samples by total samples for test_size [OK]
Common Mistakes:
  • Using absolute number instead of fraction
  • Choosing wrong fraction like 0.2 or 0.3
  • Confusing train_size with test_size
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard