Adventures in Machine Learning

Finding the Best Fit: Introducing the Akaike Information Criterion in Python

Introducing the Akaike Information Criterion

When it comes to analyzing data, one of the most common tasks is fitting a regression model. This process involves selecting the best model from a set of candidate models.

However, with so many possible models to choose from, how can we know which one is the best fit? That’s where the Akaike Information Criterion (AIC) comes into play.

In this article, we’ll explore what the AIC is, how it’s calculated, and how to use it to compare models. We’ll also show you how to apply the AIC in Python, using a simple example.

Definition of AIC

The Akaike Information Criterion (AIC) is a metric commonly used to compare statistical models. It provides a way of comparing different models that have been fit to the same data, in order to select the best one.

The AIC takes into account the number of model parameters and the goodness of fit, penalizing models that have more parameters but not a corresponding improvement in fit.

Calculation of AIC

The AIC is calculated as follows: AIC = -2log(L) + 2k, where L is the likelihood function of the model and k is the number of model parameters.

The likelihood function quantifies how well the model fits the data, while the second term in the equation penalizes models with more parameters.

This balance between goodness of fit and model complexity allows the AIC to identify models that are neither too simple nor too complex, but rather strike a balance between the two.

Use of AIC for Model Comparison

Once the AIC values have been calculated for each model, they can be compared to identify the best fit. The model with the lowest AIC value is generally considered to be the best fit.

While the AIC value itself has no intrinsic meaning, the difference between AIC values for different models (AIC) can be used to determine the relative support for each model.

A general rule of thumb is that models with a difference in AIC values of less than 2 are considered equally supported, while models with a AIC greater than 10 are strongly disfavored.

Applying AIC in Python

Now that we understand what AIC is and how it’s used, let’s see how to apply it in Python. We’ll use a simple example to illustrate the process.

The goal is to fit a linear regression model to predict the price of a house based on its size and number of bedrooms.

Dataset Loading and Variables Selection

First, we need to load the data into Python and select our predictor variables. We’ll be using the Housing Price dataset from the Statsmodels package.


import statsmodels.api as sm

from statsmodels.formula.api import ols

import pandas as pd

data = sm.datasets.get_rdataset(“Housing”, “Ecdat”).data

X = data[[“lotsize”, “bedrooms”]]

y = data[“price”]


Fitting and AIC Calculation for Model 1

Next, we’ll fit our first model using the “ols” function from the statsmodels package.


model1 = ols(“price ~ lotsize + bedrooms”, data=data).fit()

a1 = model1.aic

print(“Model 1 AIC: “, a1)


This code specifies the model formula and fits the model to our data.

The AIC value for this model is displayed using the ‘aic’ method.

Fitting and AIC Calculation for Model 2

Next, we’ll fit our second model using a different combination of predictor variables. “`python

model2 = ols(“price ~ lotsize”, data=data).fit()

a2 = model2.aic

print(“Model 2 AIC: “, a2)


This code fits our second model, which uses only the “lotsize” variable as a predictor.

Again, we calculate the AIC value using the ‘aic’ method.

Model Comparison and Selection

Finally, we can compare the AIC values for our two models to determine which one is the best fit. “`python

if a1 < a2:

print(“Model 1 is the better fit.”)


print(“Model 2 is the better fit.”)


In this case, the AIC value for Model 1 is lower than the AIC value for Model 2.

Therefore, we select Model 1 as the better fitting model.


In this article, we’ve introduced the Akaike Information Criterion and shown how it can be used to compare regression models. By taking into account both model complexity and goodness of fit, the AIC provides a useful metric for identifying the best fitting model.

We’ve also shown how to apply the AIC in Python, using a simple example. Next time you’re analyzing data, consider using the AIC to help you select the best fitting model.

The Akaike Information Criterion (AIC) is a critical metric used to compare statistical models and identify the best fit among them. It achieves this by balancing the model complexity against the goodness of fit.

We learned how to calculate AIC values using a simple example and presented how AIC helps in selecting the right model to work with. Stats models recognize AIC scores as important in choosing the best model, and Python programming language, which has become a popular tool for statisticians, scientists, and data analysts, provides an easy way of computing these scores.

By understanding AIC, we have a powerful tool in our data analysis toolkit.

Popular Posts