问题
Using sklearn.metrics I can compute R square.How I can compute Adjusted Adjusted R square using Linear Regression model?
回答1:
Scikit-Learn's Linear Regression does not return the adjusted R squared. However, from the R -squared you can calculate the adjusted R squared from the formula:
Where p is the number of predictors (also known as features or explanatory variables) and n is the number of data points. So if your data is in a dataframe called train and you have the r2
, the formula would be:
adj_r2 = 1 - (1 - r2 ** 2) * ((train.shape[1] - 1) / (train.shape[0] - train.shape[1] - 1))
train.shape[0]
is the number of observations, and train.shape[1]
is the number of features.
The StatsModels library has a linear regression method that does return the adjusted R squared (among many other metrics). Here is the documentation.
The formula above is from this Stack Exchange Answer which says this is known as Wherry Formula - 1. There are a number of different formulas used to calculate adjusted R squared, but this is the method used in R. For more on the differences between adjusted R squared methods, see the answer linked to or the paper Estimating R^2 Shrinkage in Regression
来源:https://stackoverflow.com/questions/51023806/how-to-get-adjusted-r-square-for-linear-regression