Table of Contents

    Train-Test Split

    MACHINE LEARNING

    Train-Test Split

    Dividing your dataset into training and testing sets — the foundation of evaluating ML models.

    What is Train-Test Split?

    Train-Test Split is a Machine Learning technique used to divide the dataset into two parts: one for training the model and the other for testing its performance.

    In simple words — Train-Test Split helps us check how well our ML model performs on unseen data.

    Why is Train-Test Split Needed?

    If we train and test our model on the same data, the model will memorize the data instead of learning from it. This leads to:

    Without Splitting

    • Overfitting (model memorizes data)
    • False high accuracy
    • No way to test real performance
    • Model fails on new data

    With Train-Test Split

    • Honest evaluation
    • Detects overfitting
    • Measures generalization
    • Reliable model performance
    Golden Rule: A model is only good if it works well on unseen data, not the data it was trained on.

    Concept of Train-Test Split

    The full dataset is split into two parts:

    1

    Training Set

    Used to train the model. The model learns patterns from this data.

    Usually: 70%–80% of the dataset.

    2

    Testing Set

    Used to evaluate the model after training. The model has never seen this data.

    Usually: 20%–30% of the dataset.

    STANDARD SPLIT
    80% Training + 20% Testing

    Common Train-Test Ratios

    Ratio Training Testing Best Use Case
    80 / 2080%20%Most common — standard
    70 / 3070%30%Smaller datasets
    90 / 1090%10%Very large datasets
    60 / 20 / 2060%20% Validation + 20% TestWhen validation set is also needed

    Steps in Train-Test Split

    Process Flow

    • Step 1: Load and clean the dataset.
    • Step 2: Separate features (X) and target (y).
    • Step 3: Decide split ratio (e.g., 80/20).
    • Step 4: Use train_test_split() to divide data.
    • Step 5: Train the model on training data.
    • Step 6: Evaluate model on testing data.

    Python Example

    Prerequisites: Python 3.x, pandas, and scikit-learn.
    pip install pandas scikit-learn
    import pandas as pd
    from sklearn.model_selection import train_test_split
    
    # Sample dataset
    data = {
        "Age":    [25, 30, 35, 40, 45, 50, 55, 60],
        "Salary": [25000, 30000, 40000, 50000, 60000, 70000, 80000, 90000],
        "Buy":    [0, 0, 0, 1, 1, 1, 1, 1]
    }
    df = pd.DataFrame(data)
    
    # Separate features and target
    X = df[["Age", "Salary"]]
    y = df["Buy"]
    
    # Split data into train and test
    X_train, X_test, y_train, y_test = train_test_split(
        X, y,
        test_size=0.2,
        random_state=42
    )
    
    print("Training Features:\n", X_train)
    print("Testing Features:\n", X_test)
    Output Dataset is now properly divided into 80% training and 20% testing — ready for model training.

    Important Parameters of train_test_split()

    Parameter Description
    test_sizeProportion of dataset for testing (e.g., 0.2 = 20%).
    train_sizeProportion of dataset for training (optional).
    random_stateEnsures reproducibility — same split every run.
    shuffleWhether to shuffle data before splitting (default = True).
    stratifyMaintains class distribution (used in classification).

    Stratified Train-Test Split

    When the dataset is imbalanced (e.g., 90% Class A, 10% Class B), a normal split may put all Class B examples in one set. To prevent this, we use stratification to keep the same class ratio in both sets.

    X_train, X_test, y_train, y_test = train_test_split(
        X, y,
        test_size=0.2,
        stratify=y,
        random_state=42
    )
    Why Useful Maintains the proportion of classes — important for classification problems.

    What is random_state?

    random_state is a seed value that ensures the same split happens every time you run the code. This is useful for:

    • Reproducible experiments.
    • Debugging models.
    • Comparing different ML algorithms fairly.
    Common value used: random_state = 42 (a tradition in the data science community).

    Train-Validation-Test Split (3-Way Split)

    Sometimes, we split data into three parts instead of two — adding a Validation Set to fine-tune the model.

    Set Purpose Usual %
    Training SetTrain the model60%
    Validation SetTune hyperparameters20%
    Testing SetFinal evaluation20%
    from sklearn.model_selection import train_test_split
    
    # Step 1: Split into train+val and test
    X_temp, X_test, y_temp, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
    
    # Step 2: Split train+val into train and validation
    X_train, X_val, y_train, y_val = train_test_split(X_temp, y_temp, test_size=0.25, random_state=42)
    # 0.25 of 80% = 20% of original data → Validation

    Real-Life Analogy

    Train-Test Split = Studying vs Exam

    Think of the training set as the chapters you study, and the test set as the exam questions. If you only practice the same questions repeatedly (no separate test), you can't tell whether you truly learned the topic or just memorized answers.

    Visual Representation

    Training (80%) Testing (20%)
    Model learns patterns here Model is evaluated here

    How Train-Test Split Helps Detect Overfitting

    Overfitting

    • High training accuracy
    • Low testing accuracy
    • Model memorizes data

    Good Fit

    • High training accuracy
    • High testing accuracy
    • Model generalizes well

    Underfitting

    • Low training accuracy
    • Low testing accuracy
    • Model is too simple

    Common Mistakes to Avoid

    Mistake 1 Performing preprocessing (scaling/encoding) on the entire dataset before splitting → causes data leakage.
    Mistake 2 Not using random_state → splits change every run, making results inconsistent.
    Mistake 3 Using a small test set on a small dataset → unreliable evaluation.
    Mistake 4 Not using stratify on imbalanced classification problems.
    Correct Workflow Always split first, then perform preprocessing using only training data parameters.

    Best Practices

    Quick Tips

    • Always set random_state for reproducibility.
    • Use stratify=y for classification problems.
    • Split before scaling or encoding.
    • Use a separate validation set when tuning hyperparameters.
    • For small datasets, use k-fold cross-validation instead.
    • Keep the test set untouched until the final evaluation.

    Importance of Train-Test Split

    Honest Evaluation

    • Measures real-world performance
    • Detects overfitting

    Prevents Bias

    • Test data is unseen
    • Fair model assessment

    Improves Reliability

    • Helps fine-tune models
    • Ensures stable accuracy

    Reproducible Results

    • Consistent experiments
    • Allows fair comparison

    Mathematical View

    If a dataset has N samples, then:

    FORMULA
    $$ N_{train} = N \times (1 - t) $$ $$ N_{test} = N \times t $$

    Where t = test_size (e.g., 0.2 means 20%).

    Key Takeaway

    Train-Test Split is the foundation of Machine Learning evaluation. By dividing your dataset into training and testing parts, you ensure that your model not only learns well but also performs reliably on new, unseen data.