A complete from-scratch implementation of Principal Component Analysis (PCA)
(Analyse en Composantes Principales – ACP) using NumPy and Matplotlib.
This project follows the classical PCA pipeline and focuses on understanding both:
- The mathematical foundations of PCA
- The practical implementation step by step
The algorithm is applied to a small Iris flowers dataset containing 15 individuals and 3 variables.
Principal Component Analysis (PCA) is a statistical technique used to analyze datasets with multiple variables and reduce their dimensionality while preserving as much information as possible.
When the number of variables becomes large, visualizing relationships between variables becomes difficult. PCA solves this problem by projecting the data into a lower-dimensional space formed by new axes called principal components.
These axes are chosen so that:
- They maximize variance
- They minimize information loss
- They allow visualization in 2D or 3D
The dataset contains:
- 15 Iris flowers
- 3 variables:
x1 : Sepal length
x2 : Sepal width
x3 : Petal length
Matrix size:
15 × 3
- Implement PCA manually using Python
- Display intermediate matrices
- Compute eigenvalues and eigenvectors
- Calculate explained variance
- Project data onto principal components
- Visualize individuals and variables
For each variable j, compute its mean:
μ_j = (1 / n) * Σ x_ij
Meaning:
Add all values of variable j, then divide by the number of samples n.
Subtract the mean from each value:
Xc(i, j) = X(i, j) − μ_j
Meaning:
Each column will have an average equal to zero.
Divide each centered value by its standard deviation:
Y(i, j) = Xc(i, j) / σ_j
Meaning:
All variables will have the same scale.
Z = Y / √n
Meaning:
Divide the standardized matrix by the square root of the number of samples.
R = corr(Z)
Meaning:
R measures how strongly variables are related to each other.
Properties:
- Diagonal values = 1
- Off-diagonal values = correlation between variables
Solve:
R · v = λ · v
Where:
- v : eigenvector (direction of a principal component)
- λ : eigenvalue (amount of variance)
ExplainedVariance_k = ( λ_k / Σ λ ) × 100
Meaning:
Percentage of total information carried by component k.
Scores = Z × V
Meaning:
Transform data into the new PCA coordinate system.
PCA-From-Scratch/
├── ACPtesting.py
├── images/
│ ├── Figure_1.png
│ ├── Figure_2.png
│ ├── Figure_3.png
│ ├── Figure_4.png
│ ├── Figure_5.png
│ ├── Figure_6.png
│ └── Figure_7.png
└── README.md
pip install numpy matplotlib
python ACPtesting.py
- PC1 captures the maximum variance
- PC2 captures the second highest variance
- Using PC1 and PC2 preserves most information
- Variables pointing in the same direction are positively correlated
- Variables pointing in opposite directions are negatively correlated
Hachim Fernane
Master Student – Computer Science
University of Guelma
FEEL FREE TO USE JUST PRAY FOR ME






