Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Calculate R^2 in Tensorflow

I am trying to do regression in Tensorflow. I'm not positive I am calculating R^2 correctly as Tensorflow gives me a different answer than sklearn.metrics.r2_score Can someone please look at my below code and let me know if I implemented the pictured equation correctly. Thanks

The formula I am attempting to create in TF

total_error = tf.square(tf.sub(y, tf.reduce_mean(y)))
unexplained_error = tf.square(tf.sub(y, prediction))
R_squared = tf.reduce_mean(tf.sub(tf.div(unexplained_error, total_error), 1.0))
R = tf.mul(tf.sign(R_squared),tf.sqrt(tf.abs(R_squared)))
like image 946
Matt Camp Avatar asked Feb 20 '17 17:02

Matt Camp


People also ask

How do you calculate r 2 value?

Solution. To calculate R2 you need to find the sum of the residuals squared and the total sum of squares. Start off by finding the residuals, which is the distance from regression line to each data point. Work out the predicted y value by plugging in the corresponding x value into the regression line equation.

How do you calculate R 2 in Python?

R2 = 1- 600/200 = -2 metrics in Python to compute R2 score.

What is R 2 in machine learning?

R-squared is a statistical measure that represents the goodness of fit of a regression model. The ideal value for r-square is 1. The closer the value of r-square to 1, the better is the model fitted.


3 Answers

What you are computing the "R^2" is

R^2_{\text{wrong}} = \operatorname{mean}_i \left( \frac{(y_i-\hat y_i)^2}{(y_i-\mu)^2} - 1\right)1

compared to the given expression, you are computing the mean at the wrong place. You should take the mean when computing the errors, before doing the division.

unexplained_error = tf.reduce_sum(tf.square(tf.sub(y, prediction)))
total_error = tf.reduce_sum(tf.square(tf.sub(y, tf.reduce_mean(y))))
R_squared = tf.sub(1, tf.div(unexplained_error, total_error))
like image 54
kennytm Avatar answered Oct 16 '22 09:10

kennytm


The function is given here:

def R_squared(y, y_pred):
  residual = tf.reduce_sum(tf.square(tf.subtract(y, y_pred)))
  total = tf.reduce_sum(tf.square(tf.subtract(y, tf.reduce_mean(y))))
  r2 = tf.subtract(1.0, tf.div(residual, total))
  return r2

The concept is explained here.

like image 28
Shashank Avatar answered Oct 16 '22 08:10

Shashank


I would strongly recommend against using a recipe to calculate this! The examples I've found do not produce consistent results, especially with just one target variable. This gave me enormous headaches!

The correct thing to do is to use tensorflow_addons.metrics.RQsquare(). Tensorflow Add Ons is on PyPi here and the documentation is a part of Tensorflow here. All you have to do is set y_shape to the shape of your output, often it is (1,) for a single output variable.

like image 4
rjurney Avatar answered Oct 16 '22 10:10

rjurney