Skip to content
← All questions

How do I standardize features using StandardScaler in scikit-learn?

Import `StandardScaler` from `sklearn.preprocessing`, instantiate it, and call `fit_transform` on the selected DataFrame columns. This transforms the data to have mean 0 and standard deviation 1.

Conditions

  • Input data is numerical.
  • Scikit-learn library is installed.

Reasoning, step by step

  1. Import the class: `from sklearn.preprocessing import StandardScaler`.
  2. Create an instance: `scaling = StandardScaler()`.
  3. Apply to selected columns: `scaled_data = scaling.fit_transform(df[['Alcohol', 'Malic']])`.

Example

The video shows: `from sklearn.preprocessing import StandardScaler`, `scaling=StandardScaler()`, and `scaling.fit_transform(df[['Alcohol', 'Malic']])`, producing z-scores with positive and negative values.

Common misconceptions

  • Confusing `StandardScaler` with `MinMaxScaler`, resulting in incorrect scaling ranges.
  • Assuming the output is bounded between 0 and 1, whereas standardization allows any real value.

Watch the explanation

Connected concepts

Explore next

Related questions

Understand why

↗
Understand why

↗
Find a method

↗
Meet the concept

↗
Meet the concept

↗

Answers are generated from source material and independently checked. Consult the original video or creator if something is unclear.