[英]How to compute AIC for linear regression model in Python?
I want to compute AIC for linear models to compare their complexity. 我想为线性模型计算AIC,以比较其复杂性。 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
But I receive a divide by zero encountered in log
error. 但是我divide by zero encountered in log
错误中divide by zero encountered in log
。
sklearn
's LinearRegression
is good for prediction but pretty barebones as you've discovered. sklearn
的LinearRegression
非常适合预测,但是您已经发现了相当准系统。 (It's often said that sklearn stays away from all things statistical inference.) (通常说sklearn远离所有统计推断。)
statsmodels.regression.linear_model.OLS
has a property attribute AIC
and a number of other pre-canned attributes. statsmodels.regression.linear_model.OLS
具有属性属性AIC
和许多其他预设的属性。
However, note that you'll need to manually add a unit vector to your X
matrix to include an intercept in your model. 但是,请注意,您需要手动向X
矩阵添加单位向量,以在模型中包含截距。
from statsmodels.regression.linear_model import OLS
from statsmodels.tools import add_constant
regr = OLS(y, add_constant(X)).fit()
print(regr.aic)
Source is here if you are looking for an alternative way to write manually while still using sklearn
. 来源是在这里 ,如果你正在寻找的同时仍然使用手工编写的另一种方式sklearn
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.