Open In App

Outlier Detection in Logistic Regression

Outliers, data points that deviate significantly from the rest, can significantly impact the performance of logistic regression models. In this article we will explore various techniques for detecting and handling outliers in Logistic regression.

What are Outliers?

An outlier is an observation that falls far outside the typical range of other data points in a dataset. These anomalies can arise from errors in data collection, human mistakes, equipment malfunctions, or data transmission issues. Outliers can lead to:

Outliers vs. Leverage Points vs. Influential Observations

It's important to distinguish between outliers and two related concepts:

In logistic regression, outliers with low leverage can still exert a substantial influence due to the non-linear relationship between the independent variables and the predicted probabilities.

Outlier Detection Techniques in Logistic Regression

Detecting and appropriately managing outliers is crucial for ensuring the accuracy and reliability of logistic regression analyses. Two common approaches for detecting outliers in logistic regression are:

Single-case deletion approach

The single-case deletion approach is one of the techniques of outlier detection, which involves removing individual outliers from the dataset one at a time. However, it suffers from two limitations in the presence of multiple outliers:

Multiple-case Deletion approach

One-by-one or sequential detection of outliers in a single-case deletion approach may fall into the trap of masking and swamping effects. We can use a multiple-case deletion approach instead of a single-case deletion approach to overcome this issue. Even in the presence of masking effects, the multiple-case deletion approach aims to identify the multiple influential observations in the dataset.

There are two stages involved in this deletion approach:

The multiple-case deletion approach generally leads to a more accurate identification of outliers compared to the single-case approach.

Handling Outliers

Once outliers are detected, several techniques can be used to address them:

Detection and Handling Outliers : Implementation

Step 1: Import the necessary libraries and load the dataset

import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
#load the dataset
iris = load_iris()
X,y = iris.data, iris.target

Step 2: Introducing outliers randomly to the dataset

In this step, we introduce the outliers randomly to the dataset, since it is common method in outlier detection techniques. Ten indices are randomly chosen and adds the random noise to it, this simulates the presence of outliers in the real-world datasets.

np.random.seed(0)
outlier_indices = np.random.choice(range(len(X)), size = 10, replace=False)
X[outlier_indices]+= 50 * np.random.rand(10,4)

Step 3: Creating datasets with specified outlier treatment

def create_dataset(X, y, outlier_treatment, outlier_indices):
  #Single case
  if outlier_treatment == "single":
    X_no_outliers= np.copy(X)
    y_no_outliers = np.copy(y)
    for idx in outlier_indices:
      X_no_outliers = np.delete(X_no_outliers, idx, axis =0)
      y_no_outliers = np.delete(y_no_outliers, idx)
    X_train, X_test, y_train, y_test = train_test_split(X_no_outliers, y_no_outliers, test_size=0.2, random_state=42)
  #multiple
  elif outlier_treatment == "multiple":
    #remove all the outliers
    outlier_indices = np.concatenate((outlier_indices, outlier_indices + 1, outlier_indices+2))
    X_no_outliers =np.delete(X, outlier_indices, axis=0)
    y_no_outliers = np.delete(y, outlier_indices)
    #Split into training and testing dataset
    X_train, X_test, y_train, y_test = train_test_split(X_no_outliers, y_no_outliers, test_size =0.2, random_state=42)
  return X_train, X_test, y_train, y_test

Step 4: Training logistic regression

def train_logistic_regression(X_train, X_test, y_train, y_test):
  lr= LogisticRegression(max_iter=1000)
  lr.fit(X_train, y_train)
  y_pred = lr.predict(X_test)
  acc=accuracy_score(y_test,y_pred)
  return acc

Step 5: Evaluation

Using create_dataset function we call the function with single and multiple outlier treatment depending upon the specified approach. This function creates the datasets where outliers are handles differently based on the approach chosen.

X_train_single, X_test_single, y_train_single, y_test_single = create_dataset(X, y, "single", outlier_indices)
acc_single = train_logistic_regression(X_train_single, X_test_single, y_train_single, y_test_single)
X_train_multiple, X_test_multiple, y_train_multiple, y_test_multiple = create_dataset(X, y, "multiple", outlier_indices)
acc_multiple = train_logistic_regression(X_train_multiple, X_test_multiple, y_train_multiple, y_test_multiple)
print("Accuracy using single case deletion approach:", acc_single)
print("Accuracy using multiple case deletion approach:", acc_multiple)

Output:

Accuracy using single case deletion approach: 0.7857142857142857
Accuracy using multiple case deletion approach: 0.9583333333333334

Thus, we can clearly see that multiple-case deletion approach is more effective in handling outliers compared to the single case deletion approach since it leads to a higher accuracy in the trained model.

Challenges of Outlier Detection

Some challenges in outlier detection:

Conclusion

Outlier detection is a crucial aspect of logistic regression for ensuring accurate model predictions. Through this tutorial, we have gained knowledge about outlier detection techniques such as single and multiple case deletion approaches which play a huge role in detecting the potential outliers in the logistic regression.

Article Tags :