I'm trying to get some information on the correlation of the independent variables.
My dataset has a lot of variables, therefore the heatmap is not solution, it is very unreadable.
Currently, I have made a function that returns only those variables that are highly correlated. I would like to change it in way to indicate pairs of correlated features.
The rest of the explanations below:
def find_correlated_features(df, threshold, target_variable):
df_1 = df.drop(target_variable)
#corr_matrix has in index and columns names of variables
corr_matrix = df_1.corr().abs()
# I'm taking only half of this matrix to prevent doubling results
half_of_matrix = corr_matrix.where(np.triu(np.ones(corr_matrix.shape), k = 1).astype(np.bool))
# This prints list of columns which are correlated
to_drop = [column for column in half_of_matrix.columns if any(half_of_matrix[column] > threshold)]
return to_drop
The best if this function would return pandas dataframe with column_1; column_2; corr_coef only variables that are above threshold.
Something like this:
output = {'feature name 1': column_name,
'feature name 2': index,
'correlation coef': corr_coef}
output_list.append(output)
return pd.DataFrame(output_list).sort_values('corr_coef', ascending=False)