← All tutorials

03 / DIMENSIONALITY REDUCTION

A first look at PCA.

Principal Component Analysis (PCA) projects data onto directions that capture its variance. Reduce a small five-feature dataset to two components and inspect how much information those components retain.

10 MINPYTHON + C++ COREINTERMEDIATE

What PCA gives you

Each principal component is a direction through the feature space. The first captures the most variance; later components capture what remains. A two-component projection makes high-dimensional data easier to inspect, but it may not preserve every detail.

1. Create a small dataset

Install CoolBox using the Python setup guide. This example stores the dataset in CoolBox's native DenseMatrix. The Python PCA binding accepts a NumPy-compatible array, so we convert the matrix using NumPy, which pip installs as a CoolBox dependency.

PYTHON · COOLBOX DENSE MATRIX
import numpy as np
import ml_toolbox as ml
from ml_toolbox import matrix

dr = ml.ml_core.dimensionality_reduction
X = matrix.DenseMatrix.from_rows([
    [0.2, 0.1, 0.4, 0.0, 0.3],
    [0.0, 0.3, 0.1, 0.2, 0.4],
    [1.1, 1.0, 1.2, 0.8, 1.1],
    [0.9, 1.2, 1.0, 1.1, 0.8],
    [2.0, 2.1, 1.8, 2.2, 1.9],
    [2.2, 1.9, 2.1, 1.8, 2.2],
])

print("Input shape:", X.shape)

2. Fit PCA and transform

Choose a language to fit the model and project the rows onto two components. The Python binding receives a NumPy-compatible array; PCA and the projection are still computed by CoolBox. Here we center the input but do not standardize it.

PYTHON · COOLBOX BINDING
X_for_pca = np.asarray(X.data, dtype=float).reshape(X.shape)
pca = dr.PCA(n_components=2, center=True, scale=False)
X_2d = pca.fit_transform(X_for_pca)

variance = pca.get_explained_variance_ratio()
print("Projected rows:", len(X_2d))
print(f"PC1 variance: {variance[0]:.1%}")
print(f"PC2 variance: {variance[1]:.1%}")
print(f"Total retained: {sum(variance):.1%}")

The result has two columns: one coordinate per principal component. The explained-variance ratios help quantify what the 2D projection retains.

3. Think about scaling

PCA is sensitive to feature scales. If one feature ranges in thousands while another ranges in fractions, the larger-scale feature can dominate. CoolBox lets you request scaling with scale=True using its built-in numeric routines:

PYTHON
scaled_pca = dr.PCA(n_components=2, center=True, scale=True)
X_scaled_2d = scaled_pca.fit_transform(X)
scaled_variance = scaled_pca.get_explained_variance_ratio()

Choose scaling based on the units and meaning of your data, not just the resulting plot. Compare the explained variance and the projection with and without scaling.

Keep exploring

See the graphing tutorial to visualize the transformed data, or open the repository’s dimensionality-reduction example .