Train Test Split

Python · Machine Learning (Scikit-Learn)

Theory

A model must be judged on data it has never seen, otherwise you only measure how well it memorised. train_test_split from sklearn.model_selection shuffles the rows and divides X and y into a training set (used to fit) and a test set (held back for evaluation). test_size=0.25 keeps 25% for testing, and it returns four objects in a fixed order: X_train, X_test, y_train, y_test. random_state fixes the shuffle so the split is the same every run.

For classification, a plain random split can leave the test set with too few or too many of a rare class. stratify=y keeps the class proportions the same in both parts: with 4 frauds in 20 rows (20%), the stratified test set of 5 rows gets exactly 1 fraud. Fit scalers, encoders and models on the training part only, and for time-ordered data split by date instead of shuffling.

Why we use it

It gives an honest estimate of performance on new data and exposes overfitting. Stratifying keeps rare classes represented, so precision and recall on the test set mean something.

When to use it

Split once at the start, before any fitting or scaling. Add stratify=y for classification, especially with imbalanced classes. For small datasets prefer cross-validation, and for time series use a date cut-off or TimeSeriesSplit.

How it works

  1. Features X and target y
  2. Classification with imbalanced classes? Yes: Add stratify=y. No: Plain random split.
  3. Shuffle with a fixed random_state
  4. Hold back test_size of the rows as the test set
  5. Fit everything on X_train, y_train only
  6. Evaluate once on X_test, y_test

Syntax

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, stratify=y)

Example

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42, stratify=y)
print(y_train.mean(), y_test.mean())

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic