Introduction to Normalizing Features

In today's lesson, we’ll examine an important preprocessing step for predictive modeling: normalizing features. Normalization adjusts the scale of feature data so that no single feature with a larger or smaller scale dominates the model. Our mission is to learn why normalization is necessary and to understand two primary methods of normalization, applying these techniques to the California Housing Dataset using Python.

The Importance of Normalization in Predictive Modeling

Normalization addresses the issue of features having different ranges. Without scaling, features with larger value ranges could unfairly influence the results of our predictive model. In simple terms, if one feature has values ranging from 0 to 100 and another from 0 to 1, the first feature might dominate the model training process. As we work with features like house age and median income, normalizing helps ensure that each feature contributes to the model based on its importance, not merely its scale.

Standard Scaling

Standard scaling is a method that rescales the features so that they have a mean of zero and a standard deviation of one. This method calculates the z-score of each data point, which represents how many standard deviations a data point is from the mean. Let's apply standard scaling using Python:

Python
import pandas as pd
from sklearn.datasets import fetch_california_housing
from sklearn.preprocessing import StandardScaler

# Fetch the dataset and create the DataFrame
housing_data = fetch_california_housing()
df = pd.DataFrame(housing_data.data, columns=housing_data.feature_names)
df['MedHouseValue'] = housing_data.target

# StandardScaler object
scaler = StandardScaler()

# Compute the mean and standard deviation based on the training data
scaler.fit(df[['HouseAge']])

# Perform the standardization by centering and scaling
housing_median_age_scaled = scaler.transform(df[['HouseAge']])

# Original vs. Standard Scaled Data
print("Original 'HouseAge' Head:")
print(df[['HouseAge']].head())
print("\nScaled 'HouseAge' Head:")
print(pd.DataFrame(housing_median_age_scaled, columns=['HouseAge']).head())
Original HouseAge Head: 41.0, 21.0, 52.0, 52.0, 52.0

Scaled HouseAge Head: 0.982143, -0.607019, 1.856182, 1.856182, 1.856182
Min-Max Scaling
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal