helpers.py
def variance_explained_plot(pca, pca_null=None, title=None, max_comp=None,
                            leg_loc='best', figsize=None):
    """
    Plot variance explained and cumulative variance explained for PCA.
    Citation: code modified from:
    Sebastian Raschka. 2015. Python Machine Learning. Packt Publishing.

    Parameters
    ----------
    pca : sklearn PCA object
        A fitted PCA object from sklearn.
    pca_null : sklearn PCA object
        A PCA object fitted on an uncorrelated null dataset, optional.
    title : String
        A title for the plot, optional.
    max_comp : int
        Maximum number of components to plot, default no limit.
    leg_loc : String
        Location for legend on the plot using matplotlib arguments, default 'best'
    figsize : tuple
        Tuple of width and height for figure, optional.
    """

    colors = ['#1b9e77', '#d95f02', '#7570b3', '#e7298a', '#66a61e', '#e6a602',
              '#a6761d', '#666666', '#000000']

    fig, ax = plt.subplots()
    if figsize is not None:
        fig.set_size_inches(figsize)

    if title is not None:
        fig.suptitle(title)

    if max_comp is None:
        max_comp = len(pca.explained_variance_ratio_)

    var_exp = pca.explained_variance_ratio_[:max_comp]
    cum_var_exp = np.cumsum(var_exp)
    component_indices = range(1,max_comp+1)

    if pca_null is not None:
            var_exp_null = pca_null.explained_variance_ratio_[:max_comp]
            cum_var_exp_null = np.cumsum(var_exp_null)
            component_indices_null = range(1,max_comp+1)

    ax.bar(component_indices, var_exp, align='edge', width=-0.4,
           label='comp.', color=colors[0])
    ax.step(component_indices, cum_var_exp, where='mid', label='cum.',
            color=colors[0])
    if pca_null is not None:
            ax.bar(component_indices_null, var_exp_null, align='edge',
                   width=0.4, label='comp, null',
                   color=colors[1])
            ax.step(component_indices_null, cum_var_exp_null, where='mid',
                    label='cum, null', color=colors[1])

    ax.set_xlabel('Principal component index')
    ax.set_xticks(component_indices)
    ax.set_ylabel('Explained variance ratio')
    ax.legend(loc=leg_loc)
    plt.show()

    Plot variance explained and cumulative variance explained for PCA.
    Citation: code modified from:
    Sebastian Raschka. 2015. Python Machine Learning. Packt Publishing.

    Parameters
    ----------
    pca : sklearn PCA object
        A fitted PCA object from sklearn.
    pca_null : sklearn PCA object
        A PCA object fitted on an uncorrelated null dataset, optional.
    title : String
        A title for the plot, optional.
    max_comp : int
        Maximum number of components to plot, default no limit.
    leg_loc : String
        Location for legend on the plot using matplotlib arguments, default 'best'
    figsize : tuple
        Tuple of width and height for figure, optional.
    
Plot variance explained and cumulative variance explained for PCA.
Citation: code modified from:
Sebastian Raschka. 2015. Python Machine Learning. Packt Publishing.
Plot variance explained and cumulative variance explained for PCA . Citation : code modified from : Sebastian Raschka . 2015 . Python Machine Learning . Packt Publishing .
def variance_explained_plot(pca, pca_null=None, title=None, max_comp=None,
                            leg_loc='best', figsize=None):
    colors = ['#1b9e77', '#d95f02', '#7570b3', '#e7298a', '#66a61e', '#e6a602',
              '#a6761d', '#666666', '#000000']
    fig, ax = plt.subplots()
    if figsize is not None:
        fig.set_size_inches(figsize)
    if title is not None:
        fig.suptitle(title)
    if max_comp is None:
        max_comp = len(pca.explained_variance_ratio_)
    var_exp = pca.explained_variance_ratio_[:max_comp]
    cum_var_exp = np.cumsum(var_exp)
    component_indices = range(1,max_comp+1)
    if pca_null is not None:
            var_exp_null = pca_null.explained_variance_ratio_[:max_comp]
            cum_var_exp_null = np.cumsum(var_exp_null)
            component_indices_null = range(1,max_comp+1)
    ax.bar(component_indices, var_exp, align='edge', width=-0.4,
           label='comp.', color=colors[0])
    ax.step(component_indices, cum_var_exp, where='mid', label='cum.',
            color=colors[0])
    if pca_null is not None:
            ax.bar(component_indices_null, var_exp_null, align='edge',
                   width=0.4, label='comp, null',
                   color=colors[1])
            ax.step(component_indices_null, cum_var_exp_null, where='mid',
                    label='cum, null', color=colors[1])
    ax.set_xlabel('Principal component index')
    ax.set_xticks(component_indices)
    ax.set_ylabel('Explained variance ratio')
    ax.legend(loc=leg_loc)
    plt.show()
def variance_explained_plot ( pca , pca_null = None , title = None , max_comp = None , leg_loc = 'best' , figsize = None ) : colors = [ '#1b9e77' , '#d95f02' , '#7570b3' , '#e7298a' , '#66a61e' , '#e6a602' , '#a6761d' , '#666666' , '#000000' ] fig , ax = plt . subplots ( ) if figsize is not None : fig . set_size_inches ( figsize ) if title is not None : fig . suptitle ( title ) if max_comp is None : max_comp = len ( pca . explained_variance_ratio_ ) var_exp = pca . explained_variance_ratio_ [ : max_comp ] cum_var_exp = np . cumsum ( var_exp ) component_indices = range ( 1 , max_comp + 1 ) if pca_null is not None : var_exp_null = pca_null . explained_variance_ratio_ [ : max_comp ] cum_var_exp_null = np . cumsum ( var_exp_null ) component_indices_null = range ( 1 , max_comp + 1 ) ax . bar ( component_indices , var_exp , align = 'edge' , width = - 0.4 , label = 'comp.' , color = colors [ 0 ] ) ax . step ( component_indices , cum_var_exp , where = 'mid' , label = 'cum.' , color = colors [ 0 ] ) if pca_null is not None : ax . bar ( component_indices_null , var_exp_null , align = 'edge' , width = 0.4 , label = 'comp, null' , color = colors [ 1 ] ) ax . step ( component_indices_null , cum_var_exp_null , where = 'mid' , label = 'cum, null' , color = colors [ 1 ] ) ax . set_xlabel ( 'Principal component index' ) ax . set_xticks ( component_indices ) ax . set_ylabel ( 'Explained variance ratio' ) ax . legend ( loc = leg_loc ) plt . show ( )
Plot variance explained and cumulative variance explained for PCA.
Plot variance explained and cumulative variance explained for PCA .
"""
    Plot variance explained and cumulative variance explained for PCA.
    Citation: code modified from:
    Sebastian Raschka. 2015. Python Machine Learning. Packt Publishing.

    Parameters
    ----------
    pca : sklearn PCA object
        A fitted PCA object from sklearn.
    pca_null : sklearn PCA object
        A PCA object fitted on an uncorrelated null dataset, optional.
    title : String
        A title for the plot, optional.
    max_comp : int
        Maximum number of components to plot, default no limit.
    leg_loc : String
        Location for legend on the plot using matplotlib arguments, default 'best'
    figsize : tuple
        Tuple of width and height for figure, optional.
    """
pca
A fitted PCA object from sklearn.
A fitted PCA object from sklearn .
pca_null
A PCA object fitted on an uncorrelated null dataset, optional.
A PCA object fitted on an uncorrelated null dataset optional .
title
A title for the plot, optional.
A title for the plot optional .
max_comp
Maximum number of components to plot, default no limit.
Maximum number of components to plot default no limit .
no
leg_loc
Location for legend on the plot using matplotlib arguments, default 'best'
Location for legend on the plot using matplotlib arguments default ' best '
figsize
Tuple of width and height for figure, optional.
Tuple of width and height for figure optional .