How to drop out highly correlated features in Python

How to drop out highly correlated features in Python

In machine learning, correlated features can cause problems because they can provide redundant information to the model. Having too many correlated features can also increase the risk of overfitting. One way to deal with correlated features is to drop some of them. This process is called feature selection.

Here’s how you can drop highly correlated features in Python:

  1. Import the necessary libraries. You will need to have pandas, numpy and seaborn installed.
import pandas as pd
import numpy as np
import seaborn as sns
  1. Load your dataset into a pandas DataFrame.
df = pd.read_csv('your_data.csv')


  1. Create a correlation matrix of all the features in the dataset.
corr_matrix = df.corr()


  1. Generate a heatmap of the correlation matrix to visualize the correlation between features.


  1. Identify highly correlated features. You can do this by looking for features with a correlation coefficient (e.g. Pearson correlation coefficient) that is greater than a certain threshold (e.g. 0.8).
// Create a set of highly correlated features
highly_correlated = set()
corr_threshold = 0.8
for i in range(len(corr_matrix.columns)):
       for j in range(i):
               if abs(corr_matrix.iloc[i, j]) > corr_threshold:
                      colname = corr_matrix.columns[i]
  1. Drop the highly correlated features from the dataset.
df = df.drop(highly_correlated, axis=1)


  1. Now your dataframe df will only contain features which are not highly correlated

Note that the correlation threshold can be adjusted according to your requirements, and other correlation coefficients can be used like Kendall’s rank correlation coefficient, or Spearman’s rank correlation coefficient. In addition, this method is only considering pairwise correlation between features. If you have a high number of correlated features, it may be more efficient to use a dimensionality reduction techniques such as PCA or linear discriminant analysis.

In this Learn through Codes example, you will learn: How to drop out highly correlated features in Python.

Essential Gigs