ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing
ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing

All Courses

Fundamentals of Model Evaluation and Metrics
Chapter 1: Introduction to Model Evaluation
What is a Machine Learning Model?
Why Evaluating Models Matters
The Goal of Evaluation Metrics
Types of Learning Problems: Classification
Types of Learning Problems: Regression
Overview of the Evaluation Process
Quiz for Chapter 1
Chapter 2: Metrics for Classification
Understanding Classification Predictions
Accuracy: A Simple First Metric
When Accuracy Can Be Misleading
True Positives, False Positives, True Negatives, False Negatives
The Confusion Matrix Explained
Precision: Measuring Exactness
Recall (Sensitivity): Measuring Completeness
Precision vs. Recall Trade-off
F1-Score: Combining Precision and Recall
Practice: Calculating Classification Metrics
Quiz for Chapter 2
Chapter 3: Metrics for Regression
Understanding Regression Predictions
Calculating Prediction Errors
Mean Absolute Error (MAE)
Mean Squared Error (MSE)
Root Mean Squared Error (RMSE)
Comparing MAE, MSE, and RMSE
Coefficient of Determination (R-squared)
Interpreting R-squared Values
Limitations of R-squared
Practice: Calculating Regression Metrics
Quiz for Chapter 3
Chapter 4: Preparing Data for Evaluation
Why Evaluate on Unseen Data?
The Training Set: Learning Patterns
The Test Set: Assessing Performance
Train-Test Split Procedure
Common Split Ratios
Randomness in Splitting
Potential Issues with a Single Split
Introduction to Cross-Validation Concept
Hands-on Practical: Splitting Data
Quiz for Chapter 4
Chapter 5: Basic Evaluation Workflow
Steps in a Standard Evaluation
Choosing Metrics for Your Problem
Performing the Train-Test Split
Training a Simple Model
Generating Predictions on the Test Set
Calculating Performance Metrics
Interpreting the Results
Simple Evaluation Workflow Example
Common Mistakes in Basic Evaluation
Quiz for Chapter 5

Coefficient of Determination (R-squared)

The Coefficient of Determination, often referred to as R-squared, helps assess the performance of regression models. While other measures, such as Mean Absolute Error (MAE), Mean Squared Error (MSE), and Root Mean Squared Error (RMSE), quantify the average size of prediction errors, R-squared offers a different perspective by showing how well a model accounts for the inherent variability within the data. This difference matters because the absolute error magnitude alone can be misleading; for example, an RMSE of 10 could be excellent for predicting house prices ranging in the millions, but very poor for predicting temperatures between 0 and 40 degrees.

This is where the Coefficient of Determination, commonly known as R-squared (R2R^2R2), comes in. Instead of focusing solely on the error size, R-squared provides a measure of how much of the variability in the target variable (the value you are trying to predict) is explained by your model. It essentially compares your model's performance to a very simple baseline model.

The Baseline: Predicting the Mean

Imagine the simplest possible "model" for predicting a continuous value: always predicting the average (mean) of all the actual target values in your dataset. This baseline model doesn't use any input features; it just makes the same guess every time. For example, if you're predicting house prices and the average price in your dataset is 300,000,thisbaselinemodelwouldpredict300,000, this baseline model would predict 300,000,thisbaselinemodelwouldpredict300,000 for every house, regardless of its size, location, or condition.

R-squared tells you how much better your actual regression model performs compared to this naive mean-prediction baseline.

Calculating R-squared

To understand how R-squared is calculated, we need two components:

  1. Total Sum of Squares (SST): This measures the total variance in the actual target values (yiy_iyi​). It's calculated by summing the squared differences between each actual value (yiy_iyi​) and the overall mean of the actual values (yˉ\bar{y}yˉ​). It represents the variability inherent in the data if you were just using the mean as your prediction.

    SST=∑i=1n(yi−yˉ)2SST = \sum_{i=1}^{n} (y_i - \bar{y})^2SST=i=1∑n​(yi​−yˉ​)2

    Here, nnn is the number of data points, yiy_iyi​ is the actual value for the iii-th data point, and yˉ\bar{y}yˉ​ is the mean of all actual values.

  2. Residual Sum of Squares (SSR) or Sum of Squared Errors (SSE): This measures the variance that your model cannot explain. It's calculated by summing the squared differences between each actual value (yiy_iyi​) and its corresponding predicted value (y^i\hat{y}_iy^​i​) generated by your model. This is the same sum of squared errors used in calculating MSE, just without dividing by nnn.

    SSR=∑i=1n(yi−y^i)2SSR = \sum_{i=1}^{n} (y_i - \hat{y}_i)^2SSR=i=1∑n​(yi​−y^​i​)2

    Here, y^i\hat{y}_iy^​i​ is the value predicted by your model for the iii-th data point.

Now, the R-squared formula combines these two:

R2=1−SSRSSTR^2 = 1 - \frac{SSR}{SST}R2=1−SSTSSR​

Think about this formula:

  • SSRSST\frac{SSR}{SST}SSTSSR​ represents the fraction of the total variance that your model fails to explain (the proportion of residual error relative to total variance).
  • By subtracting this fraction from 1, you get the fraction of the total variance that your model does explain.

Interpreting R-squared

R-squared values typically range from 0 to 1, although they can sometimes be negative (more on this in the "Limitations of R-squared" section). It's often expressed as a percentage.

"* R2=1R^2 = 1R2=1 (or 100%): This means your model explains all the variability in the target variable. The model's predictions (y^i\hat{y}_iy^​i​) perfectly match the actual values (yiy_iyi​), so SSR=0SSR = 0SSR=0. This is usually too good to be true in scenarios and might indicate overfitting (where the model learned the training data too well, including its noise)."

  • R2=0R^2 = 0R2=0 (or 0%): This means your model explains none of the variability. Its performance is no better than the baseline model that simply predicts the mean (yˉ\bar{y}yˉ​). In this case, SSR=SSTSSR = SSTSSR=SST.
  • 0<R2<10 < R^2 < 10<R2<1: This is the most common range. An R2R^2R2 of 0.75 means that your model explains 75% of the variance in the target variable based on the input features. The remaining 25% of the variance is not captured by the model.

Whether a specific R-squared value is "good" depends heavily on the context of the problem. In some fields, like physics experiments, you might expect very high R-squared values (>0.95>0.95>0.95). In others, like social sciences or predicting stock prices, explaining even a small portion of the variance (e.g., R2=0.1R^2 = 0.1R2=0.1 or 0.2) may be significant because the underlying processes are very complex or noisy.

Visualizing R-squared

For example, two simple regression scenarios:

In this plot, the data points are tightly clustered around the regression line (red). The model's predictions are close to the actual values, resulting in a small SSR relative to SST, and therefore a high R-squared value (e.g., R² ≈ 0.99).

Here, the data points are much more scattered around the regression line. While the line shows a general trend, the model's predictions have larger errors on average. The SSR is a larger fraction of SST, leading to a low R-squared value (e.g., R² ≈ 0.3).

R-squared complements metrics like MAE, MSE, and RMSE. While the error metrics tell you the typical size of the prediction error in the original units of your target variable, R-squared gives you a dimensionless measure (a ratio or percentage) of how much of the data's variance your model has captured. It helps answer the question: "How well does my model fit the data compared to simply using the average?"

  • Applied Linear Statistical Models, Michael H. Kutner, Christopher J. Nachtsheim, John Neter, William Li, 2005 (McGraw-Hill Irwin) - A comprehensive textbook providing a deep statistical foundation for linear regression, including a detailed discussion of R-squared, its components, and its role in model assessment.
  • An Introduction to Statistical Learning: With Applications in R, Gareth James, Daniela Witten, Trevor Hastie, Robert Tibshirani, 2021 (Springer) - This widely used textbook provides an accessible introduction to statistical learning methods. It covers R-squared as a fundamental metric for evaluating regression model performance in a machine learning context.
  • sklearn.metrics.r2_score, scikit-learn developers, 2024 - Official documentation for the R-squared implementation in scikit-learn, a popular Python machine learning library. It explains the calculation and practical application of the metric.

© 2026 ApX Machine Learning