Hits: 134
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:
df = pd.read_csv('your_data.csv')
- Create a correlation matrix of all the features in the dataset.
corr_matrix = df.corr()
- Generate a heatmap of the correlation matrix to visualize the correlation between features.
sns.heatmap(corr_matrix)
- 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]
highly_correlated.add(colname)
- Drop the highly correlated features from the dataset.
df = df.drop(highly_correlated, axis=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.
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.