Practical Guide to Principal Component Analysis (PCA) in Data Science

Topic Overview and Actualization

Welcome to our journey into high-dimensional data and the associated challenges that it presents. We'll focus on Principal Component Analysis (PCA), a significant method in the realm of dimensionality reduction. Running through a real-world example, we'll implement PCA in Python. This lesson's roadmap proceeds as follows:

  1. Introducing high-dimensional data and understanding its challenges
  2. Establishing the need for dimensionality reduction
  3. Unveiling PCA, its algorithm, and benefits
  4. Implementing PCA using Python

Understanding High-Dimensional Data

High-dimensional data describes a dataset teeming with numerous features or attributes. One good example of high-dimensional data that would benefit from Principal Component Analysis (PCA) is a dataset from a customer survey.

This dataset may have many different features (dimensions), including age, income, frequency of shopping, amount spent per shopping trip, preferred shopping time, location, and scores on several opinion and satisfaction questions, like product variety, staff helpfulness, store cleanliness, etc.

If these many features all contribute relatively equally to the variance in the dataset or if there exist correlations among these features, it might be challenging to visualize the data or make useful conclusions directly from it. By using PCA, we can reduce the dimensionality of the dataset without significant loss of information and identify the primary areas (principal components) that explain the most variance among customers.

Let's take a look at an example where we wish the model the relationship between height and weight. In our dataset, we have 3 features, but want to reduce the dimensionality to only 2 features.

In our example, we examine a dataset recording individuals' weights and heights in two different units - inches and centimeters. This redundancy heightens the dimensionality of our dataset.

import pandas as pd

data = {
    'Weight (lbs)': [150, 160, 155, 165, 170, 160, 158, 175, 180, 170],
    'Height (inches)': [68, 72, 66, 69, 71, 65, 67, 70, 73, 68],
    'Height (cm)': [172.72, 182.88, 167.64, 175.26, 180.34, 165.1, 170.18, 177.8, 185.42, 172.72]
}
df = pd.DataFrame(data)

We show a scatter plot of height in inches versus centimeters, revealing the redundancy as the data points line up in a straight line.

import matplotlib.pyplot as plt

# Creating 2D scatter plot
plt.scatter(df['Height (inches)'], df['Height (cm)'])

# Setting labels
plt.xlabel('Height (inches)')
plt.ylabel('Height (cm)')
plt.title('Scatter Plot of Heights (inches vs cm)')

# Show plot
plt.grid(True)
plt.show()

image

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal