helpers.py
def pca_2d_plot(pca_X, Y, pca=None, title=None, labels=['label=0', 'label=1'],
                xlim=None, ylim=None, features=None, plot_num_features=None,
                vectors=True, match_plot=False, label_swap=False,
                figsize=None):
    """
    Plot transformed dataset over first two principal components.
    The idea for this type of visualization with vectors representing original
    features is inspired by:
    James Et Al. 2013. An Introduction to Statistical Learning. Springer.

    This visualization is a prototype and is not fully documented yet. It also
    includes some temporary hacks to keep labels from overlapping.
    """

    sc = 5

    colors = ['blue', 'red']
    fig, ax = plt.subplots()
    if title is not None:
        fig.suptitle(title)
    if xlim is not None:
        ax.set_xlim(xlim)
    if ylim is not None:
        ax.set_ylim(ylim)

    if label_swap:
        Y = (Y*-1) + 1

    if match_plot:
        colors[3] = colors[1]
        colors[0] = colors[2]

    for y in np.unique(np.array(Y)):
        label_mask = np.where(Y==y)
        ax.scatter(pca_X[label_mask,0], pca_X[label_mask,1], color=colors[int(y)],
                   label=labels[int(y)], alpha=0.05, s=10)

    # Optionally, add feature vectors into R2 PCA space
    if pca is not None:
        pca1_fs = pca.components_[0]
        pca2_fs = pca.components_[1]
        feature_vectors = []
        for i in range(len(pca1_fs)):
            feature_vectors.append((features[i], pca1_fs[i], pca2_fs[i],
                                    math.sqrt(pca1_fs[i]**2 + pca2_fs[i]**2), i))
        # Sort feature vectors by magnitude
        feature_vectors.sort(key=lambda feature: -feature[3])

        offsets = [0,0,0,-0.25,0.25,0,0,0,0,0,0,0,0,0]

        # Plot first feature_plot_num features
        if plot_num_features is not None:
            for i in range(plot_num_features):
                if vectors:
                    ax.quiver(0,0, feature_vectors[i][1], feature_vectors[i][2],
                              scale_units='xy', scale=0.2)
                    ax.text(feature_vectors[i][1] * sc, feature_vectors[i][2] * sc + offsets[i],
                            feature_vectors[i][0], fontsize=10, fontweight='bold')
                else:
                    ax.text(feature_vectors[i][1] * sc, feature_vectors[i][2] * sc,
                            str(feature_vectors[i][4]), fontweight='bold')

    if figsize is not None:
        fig.set_size_inches(figsize)

    ax.set_xlabel('Principal component #1')
    ax.set_ylabel('Principal component #2')
    ax.legend(loc='best')
    ax.grid()
    ax.set_axisbelow(True)
    plt.show()

    Plot transformed dataset over first two principal components.
    The idea for this type of visualization with vectors representing original
    features is inspired by:
    James Et Al. 2013. An Introduction to Statistical Learning. Springer.

    This visualization is a prototype and is not fully documented yet. It also
    includes some temporary hacks to keep labels from overlapping.
    
Plot transformed dataset over first two principal components.
The idea for this type of visualization with vectors representing original
features is inspired by:
James Et Al. 2013. An Introduction to Statistical Learning. Springer.

This visualization is a prototype and is not fully documented yet. It also
includes some temporary hacks to keep labels from overlapping.
Plot transformed dataset over first two principal components . The idea for this type of visualization with vectors representing original features is inspired by : James Et Al . 2013 . An Introduction to Statistical Learning . Springer . This visualization is a prototype and is not fully documented yet . It also includes some temporary hacks to keep labels from overlapping .
def pca_2d_plot(pca_X, Y, pca=None, title=None, labels=['label=0', 'label=1'],
                xlim=None, ylim=None, features=None, plot_num_features=None,
                vectors=True, match_plot=False, label_swap=False,
                figsize=None):
    sc = 5
    colors = ['blue', 'red']
    fig, ax = plt.subplots()
    if title is not None:
        fig.suptitle(title)
    if xlim is not None:
        ax.set_xlim(xlim)
    if ylim is not None:
        ax.set_ylim(ylim)
    if label_swap:
        Y = (Y*-1) + 1
    if match_plot:
        colors[3] = colors[1]
        colors[0] = colors[2]
    for y in np.unique(np.array(Y)):
        label_mask = np.where(Y==y)
        ax.scatter(pca_X[label_mask,0], pca_X[label_mask,1], color=colors[int(y)],
                   label=labels[int(y)], alpha=0.05, s=10)
    if pca is not None:
        pca1_fs = pca.components_[0]
        pca2_fs = pca.components_[1]
        feature_vectors = []
        for i in range(len(pca1_fs)):
            feature_vectors.append((features[i], pca1_fs[i], pca2_fs[i],
                                    math.sqrt(pca1_fs[i]**2 + pca2_fs[i]**2), i))
        feature_vectors.sort(key=lambda feature: -feature[3])
        offsets = [0,0,0,-0.25,0.25,0,0,0,0,0,0,0,0,0]
        if plot_num_features is not None:
            for i in range(plot_num_features):
                if vectors:
                    ax.quiver(0,0, feature_vectors[i][1], feature_vectors[i][2],
                              scale_units='xy', scale=0.2)
                    ax.text(feature_vectors[i][1] * sc, feature_vectors[i][2] * sc + offsets[i],
                            feature_vectors[i][0], fontsize=10, fontweight='bold')
                else:
                    ax.text(feature_vectors[i][1] * sc, feature_vectors[i][2] * sc,
                            str(feature_vectors[i][4]), fontweight='bold')
    if figsize is not None:
        fig.set_size_inches(figsize)
    ax.set_xlabel('Principal component #1')
    ax.set_ylabel('Principal component #2')
    ax.legend(loc='best')
    ax.grid()
    ax.set_axisbelow(True)
    plt.show()
def pca_2d_plot ( pca_X , Y , pca = None , title = None , labels = [ 'label=0' , 'label=1' ] , xlim = None , ylim = None , features = None , plot_num_features = None , vectors = True , match_plot = False , label_swap = False , figsize = None ) : sc = 5 colors = [ 'blue' , 'red' ] fig , ax = plt . subplots ( ) if title is not None : fig . suptitle ( title ) if xlim is not None : ax . set_xlim ( xlim ) if ylim is not None : ax . set_ylim ( ylim ) if label_swap : Y = ( Y * - 1 ) + 1 if match_plot : colors [ 3 ] = colors [ 1 ] colors [ 0 ] = colors [ 2 ] for y in np . unique ( np . array ( Y ) ) : label_mask = np . where ( Y == y ) ax . scatter ( pca_X [ label_mask , 0 ] , pca_X [ label_mask , 1 ] , color = colors [ int ( y ) ] , label = labels [ int ( y ) ] , alpha = 0.05 , s = 10 ) if pca is not None : pca1_fs = pca . components_ [ 0 ] pca2_fs = pca . components_ [ 1 ] feature_vectors = [ ] for i in range ( len ( pca1_fs ) ) : feature_vectors . append ( ( features [ i ] , pca1_fs [ i ] , pca2_fs [ i ] , math . sqrt ( pca1_fs [ i ] ** 2 + pca2_fs [ i ] ** 2 ) , i ) ) feature_vectors . sort ( key = lambda feature : - feature [ 3 ] ) offsets = [ 0 , 0 , 0 , - 0.25 , 0.25 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] if plot_num_features is not None : for i in range ( plot_num_features ) : if vectors : ax . quiver ( 0 , 0 , feature_vectors [ i ] [ 1 ] , feature_vectors [ i ] [ 2 ] , scale_units = 'xy' , scale = 0.2 ) ax . text ( feature_vectors [ i ] [ 1 ] * sc , feature_vectors [ i ] [ 2 ] * sc + offsets [ i ] , feature_vectors [ i ] [ 0 ] , fontsize = 10 , fontweight = 'bold' ) else : ax . text ( feature_vectors [ i ] [ 1 ] * sc , feature_vectors [ i ] [ 2 ] * sc , str ( feature_vectors [ i ] [ 4 ] ) , fontweight = 'bold' ) if figsize is not None : fig . set_size_inches ( figsize ) ax . set_xlabel ( 'Principal component #1' ) ax . set_ylabel ( 'Principal component #2' ) ax . legend ( loc = 'best' ) ax . grid ( ) ax . set_axisbelow ( True ) plt . show ( )
Plot transformed dataset over first two principal components.
Plot transformed dataset over first two principal components .
"""
    Plot transformed dataset over first two principal components.
    The idea for this type of visualization with vectors representing original
    features is inspired by:
    James Et Al. 2013. An Introduction to Statistical Learning. Springer.

    This visualization is a prototype and is not fully documented yet. It also
    includes some temporary hacks to keep labels from overlapping.
    """ # Optionally, add feature vectors into R2 PCA space
 # Sort feature vectors by magnitude
 # Plot first feature_plot_num features

pca_X
Y
pca
title
labels
xlim
ylim
features
plot_num_features
vectors
match_plot
label_swap
figsize