Feature Scaling

Python · Advanced Python & AI

Theory

Feature scaling puts numeric features on comparable ranges. Without it, a feature measured in lakhs (salary) can overwhelm one measured in single digits (experience) simply because its numbers are bigger. StandardScaler subtracts each column's mean and divides by its standard deviation, so every column ends up with mean 0 and standard deviation 1 (a z-score). MinMaxScaler instead squeezes each column into the range 0 to 1.

In the diagram, experience [1, 3, 5] and salary [40000, 60000, 80000] have very different magnitudes, but after StandardScaler both become [-1.22, 0, 1.22]. The key rule is to fit the scaler on training data only - scaler.fit_transform(X_train) - and then use scaler.transform(X_test), so information from the test set never leaks into training. A Pipeline does this automatically.

Why we use it

Distance-based and gradient-based models treat bigger numbers as more important. Scaling gives every feature a fair say and helps optimisers converge faster and more reliably.

When to use it

Scale for KNN, SVM, K-Means, PCA, neural networks and regularised linear or logistic regression. Tree-based models (decision trees, random forests) do not need it, because splits depend only on order.

How it works

  1. Split data into train and test first
  2. scaler = StandardScaler()
  3. scaler.fit(X_train) learns each column's mean and std
  4. Transform X_train and X_test with the same scaler
  5. Is the model tree-based? Yes: Scaling is optional. No: Train on the scaled features.
  6. Model sees features on a common scale

Syntax

from sklearn.preprocessing import StandardScaler
X_scaled = StandardScaler().fit_transform(X)

Example

scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

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