๐ 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
- Problem with Outliers
- Visualization Strategy
- What is RANSAC?
- Mathematics
- Code Example
- CLI Output
- Key Insights
๐ Dataset Overview
The dataset contains:
- Average number of rooms (X)
- Median home price (Y) in $1000s
We want to understand how these variables relate.
⚠️ 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.
๐ค What is RANSAC?
RANSAC (Random Sample Consensus) is a robust regression algorithm designed to handle outliers.
How It Works
- Randomly sample subset of data
- Fit model
- Check how many points fit (inliers)
- Repeat multiple times
- 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.