如何在Python中计算线性回归模型的AIC? [英] How to compute AIC for linear regression model in Python?
问题描述
我想为线性模型计算AIC,以比较其复杂性.我这样做如下:
I want to compute AIC for linear models to compare their complexity. I did it as follows:
regr = linear_model.LinearRegression()
regr.fit(X, y)
aic_intercept_slope = aic(y, regr.coef_[0] * X.as_matrix() + regr.intercept_, k=1)
def aic(y, y_pred, k):
resid = y - y_pred.ravel()
sse = sum(resid ** 2)
AIC = 2*k - 2*np.log(sse)
return AIC
但是我收到一个divide by zero encountered in log
错误.
推荐答案
sklearn
的LinearRegression
可以很好地进行预测,但是您已经发现了一些准系统. (通常说sklearn远离所有统计推断.)
sklearn
's LinearRegression
is good for prediction but pretty barebones as you've discovered. (It's often said that sklearn stays away from all things statistical inference.)
statsmodels.regression.linear_model.OLS
具有一个属性属性AIC
和许多其他预设的属性.
statsmodels.regression.linear_model.OLS
has a property attribute AIC
and a number of other pre-canned attributes.
但是,请注意,您需要将单位矢量手动添加到X
矩阵中,以便在模型中包含截距.
However, note that you'll need to manually add a unit vector to your X
matrix to include an intercept in your model.
from statsmodels.regression.linear_model import OLS
from statsmodels.tools import add_constant
regr = OLS(y, add_constant(X)).fit()
print(regr.aic)
在此处正在寻找另一种仍然使用sklearn
的方式进行手动书写的方式.
Source is here if you are looking for an alternative way to write manually while still using sklearn
.
这篇关于如何在Python中计算线性回归模型的AIC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!