Simple R Squared Calculation in Python


Machine LearningPython

This short tutorial shows how to find the R squared value in Python using sklearn which can be useful when looking at the data correlation in a scatter plot.

R Squared in Python

First and foremost, make sure you have sklearn installed which can be installed with bioconda.

Now, there is below the Python code for the r squared calculation.

from sklearn.metrics import r2_score

# define x and y
x = [3, -0.5, 2, 7]
y = [2.5, 0.0, 2, 8]

# r square estimation
r_squared_value = r2_score(x, y)

>>> r_squared_value
>>> 0.9486081370449679

Here is a video explaining R squared in case you want to learn more about it

Related Posts