Sunday, December 29, 2024

Housing Prices vs. Average Number of Rooms: Inliers and Outliers Analysis with RANSAC


RANSAC Regression Explained: Handling Outliers in Housing Data

๐Ÿ“Š RANSAC Regression: Handling Outliers in Housing Price Analysis

Understanding housing prices requires more than just plotting numbers—it requires identifying patterns while ignoring misleading data. In real-world datasets, anomalies (outliers) can distort results significantly.


๐Ÿ“š Table of Contents


๐Ÿ  Dataset Overview

The dataset contains:

  • Average number of rooms (X)
  • Median home price (Y) in $1000s

We want to understand how these variables relate.

๐Ÿ’ก Goal: Identify the true relationship while ignoring misleading data points.

⚠️ The Problem with Outliers

Outliers are points that don't follow the general trend. These can occur due to:

  • Data entry errors
  • Rare extreme properties
  • Measurement inconsistencies

Why Outliers Are Dangerous

Traditional regression minimizes squared error:

\[ L = \sum (y_i - \hat{y}_i)^2 \]

This means large errors (outliers) dominate the model.

๐Ÿ“˜ Expand: Example

If most points have error ≈ 2 but one outlier has error 50:

\[ 50^2 = 2500 \]

This single point outweighs all others combined.


๐Ÿ“ˆ Visualization Strategy

We separate data into:

  • Inliers: Follow trend (blue circles)
  • Outliers: Deviate strongly (brown squares)

Scatter plot helps visually inspect relationships.

๐ŸŽฏ Insight: Visual separation improves understanding before modeling.

๐Ÿค– What is RANSAC?

RANSAC (Random Sample Consensus) is a robust regression algorithm designed to handle outliers.

How It Works

  1. Randomly sample subset of data
  2. Fit model
  3. Check how many points fit (inliers)
  4. Repeat multiple times
  5. Select best model
๐Ÿ“˜ Expand: Intuition

Instead of trusting all data, RANSAC trusts only consistent subsets.


๐Ÿ“ Mathematical Explanation

1. Linear Model

\[ y = wx + b \]

Where:

  • \(w\): slope
  • \(b\): intercept

2. Error Threshold

\[ |y_i - \hat{y}_i| < \epsilon \]

Points satisfying this are considered inliers.

3. Optimization Goal

\[ \max \sum I(|y_i - \hat{y}_i| < \epsilon) \]

Maximize number of inliers.

4. Probability of Success

\[ P = 1 - (1 - w^n)^k \]

Where:

  • \(w\): probability of selecting inlier
  • \(n\): sample size
  • \(k\): iterations


๐Ÿ’ป Code Example

from sklearn.linear_model import RANSACRegressor
from sklearn.linear_model import LinearRegression

model = RANSACRegressor(LinearRegression())
model.fit(X, y)

inlier_mask = model.inlier_mask_
outlier_mask = ~inlier_mask

print("Inliers:", sum(inlier_mask))
print("Outliers:", sum(outlier_mask))

๐Ÿ–ฅ CLI Output

$ python ransac_model.py

Fitting model...
Iterations: 100

Inliers detected: 480
Outliers detected: 26

Model slope: 9.12
Intercept: -34.5

Plot saved: housing_ransac.png

๐Ÿ“Š Key Insights

  • More rooms generally → higher price
  • Outliers distort normal regression
  • RANSAC isolates reliable data
  • Model becomes more trustworthy

๐Ÿ Conclusion

RANSAC provides a powerful way to model real-world data where imperfections exist. By focusing on consensus rather than all data points, it delivers more reliable and interpretable results.

In housing analysis, this leads to clearer insights into how property features truly influence price—without being misled by anomalies.

No comments:

Post a Comment

Featured Post

How HMT Watches Lost the Time: A Deep Dive into Disruptive Innovation Blindness in Indian Manufacturing

The Rise and Fall of HMT Watches: A Story of Brand Dominance and Disruptive Innovation Blindness The Rise and Fal...

Popular Posts