Understand the fundamentals of this metric, what it evaluates, and how to interpret the results.
Compute and visualise the metric with Python 3.13 code examples, covering key steps and practical checkpoints.
Combine charts and complementary metrics for effective model comparison and threshold tuning.
The coefficient of determination is a value in statistics that expresses how much of the dependent variable (objective variable) is explained by the independent variable (explanatory variable).
Generally, the higher the better the rating indicator
The best case is 1.
However, the more features you add, the higher the score tends to be.
Therefore, it is not possible to judge “high accuracy of the model” by looking at this indicator alone
Coefficient of determination when using the least squares method
#
In the case of a regression line for a single regression using the least squares method, the range of the coefficient of determination is \( 0 \le R^2 \le 1\).
Let us try to find the coefficient of determination by running a 100-line regression with random noise on the data.
fromsklearn.linear_modelimportLinearRegressionfromsklearn.pipelineimportmake_pipelinefromsklearn.preprocessingimportStandardScalerr2_scores=[]foriinrange(100):X,y=make_regression(n_samples=500,n_informative=1,n_features=1,effective_rank=4,noise=i*0.1,random_state=RND,)train_X,test_X,train_y,test_y=train_test_split(X,y,test_size=0.33,random_state=RND)# linear regressionmodel=make_pipeline(StandardScaler(with_mean=False),LinearRegression(positive=True)).fit(train_X,train_y)# Calculate coefficient of determinationpred_y=model.predict(test_X)r2=r2_score(test_y,pred_y)r2_scores.append(r2)plt.figure(figsize=(8,4))plt.hist(r2_scores,bins=20)plt.show()