Coefficient of determination also called as R^{2} score is used to evaluate the performance of a linear regression model. It is the amount of the variation in the output dependent attribute which is predictable from the input independent variable(s). It is used to check how well-observed results are reproduced by the model, depending on the ratio of total deviation of results described by the model.

**Mathematical Formula:**

R^{2}= 1- SS_{res }/ SS_{tot}

Where,

SS_{res} is the sum of squares of the residual errors.

SS_{tot} is the total sum of the errors.

**Interpretation of R ^{2} score:**

Assume R

^{2}= 0.68

It can be referred that 68% of the changeability of the dependent output attribute can be explained by the model while the remaining 32 % of the variability is still unaccounted for.

R

^{2 }indicates the proportion of data points which lie within the line created by the regression equation. A higher value of R

^{2}is desirable as it indicates better results.

**Examples**

**Case 1 Model gives accurate results**

R^{2}= 1- 0/200 = 1

**Case 2 Model gives same results always **

R^{2}= 1- 200/200 = 0

**Case 3 Model gives ambiguous results**

R^{2}= 1- 600/200 = -2

We can import r2_score from sklearn.metrics in Python to compute R^{2} score.

**Python Implementation:**

**Code 1: Import r2_score from sklearn.metrics**

`from` `sklearn.metrics ` `import` `r2_score ` |

*chevron_right*

*filter_none*

**Code 2: Calculate R ^{2} score for all the above cases.**

`### Assume y is the actual value and f is the predicted values ` `y ` `=` `[` `10` `, ` `20` `, ` `30` `] ` `f ` `=` `[` `10` `, ` `20` `, ` `30` `] ` `r2 ` `=` `r2_score(y, f) ` `print` `(` `'r2 score for perfect model is'` `, r2) ` |

*chevron_right*

*filter_none*

**Output:**

r2 score for perfect model is 1.0

`### Assume y is the actual value and f is the predicted values ` `y ` `=` `[` `10` `, ` `20` `, ` `30` `] ` `f ` `=` `[` `20` `, ` `20` `, ` `20` `] ` `r2 ` `=` `r2_score(y, f) ` `print` `(` `'r2 score for a model which predicts mean value always is'` `, r2) ` ` ` |

*chevron_right*

*filter_none*

**Output:**

r2 score for a model which predicts mean value always is 0.0

**Code 3:**

`### Assume y is the actual value and f is the predicted values ` `y ` `=` `[` `10` `, ` `20` `, ` `30` `] ` `f ` `=` `[` `30` `, ` `10` `, ` `20` `] ` `r2 ` `=` `r2_score(y, f) ` `print` `(` `'r2 score for a worse model is'` `, r2) ` |

*chevron_right*

*filter_none*

**Output:**

r2 score for a worse model is -2.0

**Conclusion:**

- The best possible score is 1 which is obtained when the predicted values are the same as the actual values.
- R
^{2}score of baseline model is 0. - During the worse cases,
**R**score can even be negative.^{2}

## Recommended Posts:

- Python | Extract Score list of String
- Z score for Outlier Detection - Python
- Python PRAW - Getting the score of a comment in Reddit
- Calculating the completeness score using sklearn in Python
- Python - Kendall Rank Correlation Coefficient
- NLP | How to score words with Execnet and Redis
- ML | Fowlkes-Mallows Score
- ML | Models Score and Error
- Important differences between Python 2.x and Python 3.x with examples
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Python | Add Logging to a Python Script
- Python | Add Logging to Python Libraries
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- Python | Visualizing O(n) using Python
- Python | Index of Non-Zero elements in Python list
- Python | Convert list to Python array
- MySQL-Connector-Python module in Python

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.