what is r-squared in linear regression models?
You asked about the python code x = r2_score(y_true, y_pred)
Note that:




y_pred
y_true




y_pred stands for "prediction of the y-variable"
y_true stands for "true value of the y-variable"


The predicted value is never the raw data. Often times, the predicted value is a line of best fit.
y_true are the raw numbers you collect during either an experiment, survey, or scientific study.




Suppose that you have a model of a teenagers height as a function of age.




AGE (years since birth)
10 years
12 years
14 years
16 years




HIEGHT (inches)
55
60
65
68




AGE is defined as years since birth (not years since conception, like in China).
Also, we round age down to the nearest whole year.
A child 10.81773 years old is listed as 10 years old.
An example of a predicted value might be that you think, on average, a child 10 years of age is 55 inches tall.
If you conduct a study where you measure the height of 1,038 children who are each 10 years old, you will find that the children are not all exactly 55 inches tall.
The raw data (measured heights of children) is known as the set of true y-values.
Statisticians often measure error by comparing the distance from the measured height of a child from the predicted height.
For example, 10-year-old Joanna's height is 52 inches (rounded to the nearest whole inch).
We predicted Joanna's height to be 55 inches.
There are 3 inches of difference between the true, and the predicted, values.
Often times, statisticians want one number for a data set, instead of 1,038 different numbers.
One thing you can do is convert the difference between the predicted height, and the actual heights of the children, into a positive number. For example, -5 becomes +5
After that, compute the average positive-difference (in inches) between actual height and predicted height.
Taking the absolute difference is important. Some children are shorter than predicted (-2 inches), and some children are taller (+7 inches).
If you allow negative numbers the average difference between the average height and actual height will always be zero.

Take 1,038 actual heights.
subtract 55 inches from the actual height.
sum up the height discrepancies without converting to positive numbers
the result will always be zero.

In fact, one way to define mean is that the mean of a sequence of numbers is a number x such that when you calculate the difference between each data-point and x, then sum the results, the answer is zero.
Generally, statisticians square the differences.
Since Joanna was short  (-2 inches) the squared error for Joanna is +4 inches.
A negative number times a negative number is always a positive number.
Squaring gets rid of the negative signs.
Taking the absolute value gets rid of the negative signs.
Actually... there are about a million ways to get rid of the negative signs.
Some statistician's are like the parrots in the 1998 movie "Paulie."
I say "taco" and they say "TACO! TACO! TACO!"
They copy what other statisticians do, and it never occurs to them that there is more than one way to do statistical analysis.
I have a degree in math, I have seen proofs that the curve which minimizes the mean-squared-error is ideal in some ways.
However, the mean-squared-error is more of a heuristic, or proxy, than a measure of what really, truly, matters.
Nobody actually has a formula which perfectly computes for data-set A, and data-set B, which data-set is more "spread-out" than the other.
It is difficult to tell what humans care about.
Anyways, mean-square-error is better than nothing. IT measures how spread-out a data-set is. A
re the data-points WAY WAY far away from the average, or all pretty close to the average?
What-if 55 inches was the true average height of a 10-year-old child?
Also imagine "what-if" the true standard deviation was 4 inches.
In that imaginary world, suppose you randomly sampled 1,038 children, each 10 years of age.
Your sample-variance (computed from experimental data) is 7.1091 inches.
What is the likelihood that a sample of 1,038 children would have a variance of 7.1091 inches or more?
If your model is correct, what is the likelihood that the data would as far, or further, from the model's prediction as that you observed?
If the data you see is way WAY WAY far away from the predicted value, then your model is probably bad.
Anyway the R-squared measure is:

0% if the data does not match the model at all
100% if the differences between the data and the prediction are adequately explained by random chance.

For example, if you toss a fair-coin 1,000 times, it is reasonably that 491 results would be heads instead of exactly 500 results of "heads".
The question is, is the observed value (491 heads out of 1,000 tosses) likely, or extremely weird, given the model which says that it should be about 500 out of 1,000.
I am a bit confused about the definition of the r-squared score in the linear regression model.
As far as I know, the R-squared score represents how much of the dependent variable can be determined by the independent variables. However, in the scikit learn library, we have an r2-score function that calculates the r-squared score like r2_score(y_true, y_pred). But both of the parameters here are the output values, and it doesn't seem that it involves any of the indepent variables. Could you help me to understand how this is calculated?