CardiovascularDisease/CVD/data_visualizer.py
def draw_corr_matrix(df: pd.DataFrame):
    """
    - Draw correlation matrix as heatmap.
    - Draw correlation for target feature and mutual information in a bar plot.
    Note: Assuming the target feature is in the last column of df.

    :parameter df: pandas dataframe with all of the relevant features as columns.
    :return: fig: matplotlib figure object;
             corr: correlation matrix for all features;
             scores: pandas dataframe with the correlation and mutual information scores
                     for the target feature.
    """

    target = df.columns[-1]

    corr = df.corr()  # Calculate the correlation matrix
    target_corr = corr.loc[target, corr.columns.delete(-1)]  # Correlation for the target
    mi = mutual_info_classif(df.iloc[:, :-1], df[target])  # Calculate MI score
    scores = target_corr.to_frame()
    scores.rename(columns={target: "Corr"}, inplace=True)
    scores["MI"] = mi
    scores_melted = pd.melt(scores, ignore_index=False)

    mask = np.zeros_like(corr)
    mask[np.triu_indices_from(mask)] = True  # Generate a mask for the upper triangle

    fig, ax = plt.subplots(2, 1, figsize=(8, 15), dpi=100)
    sns.heatmap(corr, mask=mask, square=True, ax=ax[0], cmap='Spectral_r',
                annot=True, fmt='.2f', annot_kws={'fontsize': 8})
    ax[0].set_title("Feature Correlation", fontdict={"fontsize": 14})

    # Plot the "Cardio" correlation and mutual information scores on the sme graph.
    sns.barplot(x="value", y=scores_melted.index, hue="variable", 
                data=scores_melted, ax=ax[1], palette='crest')
    # sns.barplot(x=[np.array(cardio_corr), mi], y=cardio_corr.index, ax=ax[1],
    #             color=[0.30, 0.41, 0.29])  # to plot just the "Cardio" correlation scores
    ax[1].set_title(f"Target ({target}) Correlation and Mutual Information",
                    fontdict={"fontsize": 14})
    ax[1].set_xlabel(None)
    ax[1].legend(title=None)
    ax[1].grid(axis='x')

    fig.savefig('Corr_matrix_Target.png')

    return fig, corr, scores

    - Draw correlation matrix as heatmap.
    - Draw correlation for target feature and mutual information in a bar plot.
    Note: Assuming the target feature is in the last column of df.

    :parameter df: pandas dataframe with all of the relevant features as columns.
    :return: fig: matplotlib figure object;
             corr: correlation matrix for all features;
             scores: pandas dataframe with the correlation and mutual information scores
                     for the target feature.
    
Draw correlation matrix as heatmap.
Draw correlation for target feature and mutual information in a bar plot.
Note: Assuming the target feature is in the last column of df.
Draw correlation matrix as heatmap . Draw correlation for target feature and mutual information in a bar plot . Note : Assuming the target feature is in the last column of df .
def draw_corr_matrix(df: pd.DataFrame):
    target = df.columns[-1]
    corr = df.corr()  
    target_corr = corr.loc[target, corr.columns.delete(-1)]  
    mi = mutual_info_classif(df.iloc[:, :-1], df[target])  
    scores = target_corr.to_frame()
    scores.rename(columns={target: "Corr"}, inplace=True)
    scores["MI"] = mi
    scores_melted = pd.melt(scores, ignore_index=False)
    mask = np.zeros_like(corr)
    mask[np.triu_indices_from(mask)] = True  
    fig, ax = plt.subplots(2, 1, figsize=(8, 15), dpi=100)
    sns.heatmap(corr, mask=mask, square=True, ax=ax[0], cmap='Spectral_r',
                annot=True, fmt='.2f', annot_kws={'fontsize': 8})
    ax[0].set_title("Feature Correlation", fontdict={"fontsize": 14})
    sns.barplot(x="value", y=scores_melted.index, hue="variable", 
                data=scores_melted, ax=ax[1], palette='crest')
    ax[1].set_title(f"Target ({target}) Correlation and Mutual Information",
                    fontdict={"fontsize": 14})
    ax[1].set_xlabel(None)
    ax[1].legend(title=None)
    ax[1].grid(axis='x')
    fig.savefig('Corr_matrix_Target.png')
    return fig, corr, scores
def draw_corr_matrix ( df : pd . DataFrame ) : target = df . columns [ - 1 ] corr = df . corr ( ) target_corr = corr . loc [ target , corr . columns . delete ( - 1 ) ] mi = mutual_info_classif ( df . iloc [ : , : - 1 ] , df [ target ] ) scores = target_corr . to_frame ( ) scores . rename ( columns = { target : "Corr" } , inplace = True ) scores [ "MI" ] = mi scores_melted = pd . melt ( scores , ignore_index = False ) mask = np . zeros_like ( corr ) mask [ np . triu_indices_from ( mask ) ] = True fig , ax = plt . subplots ( 2 , 1 , figsize = ( 8 , 15 ) , dpi = 100 ) sns . heatmap ( corr , mask = mask , square = True , ax = ax [ 0 ] , cmap = 'Spectral_r' , annot = True , fmt = '.2f' , annot_kws = { 'fontsize' : 8 } ) ax [ 0 ] . set_title ( "Feature Correlation" , fontdict = { "fontsize" : 14 } ) sns . barplot ( x = "value" , y = scores_melted . index , hue = "variable" , data = scores_melted , ax = ax [ 1 ] , palette = 'crest' ) ax [ 1 ] . set_title ( f"Target ({target}) Correlation and Mutual Information" , fontdict = { "fontsize" : 14 } ) ax [ 1 ] . set_xlabel ( None ) ax [ 1 ] . legend ( title = None ) ax [ 1 ] . grid ( axis = 'x' ) fig . savefig ( 'Corr_matrix_Target.png' ) return fig , corr , scores
Draw correlation matrix as heatmap.
Draw correlation matrix as heatmap .
"""
    - Draw correlation matrix as heatmap.
    - Draw correlation for target feature and mutual information in a bar plot.
    Note: Assuming the target feature is in the last column of df.

    :parameter df: pandas dataframe with all of the relevant features as columns.
    :return: fig: matplotlib figure object;
             corr: correlation matrix for all features;
             scores: pandas dataframe with the correlation and mutual information scores
                     for the target feature.
    """ # Calculate the correlation matrix # Correlation for the target # Calculate MI score # Generate a mask for the upper triangle # Plot the "Cardio" correlation and mutual information scores on the sme graph. # sns.barplot(x=[np.array(cardio_corr), mi], y=cardio_corr.index, ax=ax[1], #             color=[0.30, 0.41, 0.29])  # to plot just the "Cardio" correlation scores
matplotlib figure object;
corr: correlation matrix for all features;
scores: pandas dataframe with the correlation and mutual information scores
for the target feature.
matplotlib figure object ; corr : correlation matrix for all features ; scores : pandas dataframe with the correlation and mutual information scores for the target feature .
df
pd.DataFrame
pandas dataframe with all of the relevant features as columns.
pandas dataframe with all of the relevant features as columns .