如何在Python中计算线性回归模型的AIC? [英] How to compute AIC for linear regression model in Python?

查看:3647
本文介绍了如何在Python中计算线性回归模型的AIC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为线性模型计算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错误.

推荐答案

sklearnLinearRegression可以很好地进行预测,但是您已经发现了一些准系统. (通常说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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆