← All tutorials

05 / VISUALIZATION

See the data
in two dimensions.

Use CoolBox's Python or C++ PCA implementation to reduce five input features to two components, then render the scatter plot with its built-in graphics module. The Go and Rust examples demonstrate plotting precomputed coordinates; their current bindings do not include a PCA estimator.

10 MINCOOLBOX NUMERIC + GRAPHICSINTERMEDIATE

Use CoolBox numeric and graphics modules

Install CoolBox using the Python setup tutorial. The native graphics API renders charts without a separate plotting library. When installed with pip, the Python package installs its declared NumPy dependency automatically.

1. Create labeled groups

Create a six-row, five-feature matrix in each language. Labels are used only to color the chart. For PCA, the Python binding accepts a NumPy-compatible array while the native C++ API accepts its matrix type; Go and Rust bindings expose matrix operations but not PCA.

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

dr = ml.ml_core.dimensionality_reduction
graphics = ml.ml_core.graphics

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],
])
labels = [0, 0, 1, 1, 2, 2]

2. Project with CoolBox PCA

CoolBox provides PCA through Python and its C++ core. Go and Rust have matrix operations, but do not currently expose a PCA estimator on main; their tabs explain the limitation and link to the binding notes.

COOLBOX PCA
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()

3. Draw and save the graph

Plot each group as a separate labeled series and save the PNG with CoolBox's built-in chart renderer. No external plotting library is needed.

Choose a language to see its charting code. Go and Rust can draw the chart, but currently need PCA coordinates computed by another tool.

COOLBOX GRAPHICS
graph = graphics.Graph(800, 500, graphics.GraphType.SCATTER)
graph.set_title("CoolBox PCA projection")
graph.set_x_label(f"PC1 ({variance[0]:.1%} variance)")
graph.set_y_label(f"PC2 ({variance[1]:.1%} variance)")

colors = [graphics.BLUE, graphics.GREEN, graphics.ORANGE]
for group_id, color in enumerate(colors):
    points = [index for index, label in enumerate(labels) if label == group_id]
    graph.add_series(graphics.DataSeries(
        f"Group {group_id + 1}",
        [X_2d[index, 0] for index in points],
        [X_2d[index, 1] for index in points],
        color,
    ))

canvas = graph.render()
if not canvas.save_png("pca_2d_visualization.png"):
    raise RuntimeError("CoolBox could not save the PCA chart")
print("Saved pca_2d_visualization.png")
Example CoolBox PCA scatter plot showing two samples each in three labeled groups, plotted by PC1 and PC2.
Example output preview. Run the Python or C++ code above to generate the PCA chart from the sample dataset.

Read the plot with care

Nearby points have similar coordinates in the projected view, but a 2D projection can hide structure from the other features. Look at the explained-variance values and compare alternative scaling choices before drawing conclusions.

This walkthrough uses CoolBox’s native PCA, matrix, and graphics APIs where the language binding provides them. See the repository’s PCA visualization example for additional plotting workflows.