← All notes

Statistical note

Coefficient of determination R²

Coefficient of determination R²: theory, interpretation, Python computation, and a scientific-imaging case study.

6 min read

A regression metric defines how residual magnitude, sign, scale, and outcome distribution contribute to model quality. The metric should match the scientific cost of errors.

Concept figure for Coefficient of determination R²

Mathematical definition

The central quantity is

R2=1SSE/SST.R2=1-SSE/SST.

This expression states what is being counted, averaged, ranked, or compared. Its scale and direction must be interpreted in the context of the data and sampling design.

Compute it in Python

import numpy as np
from sklearn import metrics

y_true = np.array([1.0, 2.5, 4.0, 7.5, 10.0, 14.0])
y_pred = np.array([1.2, 2.2, 4.8, 7.0, 9.1, 13.2])
print(metrics.r2_score(y_true, y_pred))

The function is sklearn.metrics.r2_score. Inspect its current signature and return object in the official documentation for the version installed in your environment.

Interpretation and cautions

Always examine residual plots and stratified errors. Scale-dependent metrics cannot be compared across outcomes without context; percentage metrics fail near zero; R-squared can be negative on unseen data.

A numerical value is not self-interpreting. Compare it with a baseline, uncertainty interval, operational threshold, or competing model, and retain the underlying observations or confusion counts.

Scientific-imaging case study

For squared-error improvement over the mean baseline in density-map or QPI prediction, report the score across independent acquisitions and check bias across the target range.

Split train, validation, and test data at the specimen or experimental level. Report variability across independent repeats so that the metric describes generalization rather than leakage.