Skip to content

Understanding Data with Visualization

ML - Understanding Data with Visualization

Section titled “ML - Understanding Data with Visualization”

In the previous chapter, we discussed the importance of understanding data through statistical analysis for Machine Learning algorithms. Another powerful approach is Data Visualization, which helps us gain intuitive insights into our data’s structure and relationships.

Data visualization allows us to ‘see’ the data, understand distributions, identify correlations between features, and quickly assess potential relationships between input features and the target variable. Effective visualization is crucial for exploratory data analysis (EDA). We’ll explore several Python techniques for visualizing ML data.

Univariate Plots: Understanding Features Independently

Section titled “Univariate Plots: Understanding Features Independently”

The simplest form of visualization focuses on single variables, known as univariate visualization. This helps us understand the characteristics and distribution of each feature in our dataset independently. Key techniques include:

Histograms are excellent for quickly understanding the distribution of a numerical feature. They group data into bins and display the frequency of observations within each bin.

  • Shows the count of observations in predefined bins.
  • Helps visualize the underlying frequency distribution (e.g., Gaussian, skewed, uniform, exponential).
  • Can help identify potential outliers or unusual concentrations of data.

This Python script demonstrates creating histograms for each feature in the Pima Indians Diabetes dataset using Pandas and Matplotlib. We also introduce Seaborn, a library built on Matplotlib that provides enhanced visualizations.

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# Define the path to your dataset
# It's best practice to use relative paths or environment variables
# Example placeholder:
# path = 'path/to/your/pima-indians-diabetes.csv'
path = 'pima-indians-diabetes.csv' # Replace with your actual path
names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
data = pd.read_csv(path, names=names)
# Using Pandas built-in histogram function
print("Generating histograms using Pandas...")
data.hist(figsize=(10, 8))
plt.tight_layout() # Adjust layout to prevent overlapping titles
plt.show()
# Example using Seaborn for potentially better aesthetics (optional)
print("\nGenerating histogram for 'age' using Seaborn...")
sns.histplot(data=data, x='age', kde=True) # kde adds a density curve
plt.title('Distribution of Age')
plt.show()

Executing the script will display histograms for each numerical feature in the dataset. Observe the shapes: ‘age’, ‘pedi’, and ‘test’ might show right-skewed (exponential-like) distributions, while ‘mass’ and ‘plas’ might appear closer to a normal (Gaussian) distribution. ‘pres’ might have a peak around a specific value. This initial look guides further analysis and feature engineering.

Density Plots (Kernel Density Estimate - KDE)

Section titled “Density Plots (Kernel Density Estimate - KDE)”

Density plots provide a smoothed representation of a feature’s distribution. They are like smoothed histograms, generated using a kernel density estimate, showing the probability density function.

This script generates density plots for the Pima dataset features using Pandas plotting.

import pandas as pd
import matplotlib.pyplot as plt
# Define the path to your dataset
path = 'pima-indians-diabetes.csv' # Replace with your actual path
names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
data = pd.read_csv(path, names=names)
# Generate density plots for each feature
# `subplots=True` creates separate plots for each column
# `layout` arranges the subplots in a grid
# `sharex=False` allows each plot to have its own x-axis scale
print("Generating density plots using Pandas...")
data.plot(kind='density', subplots=True, layout=(3, 3), sharex=False, figsize=(10, 8))
plt.tight_layout()
plt.show()

The output displays smooth curves representing the distribution of each feature. Comparing these to the histograms provides a complementary view of the data’s shape, potentially highlighting skewness or multimodality more clearly than histograms alone.

Box plots (or box-and-whisker plots) effectively summarize the distribution of a numerical feature through five key statistics: minimum, first quartile (25th percentile), median (50th percentile), third quartile (75th percentile), and maximum. They are particularly useful for identifying skewness and potential outliers.

  • Summarizes distribution: median (line inside the box), interquartile range (IQR, the box itself), and range (whiskers).
  • The box represents the middle 50% of the data (IQR = Q3 - Q1).
  • Whiskers typically extend to 1.5 times the IQR from the box edges, or to the data extremes if they fall within this range.
  • Points beyond the whiskers are often flagged as potential outliers.

This script generates box plots for the Pima dataset features.

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# Define the path to your dataset
path = 'pima-indians-diabetes.csv' # Replace with your actual path
names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
data = pd.read_csv(path, names=names)
# Using Pandas built-in boxplot function
print("Generating box plots using Pandas...")
data.plot(kind='box', subplots=True, layout=(3, 3), sharex=False, sharey=False, figsize=(10, 8))
plt.tight_layout()
plt.show()
# Example using Seaborn for a potentially cleaner look (plotting all on one axis)
print("\nGenerating box plots using Seaborn...")
plt.figure(figsize=(12, 6))
sns.boxplot(data=data.drop('class', axis=1)) # Exclude the class label for this view
plt.title('Box Plot of Features (excluding class)')
plt.xticks(rotation=45)
plt.show()

The box plots reveal the spread and central tendency of each feature. Observe the median line’s position within the box (indicating symmetry or skewness) and the presence of points outside the whiskers (potential outliers). For example, ‘test’, ‘pedi’, ‘skin’, and ‘age’ might show several potential outliers and skewness.

Multivariate Plots: Interaction Among Multiple Variables

Section titled “Multivariate Plots: Interaction Among Multiple Variables”

Multivariate visualization helps understand the relationships between two or more features. This is crucial for feature selection and understanding complex interactions.

Correlation measures the linear relationship between two variables (e.g., using Pearson’s correlation coefficient). A correlation matrix visualizes these pairwise correlations for all numerical features in the dataset, typically using a heatmap.

This script calculates the correlation matrix for the Pima dataset and plots it as a heatmap using Matplotlib and Seaborn.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# Define the path to your dataset
path = 'pima-indians-diabetes.csv' # Replace with your actual path
names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
data = pd.read_csv(path, names=names)
# Calculate the correlation matrix
correlations = data.corr()
# Plotting the heatmap using Seaborn
print("Generating correlation matrix heatmap...")
plt.figure(figsize=(10, 8))
sns.heatmap(correlations, annot=True, cmap='coolwarm', fmt='.2f')
# annot=True displays the correlation values
# cmap sets the color scheme
# fmt='.2f' formats the numbers to two decimal places
plt.title('Correlation Matrix of Pima Diabetes Features')
plt.show()
# Optional: Original Matplotlib matshow approach (less informative visually)
# print("\nGenerating correlation matrix using Matplotlib matshow (alternative)...")
# fig = plt.figure(figsize=(10, 8))
# ax = fig.add_subplot(111)
# cax = ax.matshow(correlations, vmin=-1, vmax=1, cmap='coolwarm')
# fig.colorbar(cax)
# ticks = np.arange(0, len(names), 1)
# ax.set_xticks(ticks)
# ax.set_yticks(ticks)
# ax.set_xticklabels(names, rotation=45, ha='left')
# ax.set_yticklabels(names)
# plt.title('Correlation Matrix (matshow)')
# plt.tight_layout()
# plt.show()

The heatmap displays correlation coefficients between all pairs of features. Colors indicate the strength and direction: warmer colors (e.g., red) usually represent positive correlations, while cooler colors (e.g., blue) represent negative correlations. Values close to +1 or -1 indicate strong linear relationships, while values near 0 indicate weak linear relationships. Look for strong correlations between input features (potential multicollinearity) and between features and the target variable (‘class’).

A scatter plot matrix (or pair plot) displays pairwise scatter plots for all features in the dataset. It’s excellent for visualizing relationships between pairs of variables and identifying potential patterns or clusters.

This script generates a scatter plot matrix for the Pima dataset using Pandas plotting and Seaborn (recommended).

import pandas as pd
import matplotlib.pyplot as plt
from pandas.plotting import scatter_matrix # Updated import
import seaborn as sns
# Define the path to your dataset
path = 'pima-indians-diabetes.csv' # Replace with your actual path
names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
data = pd.read_csv(path, names=names)
# Using Pandas scatter_matrix (older style)
# print("Generating scatter matrix using Pandas...")
# scatter_matrix(data, figsize=(12, 12))
# plt.suptitle('Scatter Plot Matrix (Pandas)', y=1.02) # Adjust title position
# plt.tight_layout()
# plt.show()
# Using Seaborn pairplot (modern and more informative)
print("\nGenerating pair plot using Seaborn...")
sns.pairplot(data, hue='class') # Color points by the 'class' label
plt.suptitle('Pair Plot of Pima Diabetes Features (colored by class)', y=1.02)
plt.show()

The output shows a grid of plots. The diagonal typically contains histograms or density plots for each feature (as seen in univariate analysis). The off-diagonal plots are scatter plots showing the relationship between pairs of features. Using hue='class' in Seaborn colors the points based on the target variable, helping to see if features separate the classes well. Look for linear relationships, clusters, or other patterns in the scatter plots.

Further Resources: