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.
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.
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%}")
PCA is not available in CoolBox's documented Go bindings yet.
The Go binding coverage list explicitly excludes PCA/SVD/UMAP. Use the Go binding support notes for the current API coverage.
PCA is not available in CoolBox's documented Rust bindings yet.
The Rust crate covers selected Go APIs but excludes Python-only PCA/SVD/UMAP. See the Rust binding support notes .
CoolBox's native C++ dimensionality-reduction module exposes PCA. This example uses its source-level API and expects the dimensionality_reduction and mytrix CMake targets to be built; see the C++ setup guide for the repository build process.
#include <iostream>
#include <vector>
#include "pca.h"
int main() {
using dimensionality_reduction::PCA;
const std::vector<double> values{
1.0, 2.0, 1.5,
1.5, 2.5, 1.0,
2.0, 4.0, 2.5,
3.0, 5.0, 2.0,
5.0, 8.0, 4.0,
6.0, 9.0, 5.5,
};
PCA::Matrix X(values, 6, 3);
PCA pca(2, true, false);
const auto X_2d = pca.fit_transform(X);
const auto variance = pca.get_explained_variance_ratio();
std::cout << "Projected shape: "
<< X_2d.rows() << " x " << X_2d.cols() << '\n';
std::cout << "PC1 variance: " << variance[0] << '\n';
std::cout << "PC2 variance: " << variance[1] << '\n';
}
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:
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 .