Introduction
In this lesson, we focus on identifying outliers and trends within datasets using the Pandas library. As a critical part of Exploratory Data Analysis (EDA), recognizing outliers can help in understanding the variability in your data, and identifying trends aids in forecasting and decision-making. We'll cover statistical methods to detect outliers, visualize them using box plots and scatter plots, and use moving averages to find trends. Additionally, we'll leverage Z-scores for trend validation, providing a comprehensive approach to understanding and interpreting complex datasets.
Identifying Outliers
Definition of Outliers
Outliers are data points that differ significantly from other observations in your dataset. They can occur due to variability in the measurement or indicate experimental errors.
Common Statistical Methods
-
Interquartile Range (IQR): A robust method where outliers are data points outside the range defined by:
Q1 = df['column'].quantile(0.25) Q3 = df['column'].quantile(0.75) IQR = Q3 - Q1 outliers = df[((df['column'] < (Q1 - 1.5 * IQR)) | (df['column'] > (Q3 + 1.5 * IQR)))] -
Z-score: Measures how many standard deviations a data point is from the mean.
from scipy import stats df['z_score'] = stats.zscore(df['column']) outliers_z = df[(df['z_score'] > 3) | (df['z_score'] < -3)]
Visualizing Outliers
-
Box Plots: Useful for visualizing the distribution and highlighting outliers.
df.boxplot(column='column', grid=False) -
Scatter Plots: Helps identify outliers in bivariate data.
df.plot.scatter(x='column1', y='column2')
Identifying Trends
Moving Averages
Moving averages smooth out short-term fluctuations and highlight longer-term trends in data.
-
Simple Moving Average (SMA):
df['SMA'] = df['column'].rolling(window=5).mean() df[['column', 'SMA']].plot() -
Exponential Moving Average (EMA): (gives more weight to recent observations)
df['EMA'] = df['column'].ewm(span=5, adjust=False).mean() df[['column', 'EMA']].plot()
Using Z-scores for Trend Validation
Z-scores can also validate trends. A consistent climb in Z-scores might indicate an upward trend.
-
Code Example for Visualization:
df['trend_zscore'] = stats.zscore(df['column']) df[['column', 'trend_zscore']].plot()
Practical Example
Let's assume we have a dataset containing daily temperature recordings, and we want to identify unusual temperatures and average trends.
import pandas as pd
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
data = {'temperature': [22, 21, 23, 25, 30, 50, 21, 22, 23, 27, 28, 250]}
df = pd.DataFrame(data)
# Detecting Outliers
Q1 = df['temperature'].quantile(0.25)
Q3 = df['temperature'].quantile(0.75)
IQR = Q3 - Q1
outliers = df[(df['temperature'] < (Q1 - 1.5 * IQR)) | (df['temperature'] > (Q3 + 1.5 * IQR))]
print("Outliers: \n", outliers)
# Visualizing
plt.figure(figsize=(10,5))
plt.subplot(1, 2, 1)
df.boxplot(column='temperature')
plt.title('Boxplot')
plt.subplot(1, 2, 2)
plt.scatter(df.index, df['temperature'])
plt.title('Scatter Plot')
plt.xlabel('Index')
plt.ylabel('Temperature')
plt.show()
# Moving Average
df['SMA'] = df['temperature'].rolling(window=3).mean()
plt.figure(figsize=(10,5))
df['temperature'].plot(label='Temperature', legend=True)
df['SMA'].plot(label='SMA', legend=True)
plt.show()
Summary / Key Takeaways
In this lesson on identifying outliers and trends, you have learned:
- Outliers are significant deviations from the norm, identified using IQR and Z-scores.
- Box plots and scatter plots are valuable for visualizing outliers in a dataset.
- Moving averages, including SMA and EMA, assist in exposing underlying trends by smoothing data.
- Z-scores serve both as indicators for outlier detection and trend validation metrics.
- Practical application of these methods in Pandas enables enhanced data analysis, supporting accurate data-driven decisions.
