Esempio n. 1
0
def gscv_line_plot(x, y, gscv, title=None, directory=None, filename=None):
    """Creates line plot from dictionary of GridSearchCV objects."""
    # Extract data from dictionary of GridSearchCV objects
    data = pd.DataFrame()
    for name, gs in gscv.items():
        results = pd.DataFrame(data=gs.cv_results_)
        x_data = results.filter(like=x, axis=1).values.flatten()
        y_data = results.filter(like=y, axis=1).values.flatten()
        df = pd.DataFrame({
            proper(x): x_data,
            proper(y): y_data,
            'Model': name
        })
        data = pd.concat([data, df], axis=0)
    if title is None:
        title = 'Gridsearch Cross-validation Plot'
    # Initialize and render plot
    fig, ax = _init_image(x, y)
    ax = sns.lineplot(x=proper(x), y=proper(y), hue='Model', data=data, ax=ax)
    # Save figure if requested
    if directory is not None:
        title = title.replace('\n', ' ')
        save_plot(fig, directory=directory, filename=filename, title=title)
    # Show plot
    fig.tight_layout()
    plt.show()
Esempio n. 2
0
def _init_image(x, y, figsize=(12, 4), xlim=None, ylim=None, title=None, log=False):
    """Creates and sets the axis aesthetics, labels, scale, and limits."""

    # Initialize plot
    fig, ax = plt.subplots(figsize=figsize)
    # Set aesthetics
    sns.set(style="whitegrid", font_scale=1)
    ax.set_facecolor('w')
    ax.tick_params(colors='k')
    ax.xaxis.label.set_color('k')
    ax.yaxis.label.set_color('k')
    ax.set_title(title, color='k')
    # Set labels
    ax.set_xlabel(proper(x))
    ax.set_ylabel(proper(y))
    # Change to log scale if requested
    if log:
        ax.set_xscale('log')
    # Set x and y axis limits
    if xlim:
        ax.set_xlim(xlim)
    if ylim:
        ax.set_ylim(ylim)

    return fig, ax
        

        
         
Esempio n. 3
0
def _plot_train_val_score(model, title=None, figsize=(12,4)):
    """Plots training and validation score on single plot."""
    # Format plot title
    if title is None:
        title = model.history.params.get('name') + "\n" + \
            "Training and Validation Scores" +\
            '\n' + proper(model.metric)    
    # Extract training and validation score                    
    d = {'Epoch': model.history.epoch_log['epoch'],
            'Training': model.history.epoch_log['train_score'],
            'Validation': model.history.epoch_log.get('val_score')}
    df = pd.DataFrame(data=d)
    df = pd.melt(df, id_vars='Epoch', value_vars=['Training',
                                                'Validation'],
                var_name=['Dataset'], value_name='Score')  
    # Extract row with best score by dataset for scatterplot
    if RegressionMetricFactory()(model.metric).mode == 'max': 
        best_score = df.loc[df.groupby('Dataset').Score.idxmax()]
    else:
        best_score = df.loc[df.groupby('Dataset').Score.idxmin()]    
    # Initialize figure and axes with appropriate figsize and title
    fig, ax = _init_image(x='Epoch', y='Score', figsize=figsize,
                                    title=title)
    # Render score lineplot
    ax = sns.lineplot(x='Epoch', y='Score', hue='Dataset', data=df, 
                        legend='full', ax=ax)
    # Render scatterplot showing minimum score points
    ax = sns.scatterplot(x='Epoch', y='Score', hue='Dataset', 
                            data=best_score, legend=False, ax=ax)
    return fig, ax, title
Esempio n. 4
0
def _plot_train_score(model, title=None, figsize=(12,4)):
    """Plots training score."""
    if title is None:
        title = model.history.params.get('name') + "\n" + \
            "Training Scores" +\
            '\n' + proper(model.metric)    
    # Extract training score                    
    d = {'Epoch': model.history.epoch_log['epoch'],
            'Score': model.history.epoch_log['train_score']}
    df = pd.DataFrame(data=d)
    # Extract row with best score for scatterplot    
    if RegressionMetricFactory()(model.metric).mode == 'max': 
        best_score = df.loc[df.Score.idxmax()]
    else:
        best_score = df.loc[df.Score.idxmin()]    
    best_score = pd.DataFrame({"Epoch": [best_score['Epoch']],
                             "Score": [best_score['Score']]})        
    # Initialize figure and axes with appropriate figsize and title
    fig, ax = _init_image(x='Epoch', y='Score', figsize=figsize,
                                    title=title)
    # Render score lineplot
    ax = sns.lineplot(x='Epoch', y='Score', data=df, ax=ax)
    # Render scatterplot showing minimum score points
    ax = sns.scatterplot(x='Epoch', y='Score', data=best_score, ax=ax)
    return fig, ax, title    
Esempio n. 5
0
def plot_loss(model,
              title=None,
              figsize=(12, 4),
              directory=None,
              filename=None):
    """Plots training loss (and optionally validation loss) by epoch."""
    # Validate request
    if not isinstance(model, Estimator):
        raise ValueError("Model is not a valid Estimator or subclass object.")
    if not isinstance(figsize, tuple):
        raise TypeError("figsize is not a valid tuple.")

    # Format plot title
    if title is None:
        title = model.history.params.get('name') + "\n" + \
            "Training Plot with Learning Rate" +\
            '\n' + proper(model.history.params.get('cost')) + " Cost"

    # If val loss is on the log, plot both training and validation loss
    if 'val_cost' in model.history.epoch_log:
        fig, _ = _plot_train_val_loss(model, title=title, figsize=figsize)
    else:
        fig, _ = _plot_train_loss(model, title=title, figsize=figsize)

    # Save figure if directory is not None
    if directory is not None:
        title = title.replace('\n') + '.png'
        save_plot(fig, directory, filename, title)

    # Show plot
    fig.tight_layout()
    plt.show()
Esempio n. 6
0
    def _histogram(self, variable):
        """Prints and optionally saves individual histogram."""

        # Designate whether the probability density should be rendered
        histnorm = None
        if self.density:
            histnorm='probability'

        # Obtain the histogram object
        data=[go.Histogram(x=self.df[variable], 
                         histnorm=histnorm,
                         nbinsx=100,
                         autobinx=True)]

        # Specify the layout.
        layout = go.Layout(
            height=self.height,
            width=self.width,
            template=self.template
        )

        self.fig = go.Figure(data=data, layout=layout)

        # Format the title and update trace and layout
        if self.title is None:
            title = "Histogram : " + proper(variable) 
        else:
            title = self.title
            text = "Histogram : " + proper(variable)
            x = 0.5
            y = 1.10
            self.fig.update_layout(annotations = [dict(text=text, x=x, y=y,
                                                  xref='paper', yref='paper',
                                                  showarrow=False)])

        # Update trace and layout
        self.fig.update_layout(title=title)
Esempio n. 7
0
def plot_score(model,
               title=None,
               figsize=(12, 4),
               directory=None,
               filename=None):
    """Plots training score (and optionally validation score) by epoch."""

    # Validate request
    if not model.history.early_stop:
        raise Exception(
            "No early stop callback designated, so no score available.")
    elif not model.history.early_stop.metric:
        raise Exception("No metric designated for score.")
    if not isinstance(model, Estimator):
        raise ValueError("Model is not a valid Estimator or subclass object.")
    if not isinstance(figsize, tuple):
        raise TypeError("figsize is not a valid tuple.")

    # Format plot title
    if title is None:
        title = model.history.params.get('name') + "\n" + \
            "Evaluation Plot with Learning Rate" +\
            '\n' + proper(model.history.early_stop.metric) + " Score"

    # If val score is on the log, plot both training and validation score
    if 'val_score' in model.history.epoch_log:
        fig, _ = _plot_train_val_score(model, title=title, figsize=figsize)
    else:
        fig, _ = _plot_train_score(model, title=title, figsize=figsize)

    # Save figure if directory is not None
    if directory is not None:
        title = title.replace('\n', ' ')
        save_plot(fig, directory, filename, title)

    # Show plot
    fig.tight_layout()
    plt.show()