Feature Scaling

Python · Machine Learning (Scikit-Learn)

Theory

Features often sit on very different scales: age runs 25-53 while income in rupees runs into lakhs. Models that use distances or gradients, such as KNN, KMeans, SVM, PCA and regularised linear or logistic regression, are dominated by the column with the biggest numbers unless you rescale. StandardScaler subtracts each column's mean and divides by its standard deviation, so every column has mean 0 and standard deviation 1 (a z-score). MinMaxScaler maps each column to the range 0 to 1 with (x - min) / (max - min).

Scalers learn their numbers (mean, std, min, max) in fit, so call fit or fit_transform on the training set only and then transform the test set with the same values. A test value outside the training range simply goes below 0 or above 1. Tree-based models (decision trees, random forests) split on thresholds and do not need scaling.

Why we use it

Without scaling, a column measured in large units silently gets more weight in distance and gradient-based models. Scaling makes features comparable, helps solvers converge and often improves accuracy noticeably.

When to use it

Use StandardScaler as the default for linear models, KNN, SVM, PCA and KMeans; MinMaxScaler when you need a fixed 0-1 range. Skip scaling for tree models. Always put the scaler inside a Pipeline so it is fitted on training folds only.

How it works

  1. Numeric features on different scales
  2. Is the model tree-based? Yes: No scaling needed. No: Choose a scaler.
  3. Need a fixed 0-1 range? Yes: MinMaxScaler. No: StandardScaler (default).
  4. scaler.fit(X_train) learns mean/std or min/max
  5. transform X_train and X_test with the same numbers
  6. Comparable features for the model

Syntax

from sklearn.preprocessing import StandardScaler, MinMaxScaler
scaler = StandardScaler()
X_train_s = scaler.fit_transform(X_train)
X_test_s = scaler.transform(X_test)   # never fit on test

Example

scaler = StandardScaler()
X_train_s = scaler.fit_transform(X_train)
X_test_s = scaler.transform(X_test)
print(scaler.mean_, scaler.scale_)

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