In data science, understanding eigenvalues and eigenvectors is essential for various techniques, especially for dimensionality reduction and data transformation. These concepts are central to methods such as Principal Component Analysis (PCA), which is widely used to analyze and visualize high-dimensional data.
In simple terms, eigenvalues and eigenvectors describe how a matrix (which represents a transformation) affects the data. Eigenvectors represent directions in the data space, while eigenvalues determine how much the data is scaled along those directions.
6.1 Eigenvalue
An eigenvalue is a scalar that indicates how much the data is stretched or compressed along a specific direction (represented by an eigenvector) when a transformation is applied. In linear algebra, if is a square matrix and is a non-zero vector, the eigenvalue of matrix is defined by the equation:
This equation tells us that when matrix is applied to vector , the resulting vector is scaled by the factor along the same direction as .
To compute the eigenvalues, we solve the characteristic equation:
Where: - is the matrix (for example, the covariance matrix in PCA). - is the eigenvalue. - is the identity matrix of the same size as . - represents the determinant of the matrix.
The determinant of will be a polynomial in , and the solutions to this polynomial are the eigenvalues. The eigenvalue tells us how much the vector is stretched or compressed. For example:
If , the vector is stretched.
If , the vector is compressed.
If , the vector is collapsed to the origin.
6.2 Eigenvector
An eigenvector is a non-zero vector associated with a given eigenvalue. The eigenvector corresponds to an eigenvalue and satisfies the equation:
This equation tells us that when matrix is applied to eigenvector , the result is simply a scalar multiple of , scaled by the eigenvalue . In other words, the direction of the eigenvector remains unchanged, although its magnitude is scaled by .
6.3 Eigenvalues & Eigenvectors 2D
This document demonstrates the calculation of eigenvalues and eigenvectors for a 2D matrix. The matrix we will use is:
6.3.1 Step 1: Finding Eigenvalues
The eigenvalues are solutions to the characteristic equation:
Substituting :
The determinant is:
Setting this equal to zero:
Thus, the eigenvalues are:
6.3.2 Step 2: Finding Eigenvectors
For each eigenvalue, solve , where .
For
Substituting :
Solving :
This gives , so an eigenvector for is:
For
Substituting :
Solving :
This gives , so an eigenvector for is:
6.3.3 Calculation using Python
This Python code demonstrates how to manually compute eigenvalues and eigenvectors of a 2x2 matrix. The eigenvalues and eigenvectors can be used to understand the matrix’s transformation properties, such as scaling and rotation. The final output will give you both the eigenvalues and eigenvectors that describe how the matrix acts on vectors in its vector space.
import numpy as np# Define the matrix AA = np.array([[3, 1], [0, 2]])# Step 1: Manually compute the characteristic equation# det(A - λI) = (3 - λ)(2 - λ)eigenvalues_manual = [3, 2] # Roots of the characteristic equation# Step 2: Manually compute eigenvectors for each eigenvalue# For λ1 = 3lambda1 = eigenvalues_manual[0]A_minus_lambda1I = A - lambda1 * np.eye(2)print("Matrix (A - λ1 * I):\n", A_minus_lambda1I)
Matrix (A - λ1 * I):
[[ 0. 1.]
[ 0. -1.]]
# Solve (A - λ1 * I) * v = 0v1 = np.array([1, 0]) # Chosen based on row reduction (free variable)# For λ2 = 2lambda2 = eigenvalues_manual[1]A_minus_lambda2I = A - lambda2 * np.eye(2)print("Matrix (A - λ2 * I):\n", A_minus_lambda2I)
Matrix (A - λ2 * I):
[[1. 1.]
[0. 0.]]
# Solve (A - λ2 * I) * v = 0v2 = np.array([-1, 1]) # Chosen based on row reduction (free variable)# Combine eigenvectors into a matrixeigenvectors_manual = np.column_stack((v1, v2))print("Manual Eigenvalues:\n", eigenvalues_manual)
The visualization clearly shows how the matrix transforms the eigenvectors. The blue lines represent the original eigenvectors, and the red dashed lines represent the transformed eigenvectors. This helps in understanding the effect of matrix on these vectors and provides an intuitive grasp of the transformation process.
import osimport sysdef install_package(package):try:__import__(package)print(f"'{package}' is already installed.")exceptImportError:print(f"'{package}' is not installed. Installing now...") os.system(f"{sys.executable} -m pip install {package}")print(f"'{package}' has been successfully installed.")# Example: Install 'plotly' if it is not already installedinstall_package('plotly')
This document demonstrates the calculation of eigenvalues and eigenvectors for a 3D matrix. The matrix we will use is:
6.4.1 Step 1: Finding Eigenvalues
The eigenvalues are solutions to the characteristic equation:
Substitute into the equation:
Now, compute the determinant:
The determinant of the matrix is:
Thus, the characteristic polynomial is:
This gives us the eigenvalues:
6.4.2 Step 2: Finding Eigenvectors
For each eigenvalue, we need to solve for the corresponding eigenvector .
6.4.2.1 For :
Substitute into :
Solve :
This gives the system of equations:
can be any value.
Thus, an eigenvector for is:
6.4.2.2 For :
Substitute into :
Solve :
This gives the system of equations:
Thus, an eigenvector for is:
6.4.2.3 For :
Substitute into :
Solve :
This gives the system of equations:
Thus, an eigenvector for is:
6.4.3 Summary
The eigenvalues and eigenvectors for the matrix are:
, eigenvector:
, eigenvector:
, eigenvector:
These eigenvectors correspond to the directions in 3D space along which the matrix acts by stretching or squishing the space.
6.4.4 Calculation using Python
This Python code demonstrates how to manually compute eigenvalues and eigenvectors of a 3x3 matrix. The eigenvalues and eigenvectors can be used to understand the matrix’s transformation properties, such as scaling and rotation. The final output will give you both the eigenvalues and eigenvectors that describe how the matrix acts on vectors in its vector space.
import numpy as np# Define the matrix A (3x3 matrix)A = np.array([[3, 1, 0], [0, 2, 1], [0, 0, 1]])# Step 1: Manually compute the characteristic equation# The characteristic equation for a 3x3 matrix det(A - λI) = 0.# We will find the eigenvalues by solving the determinant equation manually.eigenvalues_manual = [3, 2, 1] # The eigenvalues can be found by solving the determinant equation# Step 2: Manually compute eigenvectors for each eigenvalue# For λ1 = 3lambda1 = eigenvalues_manual[0]A_minus_lambda1I = A - lambda1 * np.eye(3)print("Matrix (A - λ1 * I):\n", A_minus_lambda1I)
The visualization clearly shows how the matrix transforms the eigenvectors. The blue lines represent the original eigenvectors, and the red dashed lines represent the transformed eigenvectors. This helps in understanding the effect of matrix on these vectors and provides an intuitive grasp of the transformation process.
In data science and machine learning, one common application of eigenvalues and eigenvectors is Principal Component Analysis (PCA). PCA is a dimensionality reduction technique used to simplify complex datasets by transforming the data into a new set of variables, called principal components, which are linear combinations of the original variables.
In this case study, we will use the Iris dataset, which contains measurements of sepal length, sepal width, petal length, and petal width for different species of iris flowers. PCA will help reduce the number of dimensions while retaining the most important information about the data. You can get the csv version of this dataset from here.
It has 4 features, Sepal Length, Sepal Width, Petal Length, Petal Width all given in centimeters. In total it has 150 rows of data comprising of 3 species with 50 row for each species. Then a column with its species is also given.
6.5.1 Problem Statement
The goal is to apply PCA to the Iris dataset, which consists of 150 samples with 4 features each. We will:
Apply PCA to reduce the dimensionality of the dataset from 4 dimensions to 2 dimensions.
Calculate the eigenvalues and eigenvectors of the covariance matrix of the dataset.
Use the eigenvalues to determine how much variance is explained by each principal component.
Visualize the data and the transformed principal components.
6.5.2 Dataset
The Iris dataset includes the following columns:
Sepal Length
Sepal Width
Petal Length
Petal Width
Species
5.1
3.5
1.4
0.2
Setosa
4.9
3.0
1.4
0.2
Setosa
4.7
3.2
1.3
0.2
Setosa
…
…
…
…
…
6.7
3.0
5.2
2.3
Virginica
6.3
2.5
5.0
1.9
Virginica
6.5
3.0
5.5
2.1
Virginica
6.5.3 Step 1: Data Preparation
First, we will load the Iris dataset and normalize it so that each feature has a mean of 0 and a standard deviation of 1. This normalization step is necessary to ensure that all features contribute equally to the PCA process.
# Load necessary librarieslibrary(tidyverse)library(caret)library(ggplot2)library(knitr)# Load the Iris datasetdata(iris)# Step 1: Normalize the dataset (excluding the Species column)iris_data <- iris[, 1:4]iris_data_scaled <-scale(iris_data)# Check the first few rows of the scaled datakable(head(iris_data_scaled))
Sepal.Length
Sepal.Width
Petal.Length
Petal.Width
-0.8976739
1.0156020
-1.335752
-1.311052
-1.1392005
-0.1315388
-1.335752
-1.311052
-1.3807271
0.3273175
-1.392399
-1.311052
-1.5014904
0.0978893
-1.279104
-1.311052
-1.0184372
1.2450302
-1.335752
-1.311052
-0.5353840
1.9333146
-1.165809
-1.048667
6.5.4 Step 2: Compute the Covariance Matrix
Next, we will compute the covariance matrix of the normalized dataset. The covariance matrix helps us understand the relationships between the features.
# Step 2: Compute the covariance matrixcov_matrix <-cov(iris_data_scaled)# Print the covariance matrixcov_matrix
6.5.5 Step 3: Calculate Eigenvalues and Eigenvectors
We will calculate the eigenvalues and eigenvectors of the covariance matrix. The eigenvalues represent the amount of variance explained by each principal component, and the eigenvectors represent the directions of maximum variance.
# Step 3: Calculate the eigenvalues and eigenvectorseigen_decomp <-eigen(cov_matrix)# Eigenvalueseigenvalues <- eigen_decomp$valuesprint("Eigenvalues:")
Using the eigenvectors, we will transform the original data into a new space defined by the principal components.
# Step 4: Transform the data into the new space# We use the eigenvectors to project the data onto the principal componentspca_data <- iris_data_scaled %*% eigenvectors# Step 5: Create a DataFrame with the transformed data (first two principal components)pca_df <-as.data.frame(pca_data[, 1:2]) # Select first two principal componentscolnames(pca_df) <-c("PC1", "PC2")pca_df$Species <- iris$Species# Check the transformed datahead(pca_df)
We will use ggplot2 to create a scatter plot of the transformed data in the 2D space defined by the first two principal components.
# Load necessary librarieslibrary(plotly)library(datasets)# Load the Iris datasetdata(iris)# Perform PCA on the Iris dataset (using prcomp)pca_result <-prcomp(iris[, 1:4], center =TRUE, scale. =TRUE)# Create a data frame for the PCA resultspca_df <-as.data.frame(pca_result$x)# Add the Species column to the PCA resultspca_df$Species <- iris$Species# Create a 3D scatter plot using plotlyfig <-plot_ly(data = pca_df, x =~PC1, y =~PC2, z =~PC3, color =~Species, colors =c('red', 'green', 'blue'),type ='scatter3d', mode ='markers',marker =list(size =5)) %>%layout(title ="3D PCA of Iris Dataset",scene =list(xaxis =list(title ='Principal Component 1'),yaxis =list(title ='Principal Component 2'),zaxis =list(title ='Principal Component 3') ))# Show the plotfig