DEV Ex 5
DEV Ex 5
Aim:
To perform Time Series Analysis and apply the various visualization
techniques.
Code:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from statsmodels.tsa.seasonal import seasonal_decompose
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
import numpy as np
# Plotting the temperature as an example (you can replace with any other column)
plt.figure(figsize=(10, 6))
plt.plot(data['meantemp'], label='Mean Temperature')
plt.title('Line Plot of Mean Temperature in Delhi')
plt.xlabel('Date')
plt.ylabel('Temperature (°C)')
plt.legend()
plt.show()
2. Decomposition (Trend, Seasonality, and Residuals)
result = seasonal_decompose(data['meantemp'], model='additive', period=30)
result.plot()
plt.show()
3. Moving Average Smoothing
result = seasonal_decompose(data['meantemp'], model='additive', period=30)
result.plot()
plt.show()
4. Autocorrelation and Partial Autocorrelation Plots (ACF & PACF)
plt.figure(figsize=(12, 6))
plot_acf(data['meantemp'].dropna(), lags=40)
plt.show()
plt.figure(figsize=(12, 6))
plot_pacf(data['meantemp'].dropna(), lags=40)
plt.show()
5. Heatmap for Seasonality
data['Month'] = data.index.month
data['Year'] = data.index.year
heatmap_data = data.pivot_table(index='Month', columns='Year',
values='meantemp', aggfunc='mean')
plt.figure(figsize=(10, 6))
sns.heatmap(heatmap_data, cmap="YlGnBu", annot=True)
plt.title('Heatmap Showing Monthly Mean Temperature Across Years')
plt.show()
6. Lag Plot
lag_plot(data['meantemp'])
plt.title('Lag Plot of Mean Temperature')
plt.show()
7. Fourier Transform for Frequency Domain Analysis
fft_values = np.fft.fft(data['meantemp'].dropna())
fft_freq = np.fft.fftfreq(len(data['meantemp'].dropna()))
plt.figure(figsize=(10, 6))
plt.plot(fft_freq, np.abs(fft_values))
plt.title('Fourier Transform - Frequency Spectrum of Mean Temperature')
plt.xlabel('Frequency')
plt.ylabel('Amplitude')
plt.show()
8. Box Plot for Seasonality Trends
plt.figure(figsize=(10, 6))
sns.boxplot(x='Month', y='meantemp', data=data)
plt.title('Box Plot of Monthly Mean Temperature')
plt.show()
Result:
The above program has been run successfully.