Sub topic 1
Why Reduce Dimensionality?
Prerequisites
This chapter assumes that you know basic Python and NumPy, understand means and variances, and can read a feature matrix where rows are samples and columns are features.
Why Reduce Dimensionality
A dataset may contain hundreds of features, but many can be redundant. For example, height in centimeters and height in inches carry nearly the same information. Redundant features increase storage and computation costs and can make models harder to interpret.
Dimensionality reduction replaces many features with fewer features while preserving useful structure. It can reduce noise, make visualization possible, and speed up later algorithms. PCA, or principal component analysis, is a linear method for doing this.
Variance, Covariance and the Covariance Matrix
Variance measures how much one feature changes around its mean. Covariance measures whether two features change together:
- Positive covariance means that large values of one feature tend to occur with large values of the other.
- Negative covariance means that one tends to be large when the other is small.
- Covariance near zero means little linear relationship.
For a dataset with several features, these values form a covariance matrix. Its diagonal contains each feature's variance. Its off-diagonal entries contain pairwise covariances. PCA uses this matrix to find directions where the data varies most.
Centering and Standardizing Your Features
Centering subtracts each feature's mean, so every feature is measured around zero. PCA should normally use centered data.
Standardizing goes further: after centering, divide each feature by its standard deviation. This gives every feature variance one. Standardize when features use different units or scales. If the original scale has meaningful importance, centering alone may be appropriate.
Eigenvectors and Eigenvalues in Brief
An eigenvector of the covariance matrix is a direction that the matrix leaves pointing in the same direction. Its eigenvalue tells how much variance lies along that direction. PCA sorts these eigenvectors by decreasing eigenvalue.
Principal Components: Directions of Maximum Variance
The first principal component is the direction with the greatest variance. The second is perpendicular to the first and captures the greatest remaining variance. Later components follow the same rule.
Each component is a weighted combination of the original features. Projecting a sample onto these directions produces component scores. Keeping only the first few scores creates a lower-dimensional representation.
Exercise
Why might standardizing features change the principal components?
Show answer
Features with larger units or variances would otherwise dominate the covariance matrix. Standardizing gives each feature equal starting scale.