Principal Component Analysis

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. Using a real-world example, we'll implement PCA in R. 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 R

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, such as product variety, staff helpfulness, and store cleanliness.

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 draw 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 to model the relationship between height and weight. In our dataset, we have three features but want to reduce the dimensionality to only two features.

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

data <- data.frame(
  weight_lbs = c(150, 160, 155, 165, 170, 160, 158, 175, 180, 170),
  height = c(68, 72, 66, 69, 71, 65, 67, 70, 73, 68),
  height_cm = c(172.72, 182.88, 167.64, 175.26, 180.34, 165.1, 170.18, 177.8, 185.42, 172.72)
)

Visualizing Feature Redundancy

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

library(ggplot2)

ggplot(data, aes(x = height, y = height_cm)) +
  geom_point() +
  labs(
    x = "Height (inches)",
    y = "Height (cm)",
    title = "Scatter Plot of Heights (inches vs cm)"
  ) +
  theme_bw() +
  theme(panel.grid = element_line())

Plotted 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