Introduction to Outliers Detection and Treatment

Welcome to our detailed exploration of outliers detection and treatment in predictive modeling. Using real-life scenarios such as uneven pricing in housing markets, we will delve into statistical methodologies to identify outliers. Imagine an apartment costing significantly less or a mansion priced substantially higher than the standard in an area; these data points can skew the average, affecting our predictive analysis. In this session, we’re going to employ the California Housing Dataset to identify these critical data points and effectively execute robust treatment strategies.

Detecting Outliers with Z-Scores

To systematically identify outliers, we start by implementing the z-score method—a statistical measure that quantifies how many standard deviations a data point is from the mean. In mathematical terms, for a given data point (x), the z-score (z)(z) is calculated as:

z=(xμ)σz = \frac{(x - \mu)}{\sigma}

Where μ\mu is the mean and σ\sigma is the standard deviation of the dataset. A z-score beyond the threshold of three (either in the positive or negative direction) marks a data point as an outlier, much like a mansion priced comparably to a modest townhouse in a dataset of home values.

Let's explore the application of z-scores with Python to detect such outliers within the "Median Income" attribute:

import pandas as pd
from sklearn.datasets import fetch_california_housing

# Fetching the dataset
housing_data = fetch_california_housing()
df = pd.DataFrame(housing_data.data, columns=housing_data.feature_names)
df["MedHouseValue"] = housing_data.target

# Calculate Z-scores
df['MedInc_zscore'] = (df['MedInc'] - df['MedInc'].mean()) / df['MedInc'].std()

# Identifying outliers using the Z-score method
outliers_z = df[(df['MedInc_zscore'] > 3) | (df['MedInc_zscore'] < -3)]
print("Outliers based on Z-score method:", outliers_z[['MedInc', 'MedInc_zscore']], sep='\n')
Outliers based on Z-score method:
        MedInc  MedInc_zscore
131    11.6017       4.069344
409    10.0825       3.269690
510    11.8603       4.205463
511    13.4990       5.068017
512    12.2138       4.391533
...        ...            ...
20376  10.2614       3.363857
20380  10.1597       3.310326
20389  10.0595       3.257584
20426  10.0472       3.251110
20436  12.5420       4.564286

[345 rows x 2 columns]

This approach highlights the importance of not just recognizing an outlier, but understanding the degree to which a data point deviates from the norm within a specific dataset.

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