helpers.py
def complexity_curves(gs, primary_hp, log_scale=False, plot_stdev=False,
                      response='scores', title=None, show_err=False, ylim=None,
                      figsize=(5,5), leg_outside=True):
    """
    Plot model training and validation curves from results of a GridSearchCV
    object.

    Parameters
    ----------
    gs : dict
        A fitted gridsearch object.

    title : string, optional
        Title for the chart.
    """
    colors = ['#1b9e77', '#d95f02', '#7570b3', '#e7298a', '#66a61e', '#e6a602',
              '#a6761d', '#666666']

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

    if title is not None:
        ax.set_title(title)

    if ylim is not None:
        ax.set_ylim(0,ylim)

    gs_df = pd.DataFrame(copy.deepcopy(gs.cv_results_))

    # Separate parameters for neural network
    def count_layers(params_dict):
        params_dict['num_layers'] = len(params_dict['hidden_layer_sizes'])
        params_dict['hidden_layer_sizes'] = params_dict['hidden_layer_sizes'][0]
    if primary_hp == 'hidden_layer_sizes':
        gs_df['params'].apply(count_layers)
        gs_df['param_hidden_layer_sizes'] = gs_df['param_hidden_layer_sizes'].map(lambda tup : tup[0])

    # Handle case with only one varying hyperparameter
    if len(gs_df['params'][0]) == 1:
        x = gs_df['param_'+primary_hp]
        if log_scale == True:
            x = np.log(x.astype('float'))
        train = gs_df['mean_train_score']
        validation = gs_df['mean_test_score']

        if plot_stdev==True:
            train_err = gs_df['std_train_score']
            validation_err = gs_df['std_test_score']
            ax.errorbar(x, train, train_err, color=colors[0], marker='o',
                        label='Train', linestyle='--', ecolor='k', capsize=4)
            ax.errorbar(x, validation, validation_err, color=colors[0], marker='o',
                        label='Validation', linestyle='-', ecolor='k', capsize=4)
        else:
            ax.plot(x, train, color=colors[0], marker='o',
                        label='Train', linestyle='--')
            ax.plot(x, validation, color=colors[0], marker='o',
                        label='Validation', linestyle='-')

    # Handle case with two varying hyperparameters
    else:
        gs_df['params'].apply(lambda dict : dict.pop(primary_hp))
        gs_df['params'] = gs_df['params'].map(lambda dict : tuple(dict.items()))
        for i, params in enumerate(gs_df['params'].unique()):
            x = gs_df['param_'+primary_hp][gs_df['params'] == params]
            if log_scale == True:
                x = np.log(x.astype('float'))
            train = gs_df['mean_train_score'][gs_df['params'] == params]
            validation = gs_df['mean_test_score'][gs_df['params'] == params]

            if plot_stdev==True:
                train_err = gs_df['std_train_score'][gs_df['params'] == params]
                validation_err = gs_df['std_test_score'][gs_df['params'] == params]
                ax.errorbar(x, train, train_err, color=colors[i], marker='o',
                            label='Train, '+str(params), linestyle='--', ecolor='k',
                            capsize=4)
                ax.errorbar(x, validation, validation_err, color=colors[i],
                            marker='o', label='Validation, ' + str(params),
                            linestyle='-', ecolor='k', capsize=4)
            else:
                ax.plot(x, train, color=colors[i], marker='o',
                            label='Train, ' + str(params), linestyle='--')
                ax.plot(x, validation, color=colors[i], marker='o',
                            label='Validation, ' + str(params), linestyle='-')
    if log_scale == False:
        ax.set_xlabel(primary_hp)
    else:
        ax.set_xlabel('log(' + primary_hp + ')')
    ax.set_ylabel('Score')
    ax.grid()
    if leg_outside:
        ax.legend(loc='upper left', bbox_to_anchor=(1, 1))
    else:
        ax.legend(loc='best')
    plt.show()

    Plot model training and validation curves from results of a GridSearchCV
    object.

    Parameters
    ----------
    gs : dict
        A fitted gridsearch object.

    title : string, optional
        Title for the chart.
    
Plot model training and validation curves from results of a GridSearchCV
object.
Plot model training and validation curves from results of a GridSearchCV object .
def complexity_curves(gs, primary_hp, log_scale=False, plot_stdev=False,
                      response='scores', title=None, show_err=False, ylim=None,
                      figsize=(5,5), leg_outside=True):
    colors = ['#1b9e77', '#d95f02', '#7570b3', '#e7298a', '#66a61e', '#e6a602',
              '#a6761d', '#666666']
    fig, ax = plt.subplots()
    if figsize is not None:
        fig.set_size_inches(figsize)
    if title is not None:
        ax.set_title(title)
    if ylim is not None:
        ax.set_ylim(0,ylim)
    gs_df = pd.DataFrame(copy.deepcopy(gs.cv_results_))
    def count_layers(params_dict):
        params_dict['num_layers'] = len(params_dict['hidden_layer_sizes'])
        params_dict['hidden_layer_sizes'] = params_dict['hidden_layer_sizes'][0]
    if primary_hp == 'hidden_layer_sizes':
        gs_df['params'].apply(count_layers)
        gs_df['param_hidden_layer_sizes'] = gs_df['param_hidden_layer_sizes'].map(lambda tup : tup[0])
    if len(gs_df['params'][0]) == 1:
        x = gs_df['param_'+primary_hp]
        if log_scale == True:
            x = np.log(x.astype('float'))
        train = gs_df['mean_train_score']
        validation = gs_df['mean_test_score']
        if plot_stdev==True:
            train_err = gs_df['std_train_score']
            validation_err = gs_df['std_test_score']
            ax.errorbar(x, train, train_err, color=colors[0], marker='o',
                        label='Train', linestyle='--', ecolor='k', capsize=4)
            ax.errorbar(x, validation, validation_err, color=colors[0], marker='o',
                        label='Validation', linestyle='-', ecolor='k', capsize=4)
        else:
            ax.plot(x, train, color=colors[0], marker='o',
                        label='Train', linestyle='--')
            ax.plot(x, validation, color=colors[0], marker='o',
                        label='Validation', linestyle='-')
    else:
        gs_df['params'].apply(lambda dict : dict.pop(primary_hp))
        gs_df['params'] = gs_df['params'].map(lambda dict : tuple(dict.items()))
        for i, params in enumerate(gs_df['params'].unique()):
            x = gs_df['param_'+primary_hp][gs_df['params'] == params]
            if log_scale == True:
                x = np.log(x.astype('float'))
            train = gs_df['mean_train_score'][gs_df['params'] == params]
            validation = gs_df['mean_test_score'][gs_df['params'] == params]
            if plot_stdev==True:
                train_err = gs_df['std_train_score'][gs_df['params'] == params]
                validation_err = gs_df['std_test_score'][gs_df['params'] == params]
                ax.errorbar(x, train, train_err, color=colors[i], marker='o',
                            label='Train, '+str(params), linestyle='--', ecolor='k',
                            capsize=4)
                ax.errorbar(x, validation, validation_err, color=colors[i],
                            marker='o', label='Validation, ' + str(params),
                            linestyle='-', ecolor='k', capsize=4)
            else:
                ax.plot(x, train, color=colors[i], marker='o',
                            label='Train, ' + str(params), linestyle='--')
                ax.plot(x, validation, color=colors[i], marker='o',
                            label='Validation, ' + str(params), linestyle='-')
    if log_scale == False:
        ax.set_xlabel(primary_hp)
    else:
        ax.set_xlabel('log(' + primary_hp + ')')
    ax.set_ylabel('Score')
    ax.grid()
    if leg_outside:
        ax.legend(loc='upper left', bbox_to_anchor=(1, 1))
    else:
        ax.legend(loc='best')
    plt.show()
def complexity_curves ( gs , primary_hp , log_scale = False , plot_stdev = False , response = 'scores' , title = None , show_err = False , ylim = None , figsize = ( 5 , 5 ) , leg_outside = True ) : colors = [ '#1b9e77' , '#d95f02' , '#7570b3' , '#e7298a' , '#66a61e' , '#e6a602' , '#a6761d' , '#666666' ] fig , ax = plt . subplots ( ) if figsize is not None : fig . set_size_inches ( figsize ) if title is not None : ax . set_title ( title ) if ylim is not None : ax . set_ylim ( 0 , ylim ) gs_df = pd . DataFrame ( copy . deepcopy ( gs . cv_results_ ) ) def count_layers ( params_dict ) : params_dict [ 'num_layers' ] = len ( params_dict [ 'hidden_layer_sizes' ] ) params_dict [ 'hidden_layer_sizes' ] = params_dict [ 'hidden_layer_sizes' ] [ 0 ] if primary_hp == 'hidden_layer_sizes' : gs_df [ 'params' ] . apply ( count_layers ) gs_df [ 'param_hidden_layer_sizes' ] = gs_df [ 'param_hidden_layer_sizes' ] . map ( lambda tup : tup [ 0 ] ) if len ( gs_df [ 'params' ] [ 0 ] ) == 1 : x = gs_df [ 'param_' + primary_hp ] if log_scale == True : x = np . log ( x . astype ( 'float' ) ) train = gs_df [ 'mean_train_score' ] validation = gs_df [ 'mean_test_score' ] if plot_stdev == True : train_err = gs_df [ 'std_train_score' ] validation_err = gs_df [ 'std_test_score' ] ax . errorbar ( x , train , train_err , color = colors [ 0 ] , marker = 'o' , label = 'Train' , linestyle = '--' , ecolor = 'k' , capsize = 4 ) ax . errorbar ( x , validation , validation_err , color = colors [ 0 ] , marker = 'o' , label = 'Validation' , linestyle = '-' , ecolor = 'k' , capsize = 4 ) else : ax . plot ( x , train , color = colors [ 0 ] , marker = 'o' , label = 'Train' , linestyle = '--' ) ax . plot ( x , validation , color = colors [ 0 ] , marker = 'o' , label = 'Validation' , linestyle = '-' ) else : gs_df [ 'params' ] . apply ( lambda dict : dict . pop ( primary_hp ) ) gs_df [ 'params' ] = gs_df [ 'params' ] . map ( lambda dict : tuple ( dict . items ( ) ) ) for i , params in enumerate ( gs_df [ 'params' ] . unique ( ) ) : x = gs_df [ 'param_' + primary_hp ] [ gs_df [ 'params' ] == params ] if log_scale == True : x = np . log ( x . astype ( 'float' ) ) train = gs_df [ 'mean_train_score' ] [ gs_df [ 'params' ] == params ] validation = gs_df [ 'mean_test_score' ] [ gs_df [ 'params' ] == params ] if plot_stdev == True : train_err = gs_df [ 'std_train_score' ] [ gs_df [ 'params' ] == params ] validation_err = gs_df [ 'std_test_score' ] [ gs_df [ 'params' ] == params ] ax . errorbar ( x , train , train_err , color = colors [ i ] , marker = 'o' , label = 'Train, ' + str ( params ) , linestyle = '--' , ecolor = 'k' , capsize = 4 ) ax . errorbar ( x , validation , validation_err , color = colors [ i ] , marker = 'o' , label = 'Validation, ' + str ( params ) , linestyle = '-' , ecolor = 'k' , capsize = 4 ) else : ax . plot ( x , train , color = colors [ i ] , marker = 'o' , label = 'Train, ' + str ( params ) , linestyle = '--' ) ax . plot ( x , validation , color = colors [ i ] , marker = 'o' , label = 'Validation, ' + str ( params ) , linestyle = '-' ) if log_scale == False : ax . set_xlabel ( primary_hp ) else : ax . set_xlabel ( 'log(' + primary_hp + ')' ) ax . set_ylabel ( 'Score' ) ax . grid ( ) if leg_outside : ax . legend ( loc = 'upper left' , bbox_to_anchor = ( 1 , 1 ) ) else : ax . legend ( loc = 'best' ) plt . show ( )
Plot model training and validation curves from results of a GridSearchCV
object.
Plot model training and validation curves from results of a GridSearchCV object .
"""
    Plot model training and validation curves from results of a GridSearchCV
    object.

    Parameters
    ----------
    gs : dict
        A fitted gridsearch object.

    title : string, optional
        Title for the chart.
    """ # Separate parameters for neural network
 # Handle case with only one varying hyperparameter
 # Handle case with two varying hyperparameters

gs
A fitted gridsearch object.
A fitted gridsearch object .
primary_hp
log_scale
plot_stdev
response
title
Title for the chart.
Title for the chart .
show_err
ylim
figsize
leg_outside