cytometer/utils.py
def plot_confusion_matrix(y_true, y_pred,
                          normalize=False,
                          title=None,
                          xlabel=None,
                          ylabel=None,
                          cmap=plt.cm.Blues,
                          colorbar=True):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.

    Copied from https://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html#sphx-glr-auto-examples-model-selection-plot-confusion-matrix-py
    on 27 Mar 2019. Small modifications to allow input xlabel, ylabel.
    """
    if not title:
        if normalize:
            title = 'Normalized confusion matrix'
        else:
            title = 'Confusion matrix, without normalization'

    # Compute confusion matrix
    classes = np.unique(np.concatenate((y_true, y_pred)))
    cm = confusion_matrix(y_true, y_pred, labels=classes)
    # Only use the labels that appear in the data
    # classes = classes[unique_labels(y_true, y_pred)]
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    print(cm)

    if xlabel is None:
        xlabel = 'Predicted label'
    if ylabel is None:
        ylabel = 'True label'

    fig, ax = plt.subplots()
    im = ax.imshow(cm, interpolation='nearest', cmap=cmap)
    if colorbar:
        cb = ax.figure.colorbar(im, ax=ax)
        for cbi in cb.ax.yaxis.get_ticklabels():
            cbi.set_size(14)
    # We want to show all ticks...
    ax.set(xticks=np.arange(cm.shape[1]),
           yticks=np.arange(cm.shape[0]),
           # ... and label them with the respective list entries
           xticklabels=classes, yticklabels=classes)
    plt.title(title, fontsize=16)
    plt.xlabel(xlabel, fontsize=14)
    plt.ylabel(ylabel, fontsize=14)

    # Rotate the tick labels and set their alignment.
    plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
             rotation_mode="anchor")
    plt.tick_params(axis='both', which='major', labelsize=14)

    # Loop over data dimensions and create text annotations.
    fmt = '.2f' if normalize else 'd'
    thresh = cm.max() / 2.
    for i in range(cm.shape[0]):
        for j in range(cm.shape[1]):
            ax.text(j, i, format(cm[i, j], fmt),
                    ha="center", va="center",
                    color="white" if cm[i, j] > thresh else "black",
                    fontsize=14)
    fig.tight_layout()
    return ax

    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.

    Copied from https://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html#sphx-glr-auto-examples-model-selection-plot-confusion-matrix-py
    on 27 Mar 2019. Small modifications to allow input xlabel, ylabel.
    
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.


This function prints and plots the confusion matrix . Normalization can be applied by setting ` normalize = True ` .
def plot_confusion_matrix(y_true, y_pred,
                          normalize=False,
                          title=None,
                          xlabel=None,
                          ylabel=None,
                          cmap=plt.cm.Blues,
                          colorbar=True):
    if not title:
        if normalize:
            title = 'Normalized confusion matrix'
        else:
            title = 'Confusion matrix, without normalization'
    classes = np.unique(np.concatenate((y_true, y_pred)))
    cm = confusion_matrix(y_true, y_pred, labels=classes)
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')
    print(cm)
    if xlabel is None:
        xlabel = 'Predicted label'
    if ylabel is None:
        ylabel = 'True label'
    fig, ax = plt.subplots()
    im = ax.imshow(cm, interpolation='nearest', cmap=cmap)
    if colorbar:
        cb = ax.figure.colorbar(im, ax=ax)
        for cbi in cb.ax.yaxis.get_ticklabels():
            cbi.set_size(14)
    ax.set(xticks=np.arange(cm.shape[1]),
           yticks=np.arange(cm.shape[0]),
           xticklabels=classes, yticklabels=classes)
    plt.title(title, fontsize=16)
    plt.xlabel(xlabel, fontsize=14)
    plt.ylabel(ylabel, fontsize=14)
    plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
             rotation_mode="anchor")
    plt.tick_params(axis='both', which='major', labelsize=14)
    fmt = '.2f' if normalize else 'd'
    thresh = cm.max() / 2.
    for i in range(cm.shape[0]):
        for j in range(cm.shape[1]):
            ax.text(j, i, format(cm[i, j], fmt),
                    ha="center", va="center",
                    color="white" if cm[i, j] > thresh else "black",
                    fontsize=14)
    fig.tight_layout()
    return ax
def plot_confusion_matrix ( y_true , y_pred , normalize = False , title = None , xlabel = None , ylabel = None , cmap = plt . cm . Blues , colorbar = True ) : if not title : if normalize : title = 'Normalized confusion matrix' else : title = 'Confusion matrix, without normalization' classes = np . unique ( np . concatenate ( ( y_true , y_pred ) ) ) cm = confusion_matrix ( y_true , y_pred , labels = classes ) if normalize : cm = cm . astype ( 'float' ) / cm . sum ( axis = 1 ) [ : , np . newaxis ] print ( "Normalized confusion matrix" ) else : print ( 'Confusion matrix, without normalization' ) print ( cm ) if xlabel is None : xlabel = 'Predicted label' if ylabel is None : ylabel = 'True label' fig , ax = plt . subplots ( ) im = ax . imshow ( cm , interpolation = 'nearest' , cmap = cmap ) if colorbar : cb = ax . figure . colorbar ( im , ax = ax ) for cbi in cb . ax . yaxis . get_ticklabels ( ) : cbi . set_size ( 14 ) ax . set ( xticks = np . arange ( cm . shape [ 1 ] ) , yticks = np . arange ( cm . shape [ 0 ] ) , xticklabels = classes , yticklabels = classes ) plt . title ( title , fontsize = 16 ) plt . xlabel ( xlabel , fontsize = 14 ) plt . ylabel ( ylabel , fontsize = 14 ) plt . setp ( ax . get_xticklabels ( ) , rotation = 45 , ha = "right" , rotation_mode = "anchor" ) plt . tick_params ( axis = 'both' , which = 'major' , labelsize = 14 ) fmt = '.2f' if normalize else 'd' thresh = cm . max ( ) / 2. for i in range ( cm . shape [ 0 ] ) : for j in range ( cm . shape [ 1 ] ) : ax . text ( j , i , format ( cm [ i , j ] , fmt ) , ha = "center" , va = "center" , color = "white" if cm [ i , j ] > thresh else "black" , fontsize = 14 ) fig . tight_layout ( ) return ax
This function prints and plots the confusion matrix.
This function prints and plots the confusion matrix .
"""
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.

    Copied from https://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html#sphx-glr-auto-examples-model-selection-plot-confusion-matrix-py
    on 27 Mar 2019. Small modifications to allow input xlabel, ylabel.
    """ # Compute confusion matrix # Only use the labels that appear in the data # classes = classes[unique_labels(y_true, y_pred)] # We want to show all ticks... # ... and label them with the respective list entries # Rotate the tick labels and set their alignment. # Loop over data dimensions and create text annotations.
y_true
y_pred
normalize
title
xlabel
ylabel
cmap
colorbar