# PCA (Principal Component Analysis) in Python - ML From Scratch 11

In this Machine Learning from Scratch Tutorial, we are going to implement a PCA algorithm using only built-in Python modules and numpy. We will also learn about the concept and the math behind this popular ML algorithm.

All algorithms from this course can be found on GitHub together with example tests.

## Implementation

```
import numpy as np
class PCA:
def __init__(self, n_components):
self.n_components = n_components
self.components = None
self.mean = None
def fit(self, X):
# Mean centering
self.mean = np.mean(X, axis=0)
X = X - self.mean
# covariance, function needs samples as columns
cov = np.cov(X.T)
# eigenvalues, eigenvectors
eigenvalues, eigenvectors = np.linalg.eig(cov)
# -> eigenvector v = [:,i] column vector, transpose for easier calculations
# sort eigenvectors
eigenvectors = eigenvectors.T
idxs = np.argsort(eigenvalues)[::-1]
eigenvalues = eigenvalues[idxs]
eigenvectors = eigenvectors[idxs]
# store first n eigenvectors
self.components = eigenvectors[0:self.n_components]
def transform(self, X):
# project data
X = X - self.mean
return np.dot(X, self.components.T)
```

**Join My Newsletter! **Get Python and ML tips emailed directly to your inbox. Each month you’ll get a summary of all the content I created, including the newest videos, articles, promotions, tips, and more.

# TensorFlow 2 Beginner

Learn all the necessary basics to get started with TensorFlow 2 and Keras.

# PyTorch Beginner

Learn all the necessary basics to get started with this deep learning framework.

# ML From Scratch

Implement popular Machine Learning algorithms from scratch using only built-in Python modules and numpy.