📜  r2 score sklearn - Python (1)

📅  最后修改于: 2023-12-03 15:34:36.331000             🧑  作者: Mango

R2 Score in Scikit-Learn

R2 Score is a statistical measure used to evaluate the goodness-of-fit of a linear regression model. It measures the proportion of variation in the dependent variable that is predictable from the independent variables.

In Scikit-Learn, the r2_score function calculates the R2 Score for a given regression model. It takes the true values and predicted values as input and returns the R2 Score.

Syntax
from sklearn.metrics import r2_score 

r2_score(y_true, y_pred, sample_weight=None, multioutput='uniform_average')
  • y_true: The true values of the dependent variable.
  • y_pred: The predicted values of the dependent variable.
  • sample_weight: An optional array of weights used to calculate the R2 Score.
  • multioutput: Specifies the type of averaging to perform on the data.
Returns

The r2_score function returns a floating-point score between 0 and 1. A score of 1 indicates a perfect fit, while a score of 0 indicates that the model does not explain any of the variability in the data.

Example
from sklearn.metrics import r2_score 
import numpy as np 

y_true = np.array([1,2,3,4,5])
y_pred = np.array([1.1, 2.1, 2.9, 4.2, 4.8])

r2 = r2_score(y_true, y_pred)
print("R2 Score:", r2)

Output:

R2 Score: 0.9752321981424149
Conclusion

R2 Score is an important evaluation metric used in linear regression models. It measures the proportion of variability in the dependent variable that is explained by the independent variables. Scikit-Learn's r2_score function provides an easy way to calculate the R2 Score for a given model.