A practical PCA (Principal Component Analysis) notebook that explores dimensionality reduction on two image datasets:
- scikit-learn Digits (built-in; small grayscale digit images)
- An Arabic letters image dataset (grayscale images stored as flattened pixel vectors in CSV files)
The notebook focuses on understanding:
- Explained variance and cumulative variance
- Choosing the number of principal components (k)
- Image reconstruction quality after compression
- The impact of PCA on classification performance (e.g., Logistic Regression / KNN)
Digit reconstruction as k grows · Classifier accuracy vs. k on Arabic letters
- Loads Digits using
sklearn.datasets.load_digits - Loads Arabic letters images from CSV files (image vectors + labels)
Typically, each image is stored as a 1D vector (flattened pixels).
Example: a 32×32 image → 1024 features.
The notebook fits PCA and analyzes:
explained_variance_ratio_- cumulative explained variance
- scree/cumulative plots to determine how many components are needed to retain a desired variance level.
The notebook reconstructs images using different values of k to visually compare:
- original image vs reconstructed image
- how compression affects clarity and details
This builds intuition about the trade-off between: compression ↔ information retention
The notebook tests how PCA affects classification by training models on PCA-reduced features and evaluating performance. Common experiments include:
- varying
k(number of components) - varying variance thresholds (e.g., 90%, 95%, 98%)
- comparing performance before/after PCA
Models may include:
- Logistic Regression
- K-Nearest Neighbors (KNN)
- Understand PCA beyond theory through real image experiments
- Interpret explained variance and choose
ksystematically - Observe reconstruction quality changes as
kchanges - Measure how dimensionality reduction affects classification accuracy
- Learn why splitting strategies (e.g., stratification) matter for fair evaluation
├── PCA_expirements.ipynb
├── (Arabic letters CSV files) # optional (if available)
├── assets/
│ └── preview.png # optional screenshot
└── README.mdpip install numpy pandas matplotlib scikit-learnjupyter notebookOpen:
PCA_expirements.ipynb
If you are using Google Colab, upload the CSV files to
/content/(or update the file paths in the notebook).
The Arabic letters dataset files may be too large to include directly in the repository (or may fail to upload due to size limits). If you want the exact dataset used in this notebook, feel free to contact me on LinkedIn and I can share it.
- Add
StandardScalercomparison (scaled vs unscaled PCA pipelines) - Try additional models (SVM, RandomForest, MLP)
- Benchmark runtime vs number of components
- Add PCA-based 2D visualization (and compare with t-SNE / UMAP)
- Automate best-
kselection using validation accuracy
- scikit-learn PCA Docs: https://scikit-learn.org/stable/modules/decomposition.html#pca
- Digits Dataset (sklearn): https://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_digits.html
Mohammed Alawfi 📍 Madinah, Saudi Arabia
This project is licensed under the MIT License. See LICENSE for details.

