0% found this document useful (0 votes)
9 views1 page

Outlier Detection Using IQR in Pandas

The document contains Python code for analyzing a housing dataset using pandas, seaborn, and matplotlib. It visualizes the distribution of numerical features through histograms and box plots, and detects outliers using the Interquartile Range (IQR) method. Finally, it provides a summary of the dataset's statistics.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views1 page

Outlier Detection Using IQR in Pandas

The document contains Python code for analyzing a housing dataset using pandas, seaborn, and matplotlib. It visualizes the distribution of numerical features through histograms and box plots, and detects outliers using the Interquartile Range (IQR) method. Finally, it provides a summary of the dataset's statistics.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import pandas as pd

import numpy as np
import seaborn as sns
import [Link] as plt
from [Link] import fetch_california_housing
housing_df=pd.read_csv("[Link]")
numerical_features = housing_df.select_dtypes(include=[[Link]]).columns
[Link](figsize=(15, 10))
for i, feature in enumerate(numerical_features):
[Link](3, 3, i + 1)
[Link](housing_df[feature], kde=True, bins=30, color='blue')
[Link](f'Distribution of {feature}')
plt.tight_layout()
[Link]()
[Link](figsize=(15, 10))
for i, feature in enumerate(numerical_features):
[Link](3, 3, i + 1)
[Link](x=housing_df[feature], color='orange')
[Link](f'Box Plot of {feature}')
plt.tight_layout()
[Link]()
print("Outliers Detection:")
outliers_summary = {}
for feature in numerical_features:
Q1 = housing_df[feature].quantile(0.25)
Q3 = housing_df[feature].quantile(0.75)
IQR = Q3 - Q1
lower_bound = Q1 - 1.5 × IQR
upper_bound = Q3 + 1.5 × IQR
outliers = housing_df[(housing_df[feature] < lower_bound) |
(housing_df[feature] > upper_bound)]
outliers_summary[feature] = len(outliers)
print(f"{feature}: {len(outliers)} outliers")
print("\nDataset Summary:")
print(housing_df.describe())

You might also like