VOOZH about

URL: https://www.geeksforgeeks.org/machine-learning/how-to-calculate-studentized-residuals-in-python/

⇱ How to Calculate Studentized Residuals in Python? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Calculate Studentized Residuals in Python?

Last Updated : 23 Jul, 2025

Studentized residual is a statistical term and it is defined as the quotient obtained by dividing a residual by its estimated standard deviation. This is a crucial technique used in the detection of outlines. Practically, one can claim that any type of observation in a dataset having a studentized residual of more than 3 (absolute value) is an outlier. 

The following Python libraries should already be installed in our system:

  • pandas
  • numpy
  • statsmodels

You can install these packages on your system by using the below command on the terminal.

pip3 install pandas numpy statsmodels matplotlib

Steps to calculate studentized residuals in Python

Step 1: Import the libraries.

We need to import the libraries in the program that we have installed above.

 
 Step 2: Create a data frame.

Firstly, we are required to create a data frame. With the help of the pandas' package, we can create a data frame. The snippet is given below,

Step 3: Build a simple linear regression model.

Now we need to build a simple linear regression model of the created dataset. For fitting a simple linear regression model Python provides ols() function from statsmodels package.

Syntax:

statsmodels.api.OLS(y, x)

Parameters:

  • y : It represents the variable that depends on x
  • x :It represents independent variable

Example:

 
 Step 4: Producing studentized residual. 

For producing a dataFrame that would contain the studentized residuals of each observation in the dataset we can use outlier_test() function.

Syntax:

simple_regression_model.outlier_test()

This function will produce a dataFrame that would contain the studentized residuals for each observation in the dataset

 
 Below is the complete implementation.

Output:

👁 Image

The output is a data frame that contains:

  • The studentized residual
  • The unadjusted p-value of the studentized residual
  • The Bonferroni-corrected p-value of the studentized residual

We can see that the studentized residual for the first observation in the dataset is -1.121201, the studentized residual for the second observation is 0.954871, and so on.

Visualization:

Now let us go into the visualization of the studentized residual. With the help of matplotlib we can make a plot of the predictor variable values VS the corresponding studentized residuals.

Example:

Output:

👁 Image

Plot.png:

👁 Image

Comment