def chartData(data, keyword):
    #Takes input of the results of the fts search and the phrase that was searched for
    #and creates bar chart of occurences by book for the phrase
    from matplotlib import pyplot as plt
    import numpy as np
    import math
    
    data = sorted(data, key=lambda x: x[1], reverse=True)
    info = []#number of occurences 
    books = []#list of books
    for d in data:
        info.append(d[1])
        b = d[0]
        books.append(b[b.find('LIBER'):])#Use only book number for label since name is the same for all books
    
    #create chart
    plt.close()
    fig = plt.figure()
    width = 0.4
    ind = np.arange(len(books))
    plt.bar(ind, info, width=width)
    plt.xticks(ind + width/2., books)
    plt.yticks(np.arange(0,max(info)*2,math.ceil(max(info)/5)))
    plt.ylabel('Number of Occurences')
    plt.xlabel('Books')
    plt.title('Occurences of "' + keyword + '" by book in Curtius Rufus (Latin)')
    fig.autofmt_xdate()
    plt.show(block=False)#display plot, and continue with program
def plot_confusion_matrix(confusion_matrix,
                          class_labels,
                          normalize=False,
                          title='Confusion Matrix',
                          cmap=plt.cm.Blues):
    """ Code courtesy of Abinav Sagar: https://towardsdatascience.com/convolutional-neural-network-for-breast-cancer-classification-52f1213dcc9 """

    if normalize:
        confusion_matrix = confusion_matrix.astype(
            'float') / confusion_matrix.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    print(confusion_matrix)

    plt.imshow(confusion_matrix, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(class_labels))
    plt.xticks(tick_marks, class_labels, rotation=55)
    plt.yticks(tick_marks, class_labels)
    fmt = '.2f' if normalize else 'd'
    thresh = confusion_matrix.max() / 2.
    for i, j in itertools.product(range(confusion_matrix.shape[0]),
                                  range(confusion_matrix.shape[1])):
        plt.text(j,
                 i,
                 format(confusion_matrix[i, j], fmt),
                 horizontalalignment="center",
                 color="white" if confusion_matrix[i, j] > thresh else "black")

    plt.ylabel('True label')
    plt.xlabel('Predicted label')
    plt.tight_layout()
Exemplo n.º 3
0
def check_sanity():
    fig = plt.figure(figsize=[10, 5])
    # Axes for flower image
    ax = fig.add_axes([.5, .4, .5, .5])

    # Displaay image
    result = process_image('flowers/test/1/image_06743.jpg')
    ax = imshow(result, ax)
    ax.axis('off')
    index = 77
    ax.set_title(cat_to_name[str(index)])

    # Prediction of image
    predictions, classes = predict('flowers/test/1/image_06743.jpg',
                                   model,
                                   device=device)

    # Create bar graph
    # Axis x and y
    ax1 = fig.add_axes([0, -.4, .888, .888])

    # Classes probability
    y_pos = np.arange(len(classes))

    # Horizontal bar chart to see it better
    plt.barh(y_pos, predictions, align='center', alpha=0.5)
    plt.yticks(y_pos, classes)
    plt.xlabel('probabilities')
    plt.show()
Exemplo n.º 4
0
def plot_confusion_matrix(cls_pred, cls_true):
    # This is called from print_test_accuracy() below.

    # cls_pred is an array of the predicted class-number for
    # all images in the test-set.

    # Get the true classifications for the test-set.

    # Get the confusion matrix using sklearn.
    cm = confusion_matrix(y_true=cls_true,
                          y_pred=cls_pred)

    # Print the confusion matrix as text.
    print(cm)

    # Plot the confusion matrix as an image.
    plt.matshow(cm)

    # Make various adjustments to the plot.
    plt.colorbar()
    tick_marks = np.arange(labels_type)
    plt.xticks(tick_marks, range(labels_type))
    plt.yticks(tick_marks, range(labels_type))
    plt.xlabel('Predicted')
    plt.ylabel('True')

    # Ensure the plot is shown correctly with multiple plots
    # in a single Notebook cell.
    plt.show()
Exemplo n.º 5
0
def plotColorCodedNetworkSpikes(network):
    assert network is not None, "Network is not initialised! Visualising failed."
    import matplotlib as plt
    from NetworkBuilder import sameDisparityInd
    
    cellsOutSortedByDisp = []
    spikes = []
    for disp in range(0, maxDisparity+1):
        cellsOutSortedByDisp.append([network[x][2] for x in sameDisparityInd[disp]])
        spikes.append([x.getSpikes() for x in cellsOutSortedByDisp[disp]])
    
    sortedSpikes = sortSpikesByColor(spikes)
    print sortedSpikes
    framesOfSpikes = generateColoredFrames(sortedSpikes)
    print framesOfSpikes
    
    fig = plt.figure()
    
    initialData = createInitialisingDataColoredPlot()
    
    imNet = plt.imshow(initialData[0], c=initialData[1], cmap=plt.cm.coolwarm, interpolation='none', origin='upper')
    
    plt.xticks(range(0, dimensionRetinaX)) 
    plt.yticks(range(0, dimensionRetinaY))
    plt.title("Disparity Map {0}".format(disparity))
    args = (framesOfSpikes, imNet)
    anim = animation.FuncAnimation(fig, animate, fargs=args, frames=int(simulationTime)*10, interval=30)
          
    plt.show()
Exemplo n.º 6
0
def plotConfusionMatrix(lbllist, predlist, classes, type):
    confusionMatrix = confusion_matrix(lbllist, predlist)

    # print(confusionMatrix)

    plt.imshow(confusionMatrix, interpolation="nearest", cmap=plt.cm.Blues)
    if type == 'train':
        plt.title("Confusion matrix training")
    elif type == 'test':
        plt.title("Confusion matrix testing")
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    fmt = "d"
    thresh = confusionMatrix.max() / 2.
    for i, j in itertools.product(range(confusionMatrix.shape[0]),
                                  range(confusionMatrix.shape[1])):
        plt.text(j,
                 i,
                 format(confusionMatrix[i, j], fmt),
                 horizontalalignment="center",
                 color="white" if confusionMatrix[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel("True label")
    plt.xlabel("Predicted label")
    # plt.show()
    if type == 'train':
        plt.savefig(LOG_PATH + 'Confusion matrix training.png')
    elif type == 'test':
        plt.savefig(LOG_PATH + 'Confusion matrix testing.png')
    plt.close()
def plot_confusion_matrix(cm, classes, title='混淆矩阵', cmap=plt.cm.Greens):
    # imshow() 表示绘制并显示二维图 有18个参数
    # 参数1 X 混淆矩阵中显示的数值 二维数组
    # 参数2 cmap 颜色 plt.cm.Blues表示蓝色 plt.cm.Reds表示红色 plt.cm.Greens表示绿色
    # 参数5 interpolation 插值法 一般有如下值
    #     nearest 最近邻插值法
    #     bilinear 双线性插值法
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.rcParams['axes.unicode_minus'] = False
    plt.imshow(cm, cmap=cmap, interpolation="nearest")
    plt.title(title)  # 标题
    plt.colorbar()  # 显示颜色的进度条
    tick_marks = np.arange(2)  # [0 1]
    plt.xticks(tick_marks, classes)  # 对x轴上分类进行标记
    plt.yticks(tick_marks, classes)  # 对y轴上分类进行标记

    thresh = np.mean(cm)
    for i in range(2):
        for j in range(2):
            plt.text(i,
                     j,
                     cm[j][i],
                     horizontalalignment='center',
                     color='white' if cm[i][j] >= thresh else 'black')

    plt.xlabel('预测值')
    plt.ylabel('真实值')
Exemplo n.º 8
0
def plot_test_image(testX, image_index, predictions_array, true_binary_labels):
    """
        testX: this is the test dataset
        image_index: index of the image that we will plot from the test dataset
        predictions_array: it is the array that contains all the predictions of the test dataset as output of model.predict(testX)
        true_binary_labels: these are the true label expressed as INTEGER values. It does not work with hot-encoding and string labels. 
    """
    single_predictions_array, true_binary_label, test_image = predictions_array, true_binary_labels[
        image_index], testX[image_index]
    plt.grid(False)
    plt.xticks([])
    plt.yticks([])

    plt.imshow(test_image, cmap=plt.cm.binary)

    predicted_binary_label = np.argmax(predictions_array)
    #print ("predicted_binary_label:", predicted_binary_label)
    #print ("true_binary_label:",true_binary_label)

    if predicted_binary_label == true_binary_label:
        color = 'blue'
    else:
        color = 'red'

    plt.xlabel("predicted: {} {:2.0f}% (true: {})".format(
        predicted_binary_label, 100 * np.max(single_predictions_array),
        true_binary_label),
               color=color)
Exemplo n.º 9
0
def test_random_forest(T=1000):
    """
    Method for testing the random forest algorithm with bank dataset.
    Problem #2.2d in HW2 for CS3505
    T = number of times algorithm will be run
    """
    import matplotlib.pyplot as plt

    S_train = read_file('train.csv', "bank")
    S_train, medians, majority = process_bank_data(S_train, "train")
    S_test = read_file('test.csv', "bank")
    S_test, medians, _ = process_bank_data(S_test, "test", medians)
    master_list = create_attribute_dictionary("bank")


    for s in (2,4,6):
        training_errors = []
        testing_errors = []
        for i in range(T):
            ensemble = random_forest(S_train, master_list, i, s)
            training_errors.append(100 - test_ensemble(ensemble, S_train)*100)
            testing_errors.append(100 - test_ensemble(ensemble, S_test)*100)

        #plot the error rates versus the no of trees
        print("Sample size: " + str(s))
        plt.title('Error rates per no of trees\nRandom Forest')
        plt.xlabel('No. of Trees')
        plt.ylabel('Percentage Incorrect')
        plt.plot(training_errors, label="train")
        plt.plot(testing_errors, label="test")
        plt.legend(loc='lower right')
        plt.yticks(np.arange(0, 20, 5))
        plt.show()
Exemplo n.º 10
0
def musicType_plot(rap, randb, classical, indie, pop, df):
    plt.figure()

    # declaring testing data
    xs = [1, 2, 3, 4, 5]
    ys = [randb, rap, classical, pop, indie]

    # setting range
    xrng = np.arange(len(xs))
    yrng = np.arange(0, max(ys)+60, 50)

    #labeling data
    plt.xlabel('Music Type')
    plt.ylabel('Music volume')

    # spacing and declare bar chart
    plt.bar(xrng, ys, 0.45, align="center") 


    # labeling
    plt.xticks(xrng, ["randb", "rap", "classical", "pop", "indie"])
    plt.yticks(yrng)

    plt.grid(True)
    plt.show()
Exemplo n.º 11
0
def plot_feature_importances_cancer(model, cancer):
    n_features = cancer.data.shape[1]
    plt.barh(np.arange(n_features), model.feature_importances_, align='center')
    plt.yticks(np.arange(n_features), cancer.feature_names)
    plt.xlabel("Feature importance")
    plt.ylabel("Feature")
    plt.ylim(-1, n_features)
def plot_3df(
    df, plot_title, x_axis, y_axis, plot, save
):  # function for plotting high-dimension and low-dimension eigenvalues
    x1 = df[df.columns[0]]
    y1 = df[df.columns[1]]
    y2 = df[df.columns[2]]
    y3 = df[df.columns[3]]
    min1 = df[df.columns[1]].min()
    min2 = df[df.columns[2]].min()
    min3 = df[df.columns[3]].min()
    df_min = min(min1, min2, min3)
    plt.figure(figsize=(8, 8))
    plt.plot(x1, y1, color='red')
    plt.plot(x1, y2, color='blue')
    plt.plot(x1, y3, color='green')
    plt.grid(color='black', linestyle='-',
             linewidth=0.1)  # parameters for plot grid
    plt.xticks(np.arange(0,
                         max(x1) * 1.1,
                         int(max(x1) / 10)))  # adjusting the intervals to 250
    plt.yticks(np.arange(df_min * 0.9, 100, int(max(y1) / 10)))
    plt.title(plot_title).set_position([0.5, 1.05])
    plt.xlabel(x_axis)
    plt.ylabel(y_axis)
    plt.legend(loc='best')  # creating legend and placing in at the top right
    if save == 'yes':
        plt.savefig(plot_title)
    if plot == 'yes':
        plt.show()
    plt.close()
def print_save_all_mean_faces(x_class_mean, global_mean, show, save):
    rows = 4
    cols = 14
    index = 1
    font_size = 10
    plt.figure(figsize=(20, 10))
    plt.subplot(rows, cols,
                index), plt.imshow(np.reshape(global_mean, (46, 56)).T,
                                   cmap='gist_gray')
    title = str("Global Mean")
    plt.title(title, fontsize=font_size).set_position([0.5, 0.95]), plt.xticks(
        []), plt.yticks([])
    index = index + 1
    for i in range(0, x_class_mean.shape[1]):
        title = str("Class Mean " + str(i + 1))
        plt.subplot(rows, cols,
                    index), plt.imshow(np.reshape(x_class_mean[:, i],
                                                  (46, 56)).T,
                                       cmap='gist_gray')
        plt.title(title, fontsize=font_size).set_position(
            [0.5, 0.95]), plt.xticks([]), plt.yticks([])
        index = index + 1
    if show == 'yes':
        plt.show()
    if save == 'yes':
        plt.savefig('Global and Class Mean')
    plt.close()
Exemplo n.º 14
0
def plot_confusion_matrix(cm,
                          classes,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues,
                          filename='viz\\confusion_matrix.png'):
    plt.figure()
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]

    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j,
                 i,
                 cm[i, j],
                 horizontalalignment="center",
                 color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
    plt.savefig(filename)
Exemplo n.º 15
0
def plotRetinaSpikes(retina=None, label=""):
    
    assert retina is not None, "Network is not initialised! Visualising failed."
    import matplotlib.pyplot as plt
    from matplotlib import animation
    
    print "Visualising {0} Spikes...".format(label) 

    spikes = [x.getSpikes() for x in retina]
#     print spikes
    
    sortedSpikes = sortSpikes(spikes)
#     print sortedSpikes
    
    framesOfSpikes = generateFrames(sortedSpikes)
#     print framesOfSpikes
    
    x = range(0, dimensionRetinaX)
    y = range(0, dimensionRetinaY)
    from numpy import meshgrid
    rows, pixels = meshgrid(x,y)
    
    fig = plt.figure()
    
    initialData = createInitialisingData()
    
    imNet = plt.imshow(initialData, cmap='green', interpolation='none', origin='upper')
    
    plt.xticks(range(0, dimensionRetinaX)) 
    plt.yticks(range(0, dimensionRetinaY))
    args = (framesOfSpikes, imNet)
    anim = animation.FuncAnimation(fig, animate, fargs=args, frames=int(simulationTime)*10, interval=30)
          
    plt.show()
Exemplo n.º 16
0
def plot_confusion_matrix(cm,
                          classes,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print("Confusion Matrix, without normalization")
    print(cm)
    #imshow displays data as an image on a 2d master
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    #returns evenly spaced values with a given inteerval
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)
    fmt = '.2f' if normalize else 'd'
    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j,
                 i,
                 format(cm[i, j], fmt),
                 horizontalalignment='center',
                 color='white' if cm[i, j] > thresh else "black")
    plt.tight_layout()
    plt.ylabel('True Label')
    plt.xlabel('Predicted Label')
Exemplo n.º 17
0
def plot_confusion_matrix(cm, classes,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, 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)

    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, cm[i, j],
                 horizontalalignment="center",
                 color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
Exemplo n.º 18
0
def plot_sleep(label, pred, file_time, kfold, name):
    x = range(0, len(label))
    y = np.arange(5)
    y_stage = ["W", "REM", "N1", "N2", "N3"]
    plt.figure(figsize=(24, 8))
    plt.ylabel("Sleep Stage")
    plt.xlabel("Sleep Time")
    time_choice = np.array([
        s.decode('UTF-8')
        for s in file_time[0:len(file_time):int(len(file_time) / 10)]
    ])
    plt.xticks(range(0, len(file_time), int(len(file_time) / 10)),
               time_choice)  ##为了将坐标刻度设为字
    plt.yticks(y, y_stage)  ##为了将坐标刻度设为字
    # plot中参数的含义分别是横轴值,纵轴值,线的形状,颜色,透明度,线的宽度和标签
    plt.plot(x,
             pred,
             'r-',
             color='blue',
             alpha=1,
             linewidth=1,
             label='predict')
    plt.plot(x, label, 'r-', color='red', alpha=1, linewidth=1, label='label')
    plt.legend(loc='best')
    plt.savefig(sleepPicture_path + str(kfold) + "sdt" + str(name) +
                ".svg")  ##保存睡眠模型图文件
    plt.close()
Exemplo n.º 19
0
def plot_classification_report(cr,
                               title='Classification report ',
                               with_avg_total=False,
                               cmap=plt.cm.Blues):

    lines = cr.split('\n')

    classes = []
    plotMat = []
    for line in lines[2:(len(lines) - 3)]:
        #print(line)
        t = line.split()
        if len(t):
            classes.append(t[0])
            v = [float(x) for x in t[1:len(t) - 1]]
            print(v)
            plotMat.append(v)

    if with_avg_total:
        aveTotal = lines[len(lines) - 1].split()
        classes.append('avg/total')
        vAveTotal = [float(x) for x in t[1:len(aveTotal) - 1]]
        plotMat.append(vAveTotal)

    plt.imshow(plotMat, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    x_tick_marks = np.arange(3)
    y_tick_marks = np.arange(len(classes))
    plt.xticks(x_tick_marks, ['precision', 'recall', 'f1-score', 'support'],
               rotation=45)
    plt.yticks(y_tick_marks, classes)
    plt.tight_layout()
    plt.ylabel('Classes')
    plt.xlabel('Measures')
Exemplo n.º 20
0
def plot_df(
    df1, df2, plot_title, x_axis, y_axis, plot, save
):  # function for plotting high-dimension and low-dimension eigenvalues
    y1 = df1[df1.columns[1]]
    x1 = df1['M']
    y2 = df2[df2.columns[1]]
    x2 = df2['M']

    plt.figure(figsize=(8, 8))
    plt.plot(x1, y1, color='red')
    plt.plot(x2, y2, color='blue')
    plt.grid(color='black', linestyle='-',
             linewidth=0.1)  # parameters for plot grid
    plt.xticks(np.arange(0,
                         max(x1) * 1.1,
                         int(max(x1) / 10)))  # adjusting the intervals to 250
    plt.yticks(np.arange(0, max(y1) * 1.1, int(max(y1) / 10)))
    plt.title(plot_title).set_position([0.5, 1.05])
    plt.xlabel(x_axis)
    plt.ylabel(y_axis)
    plt.legend(loc='best')  # creating legend and placing in at the top right
    if save == 'yes':
        plt.savefig(plot_title)
    if plot == 'yes':
        plt.show()
    plt.close()
Exemplo n.º 21
0
def silhouette():
    if not os.path.exists("Stardust_results"):
        print(
            "The directory structure Stardust_results doest not exist. Please run run_stardust first"
        )
        sys.exit()
    if not os.path.exists("Stardust_results/analysis"):
        os.mkdir("Stardust_results/analysis")
    output_path = "Stardust_results/analysis/"
    from sklearn.metrics import silhouette_samples, silhouette_score
    data_df = pd.read_csv(
        'Stardust_results/visualization_output/3_pass/data.csv',
        delimiter=",",
        index_col=False)
    data_df.set_index('data', inplace=True)
    silhouette_avg = silhouette_score(data_df[['x', 'y']], data_df['cluster'])
    sample_silhouette_values = silhouette_samples(data_df[['x', 'y']],
                                                  data_df['cluster'])
    print("silhouette score ", silhouette_avg)

    y_lower = 10
    import matplotlib.cm as cm
    fig = plt.figure(figsize=(4, 7))
    n_clusters = len(list(data_df['cluster'].unique()))
    for i in range(n_clusters):
        # Aggregate the silhouette scores for samples belonging to
        # cluster i, and sort them
        ith_cluster_silhouette_values = \
            sample_silhouette_values[data_df['cluster'] == i]

        ith_cluster_silhouette_values.sort()

        size_cluster_i = ith_cluster_silhouette_values.shape[0]
        y_upper = y_lower + size_cluster_i

        color = cm.nipy_spectral(float(i) / n_clusters)
        plt.fill_betweenx(np.arange(y_lower, y_upper),
                          0,
                          ith_cluster_silhouette_values,
                          facecolor=color,
                          edgecolor=color,
                          alpha=0.7)

        # Label the silhouette plots with their cluster numbers at the middle
        plt.text(-0.05, y_lower + 0.5 * size_cluster_i, str(i))

        # Compute the new y_lower for next plot
        y_lower = y_upper + 10  # 10 for the 0 samples

    plt.title("The silhouette plot for the various clusters.")
    plt.xlabel("silhouette coefficient", fontsize=20)
    plt.ylabel("Cluster label", fontsize=20)
    plt.axvline(x=silhouette_avg, color="red", linestyle="--")

    plt.yticks([])  # Clear the yaxis labels / ticks
    plt.xticks([-0.1, 0, 0.2, 0.4, 0.6, 0.8, 1])
    sns.despine(bottom=False, left=False)
    fig.savefig(output_path + "/silhouette.pdf", bbox_inches='tight', dpi=600)
    fig.savefig(output_path + "/silhouette.png", bbox_inches='tight', dpi=600)
def plot_line(x, y, y_hat, line_color='blue'):
    # Plot outputs
    plt.scatter(x, y, color='black')
    plt.plot(x, y_hat, color=line_color, linewidth=3)
    plt.xticks(())
    plt.yticks(())

    plt.show()
Exemplo n.º 23
0
def plot_gallery(images, titles, h, w, n_row=3, n_col=4):
    """Helper function to plot a gallery of portraits"""
    pl.figure(figsize=(1.8 * n_col, 2.4 * n_row))
    pl.subplots_adjust(bottom=0, left=.01, right=.99, top=.90, hspace=.35)
    for i in range(n_row * n_col):
        pl.subplot(n_row, n_col, i + 1)
        pl.imshow(images[i].reshape((h, w)), cmap=pl.cm.gray)
        pl.title(titles[i], size=12)
        pl.xticks(())
        pl.yticks(())
Exemplo n.º 24
0
def plot_sample(x, title="", width=28, fName=None):
    fig = plt.figure()
    sample = x.reshape(width, width)
    # interploation can be 'nearest' to put grid at the center the pixels
    plt.imshow(sample, interpolation='None', cmap='gray')
    plt.title(title)
    plt.xticks([])
    plt.yticks([])
    if fName != None:
        savefig(fName, bbox_inches='tight')
    plt.show()
Exemplo n.º 25
0
def cm_plot(original_label, predict_label, kunm, pic=None):

    prec_score = precision_score(original_label, predict_label, average=None)
    recall = recall_score(original_label, predict_label, average=None)
    f1 = f1_score(original_label, predict_label, average=None)
    cm = confusion_matrix(original_label, predict_label)
    cm_new = np.empty(shape=[5, 5])
    for x in range(5):
        t = cm.sum(axis=1)[x]
        for y in range(5):
            cm_new[x][y] = round(cm[x][y] / t * 100, 2)
    plt.figure()
    plt.matshow(cm_new, cmap=plt.cm.Blues)
    plt.colorbar()
    x_numbers = []
    y_numbers = []
    cm_percent = []
    for x in range(5):
        y_numbers.append(cm.sum(axis=1)[x])
        x_numbers.append(cm.sum(axis=0)[x])
        for y in range(5):
            percent = format(cm_new[x, y] * 100 / cm_new.sum(axis=1)[x], ".2f")
            cm_percent.append(str(percent))
            plt.annotate(format(cm_new[x, y] * 100 / cm_new.sum(axis=1)[x],
                                ".2f"),
                         xy=(y, x),
                         horizontalalignment='center',
                         verticalalignment='center',
                         fontsize=10)
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
    plt.title('confusion matrix')

    y_stage = [
        "W\n(" + str(y_numbers[0]) + ")", "N1\n(" + str(y_numbers[1]) + ")",
        "N2\n(" + str(y_numbers[2]) + ")", "N3\n(" + str(y_numbers[3]) + ")",
        "REM\n(" + str(y_numbers[4]) + ")"
    ]
    x_stage = [
        "W\n(" + str(x_numbers[0]) + ")", "N1\n(" + str(x_numbers[1]) + ")",
        "N2\n(" + str(x_numbers[2]) + ")", "N3\n(" + str(x_numbers[3]) + ")",
        "REM\n(" + str(x_numbers[4]) + ")"
    ]
    y = [0, 1, 2, 3, 4]
    plt.xticks(y, x_stage)
    plt.yticks(y, y_stage)
    #sns.heatmap(cm_percent, fmt='g', cmap="Blues", annot=True, cbar=False, xticklabels=x_stage, yticklabels=y_stage)  # 画热力图,annot=True 代表 在图上显示 对应的值, fmt 属性 代表输出值的格式,cbar=False, 不显示 热力棒

    plt.savefig(savepath + "matrix" + str(kunm) + ".svg")
    #plt.show()
    plt.show()
    plt.close()
    # plt.savefig("/home/data_new/zhangyongqing/flx/pythoncode/"+str(knum)+"matrix.jpg")
    return kappa(cm), classification_report(original_label, predict_label)
Exemplo n.º 26
0
def plot_sample_from_dataset(images, labels,rows=5, colums=5, width=8,height=8):

  plt.figure(figsize=(width,height))
  for i in range(rows*colums):
      plt.subplot(rows,colums,i+1)
      plt.xticks([])
      plt.yticks([])
      plt.grid(False)
      plt.imshow(images[i], cmap=plt.cm.binary)
      plt.xlabel(labels[i])
  plt.show()
    def make_spider(self, df, row, title, color):
        """
            Function which draw the radar charts of the clusters
            obtained with K-Means
        """
        # number of variable
        categories = list(df)[1:]
        N = len(categories)

        # What will be the angle of each axis in the plot? (we divide the plot / number of variable)
        angles = [n / float(N) * 2 * pi for n in range(N)]
        angles += angles[:1]

        # Initialise the spider plot
        # fig, ax = plt.subplots(2, 2, row + 1, polar=True)
        fig = plt.figure()
        ax = fig.add_subplot(int(str(22) + str(row + 1)), polar=True)

        # If you want the first axis to be on top:
        ax.set_theta_offset(pi / 2)
        ax.set_theta_direction(-1)

        # Draw one axe per variable + add labels labels yet
        plt.xticks(angles[:-1], categories, color='black', size=8)

        # Draw ylabels
        ax.set_rlabel_position(0)
        plt.yticks([10, 20, 30], ["10", "20", "30"], color="black", size=7)
        plt.ylim(0, 40)

        # Ind1
        values = df.loc[row].drop('group').values.flatten().tolist()
        values += values[:1]
        ax.plot(angles, values, color=color, linewidth=2, linestyle='solid')
        ax.fill(angles, values, color=color, alpha=0.4)

        # Add a title
        # plt.title(title, size=11, color=color, y=1.1)

        # ------- PART 2: Apply to all individuals
        # initialize the figure
        my_dpi = 96
        plt.figure(figsize=(1000 / my_dpi, 1000 / my_dpi), dpi=my_dpi)

        # Create a color palette:
        my_palette = plt.cm.get_cmap("Set2", len(df.index))

        # Loop to plot
        for row in range(0, len(df.index)):
            make_spider(row=row,
                        title='group ' + df['group'][row],
                        color=my_palette(row))
        plt.show()
Exemplo n.º 28
0
def cm_plot(original_label, predict_label, kunm, savepath):

    prec_score = precision_score(original_label, predict_label, average=None)
    recall = recall_score(original_label, predict_label, average=None)
    f1 = f1_score(original_label, predict_label, average=None)
    cm = confusion_matrix(original_label, predict_label)
    cm_new = np.empty(shape=[5, 5])
    for x in range(5):
        t = cm.sum(axis=1)[x]
        for y in range(5):
            cm_new[x][y] = round(cm[x][y] / t * 100, 2)
    plt.figure()
    plt.matshow(cm_new, cmap=plt.cm.Blues)
    plt.colorbar()
    x_numbers = []
    y_numbers = []
    cm_percent = []
    for x in range(5):
        y_numbers.append(cm.sum(axis=1)[x])
        x_numbers.append(cm.sum(axis=0)[x])
        for y in range(5):
            percent = format(cm_new[x, y] * 100 / cm_new.sum(axis=1)[x], ".2f")
            cm_percent.append(str(percent))
            plt.annotate(format(cm_new[x, y] * 100 / cm_new.sum(axis=1)[x],
                                ".2f"),
                         xy=(y, x),
                         horizontalalignment='center',
                         verticalalignment='center',
                         fontsize=10)
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
    plt.title('confusion matrix')

    y_stage = [
        "W\n(" + str(y_numbers[0]) + ")", "N1\n(" + str(y_numbers[1]) + ")",
        "N2\n(" + str(y_numbers[2]) + ")", "N3\n(" + str(y_numbers[3]) + ")",
        "REM\n(" + str(y_numbers[4]) + ")"
    ]
    x_stage = [
        "W\n(" + str(x_numbers[0]) + ")", "N1\n(" + str(x_numbers[1]) + ")",
        "N2\n(" + str(x_numbers[2]) + ")", "N3\n(" + str(x_numbers[3]) + ")",
        "REM\n(" + str(x_numbers[4]) + ")"
    ]
    y = [0, 1, 2, 3, 4]
    plt.xticks(y, x_stage)
    plt.yticks(y, y_stage)

    plt.savefig(savepath + "matrix" + str(kunm) + ".svg")
    plt.show()
    plt.close()
    return kappa(cm), classification_report(original_label, predict_label)
Exemplo n.º 29
0
def plot_feature_importances_cancer(scores):
    names, val_scores = [name for name, _, _, _, _, _, _ in scores
                         ], [score for _, score, _, _, _, _, _ in scores]

    plt.rcParams["figure.figsize"] = [15, 9]
    n_features = len(names)
    plt.barh(range(n_features), val_scores, align='center')
    plt.yticks(np.arange(n_features), names)
    plt.xlabel("Accuracy")
    plt.ylabel("Model")
    path = WORKING_PATH + "/___comparison_cancer_model.png"
    plt.savefig(path)
    plt.clf()
    return path
Exemplo n.º 30
0
def object_detection_api(img_path, threshold=0.5, rect_th=3, text_size=3, text_th=3):

  boxes, pred_cls = get_prediction(img_path, threshold) # Get predictions
  img = cv2.imread(img_path) # Read image with cv2
  img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Convert to RGB
  for i in range(len(boxes)):
    cv2.rectangle(img, boxes[i][0], boxes[i][1],color=(0, 255, 0), thickness=rect_th) # Draw Rectangle with the coordinates
    cv2.putText(img,pred_cls[i], boxes[i][0],  cv2.FONT_HERSHEY_SIMPLEX, text_size, (0,255,0),thickness=text_th) # Write the prediction class
    
  plt.figure(figsize=(20,30)) # display the output image
  plt.imshow(img)
  plt.xticks([])
  plt.yticks([])
  plt.show()
Exemplo n.º 31
0
def plot_value_array(i, predictions_array, true_label, number_of_classes=3):
    predictions_array, true_label = predictions_array, true_label[i]
    plt.style.use(['classic'])
    plt.grid(False)
    plt.xticks(range(number_of_classes))
    plt.yticks([])
    thisplot = plt.bar(range(number_of_classes), 1, color="#FFFFFF")
    plt.ylim([0, 1])
    predicted_label = np.argmax(predictions_array)
    #print(true_label[0])
    #print(predicted_label)

    thisplot[predicted_label].set_color('red')
    thisplot[true_label].set_color('blue')
def plot_confusion_matrix(cm, labels, title='Confusion matrix', cmap=plt.cm.Blues, save=False):
    plt.figure()
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(labels))
    plt.xticks(tick_marks, labels, rotation=45)
    plt.yticks(tick_marks, labels)
    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
    plt.show()
    if save:
        plt.savefig(save)
Exemplo n.º 33
0
def dimension_reduction(method, dimension):
    my_arrays = load_data(0, get_avg=True)
    for i in range(1, 89):
        my_arrays = np.concatenate((my_arrays, load_data(i, get_avg=True)),
                                   axis=0)
    # print(my_arrays.shape)
    my_tensor = torch.from_numpy(my_arrays)
    my_labels = np.loadtxt('hist/rotation.txt')
    print(my_labels.shape)
    # time.sleep(30)
    color_choice = ['b', 'g', 'r', 'yellow']
    # print(my_tensor)
    if method == 'tsne':
        module = manifold.TSNE(n_components=dimension,
                               init='random',
                               random_state=500,
                               early_exaggeration=5,
                               method='exact')
    elif method == 'PCA':
        module = PCA(n_components=3)
    x_numpy = my_tensor.data.cpu().numpy()
    x_reduced = module.fit_transform(x_numpy)
    print(x_reduced.shape)
    print(x_reduced)
    plt.figure()
    if dimension == 2:
        for i in range(x_reduced.shape[0]):
            color_index = int(my_labels[i])
            plt.plot(x_reduced[i, 0],
                     x_reduced[i, 1],
                     'o',
                     color=color_choice[color_index])
        plt.xticks()
        plt.yticks()

    elif dimension == 3:
        ax = plt.axes(projection='3d')
        for i in range(x_reduced.shape[0]):
            color_index = int(my_labels[i])
            ax.scatter(x_reduced[i, 0],
                       x_reduced[i, 1],
                       x_reduced[i, 2],
                       color=color_choice[color_index])
        ax.set_xlabel('X Label')
        ax.set_ylabel('Y Label')
        ax.set_zlabel('Z Label')

    plt.savefig('plots/{}-24bin-{}d.pdf'.format(method, dimension))
    plt.show()
Exemplo n.º 34
0
def plot_confusion_matrix(cm, classes,
    normalize=False, title='Confusion matrix',
    cmap=plt.cm.Blues, filename='viz\\confusion_matrix.png'):
  plt.figure()
  plt.imshow(cm, interpolation='nearest', cmap=cmap)
  plt.title(title)
  plt.colorbar()
  tick_marks = np.arange(len(classes))
  plt.xticks(tick_marks, classes, rotation=45)
  plt.yticks(tick_marks, classes)

  if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]

  thresh = cm.max() / 2.
  for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
      plt.text(j, i, cm[i, j], horizontalalignment="center", color="white" if cm[i, j] > thresh else "black")

  plt.tight_layout()
  plt.ylabel('True label')
  plt.xlabel('Predicted label')
  plt.savefig(filename)
Exemplo n.º 35
0
def plotDisparityMap(network=None, disparity=0):
    
    assert network is not None, "Network is not initialised! Visualising failed."
    assert disparity >= 0 and disparity <= maxDisparity, "No such disparity map in the network."
    import matplotlib.pyplot as plt
    from matplotlib import animation
    from NetworkBuilder import sameDisparityInd
    
    print "Visualising results for disparity value {0}...".format(disparity) 
    
    cellsOut = [network[x][2] for x in sameDisparityInd[disparity]]

    spikes = [x.getSpikes() for x in cellsOut]
#     print spikes
    
    sortedSpikes = sortSpikes(spikes)
#     print sortedSpikes
    
    framesOfSpikes = generateFrames(sortedSpikes)
#     print framesOfSpikes
    
    x = range(0, dimensionRetinaX)
    y = range(0, dimensionRetinaY)
    from numpy import meshgrid
    rows, pixels = meshgrid(x,y)
    
    fig = plt.figure()
    
    initialData = createInitialisingData()
#     print initialData
    imNet = plt.imshow(initialData, cmap='gray', interpolation='none', origin='upper')
    
    plt.xticks(range(0, dimensionRetinaX)) 
    plt.yticks(range(0, dimensionRetinaY))
    plt.title("Disparity Map {0}".format(disparity))
    args = (framesOfSpikes, imNet)
    anim = animation.FuncAnimation(fig, animate, fargs=args, frames=int(simulationTime)*10, interval=30)
          
    plt.show()
Exemplo n.º 36
0
plt.loglog(xr10/10,Nr10,'o',markersize=8,color=c1,label=r'$\sigma=0.10h_0$')
plt.loglog(xr20/10,Nr20,'s',markersize=8,color=c1,label=r'$\sigma=0.20h_0$')
plt.loglog(xr25/10,Nr25,'^',markersize=8,color=c1,label=r'$\sigma=0.25h_0$')
plt.loglog(xr30/10,Nr30,'v',markersize=8,color=c1,label=r'$\sigma=0.30h_0$')
plt.loglog(xr40/10,Nr40,'<',markersize=8,color=c1,label=r'$\sigma=0.40h_0$')
plt.loglog(xr50/10,Nr50,'>',markersize=8,color=c1,label=r'$\sigma=0.50h_0$')
plt.loglog(xg2/10,Ng2,'d',markersize=8,color=c2,label=r'$\gamma=0.2l_p$')
plt.loglog(xg4/10,Ng4,'o',markersize=8,color=c2,label=r'$\gamma=0.4l_p$')
plt.loglog(xg10/10,Ng10,'s',markersize=8,color=c2,label=r'$\gamma=l_p$')
plt.loglog(xg20/10,Ng20,'^',markersize=8,color=c2,label=r'$\gamma=2l_p$')
plt.loglog(xg40/10,Ng40,'v',markersize=8,color=c2,label=r'$\gamma=4l_p$')
plt.loglog(xg100/10,Ng100,'<',markersize=8,color=c2,label=r'$\gamma=10l_p$')
plt.loglog(xg1000/10,Ng1000,'>',markersize=8,color=c2,label=r'$\gamma=100l_p$')
plt.legend(loc='upper right',prop={'size':17})
plt.xticks((10**0, 10**1, 10**2), (r'$10^{0}$', r'$10^{1}$', r'$10^{2}$'), fontsize=24)
plt.yticks((10**0, 10**1, 10**2, 10**3), (r'$10^{0}$', r'$10^{1}$', r'$10^{2}$', r'$10^{3}$'), fontsize=24)
ax = plt.gca()
ax.tick_params(axis='both',reset=False,which='both',length=10,width=2,direction='bottom')
ax.yaxis.set_tick_params(length=20,width=2,direction='bottom')
ax.xaxis.set_tick_params(length=20,width=2,direction='bottom')
ax.tick_params(axis='both',reset=False,which='minor',length=10,width=1,direction='bottom')
ax.xaxis.set_tick_params(length=20,width=1,direction='bottom')
ax.yaxis.set_tick_params(length=20,width=1,direction='bottom')
plt.annotate(r'$N_{LSA}=173$', fontsize=24, xy=(6, 173), xytext=(9,173), horizontalalignment='left', verticalalignment='center', arrowprops=dict(facecolor='black', shrink=0.05))
plt.xlabel(r'$\mathit{L_w/l_p}$',fontsize=24)
plt.ylabel(r'$\mathit{N(L_{w})}$',fontsize=24)
l1 = [3,80]
l2 = [80,3]
scatter(l1,l2)
plot(l1,l2,color='black',linewidth=4)
plt.yticks((10**0, 10**1, 10**2, 10**3), (r'$10^{0}$', r'$10^{1}$', r'$10^{2}$', r'$10^{3}$'), fontsize=24)
Exemplo n.º 37
0
diabetes_y_train = diabetes.target[:-20]
diabetes_y_test  = diabetes.target[-20:]


regr = linear_model.LinearRegression()
regr.fit(diabetes_X_train, diabetes_y_train)

# >> LinearRegression(copy_X=True, fit_intercept=True, normalize=False)
print regr.coef_

# The mean square error
np.mean((regr.predict(diabetes_X_test)-diabetes_y_test)**2)

# Explained variance score: 1 is perfect prediction
# and 0 means that there is no linear relationship
# between X and Y.
regr.score(diabetes_X_test, diabetes_y_test) 

# Plot results

pl.clf()          # Clear plots

pl.plot(diabetes_X_test, regr.fit(diabetes_X_train, diabetes_y_train));

pl.title('Linear regression of sample diabetes data\n'
         'Centroids are marked with white cross')
pl.xlim(x_min, x_max)
pl.ylim(y_min, y_max)
pl.xticks(())
pl.yticks(())
pl.show()
Exemplo n.º 38
0
x = (df['west'] - df['west'].min())*477/(df['east'].max() - df['west'].min())
y = 798-(df['north'] - df['south'].min())*798/(df['north'].max() - df['south'].min())
s = df['currentspeed'] / df['currentspeed'].max()
plt.scatter(x,y,c=s,linewidth=0,s=1000,alpha=0.1)

#x0 = (df.ix[0]['west'] - df['west'].min())*477/(df['east'].max() - df['west'].min())
#y0 = 798-(df.ix[0]['north'] - df['south'].min())*798/(df['north'].max() - df['south'].min())
#plt.scatter(x0,y0,c='r',s=2000)
#x0 = (df.ix[0]['east'] - df['west'].min())*477/(df['east'].max() - df['west'].min())
#y0 = 798-(df.ix[0]['south'] - df['south'].min())*798/(df['north'].max() - df['south'].min())
#plt.scatter(x0,y0,c='r',s=2000)
plt.xlim(0,477)
plt.ylim(798,0)
plt.xticks([])
plt.yticks([])
#plt.plot([df['west'],df['west'],df['east'],df['east'],df['west']],[df['south'],df['north'],df['north'],df['south'],df['south']],linewidth=20,alpha=0.2)

# <codecell>

plt.figure(figsize=(15,15))

patches = []
verts = [(df['west'],df['south']),
    (df['west'],df['north']),
    (df['east'],df['north']),
    (df['east'],df['south']),
    (df['west'],df['south'])]

codes = [Path.MOVETO,
    Path.LINETO,
Exemplo n.º 39
0
def create_figure_surface(figid, aspect, xx, yy, cmin, cmax, levels, slices, v3d):
    ''' creates a plot of the surface of a tracer data
    Parameters
    ----------
    figid : int  
            id of figure
    aspect: float
            aspect ratio of figure
    xx    : array 
            scale of x axis
    yy    : array 
            scale of y axis     
    cmin,cmax : array
                minimum and maxminm of the color range
    levels : array
            range of contourlines
    slices : array
            location of slices
    v3d    : vector data in geometry format
    Returns
    -------
    plot :  of surface data
    '''
    # prepare matplotlib
    import matplotlib
    matplotlib.rc("font",**{"family":"sans-serif"})
    matplotlib.rc("text", usetex=True)
    #matplotlib.use("PDF")
    import matplotlib.pyplot as plt
    # basemap
    from mpl_toolkits.basemap import Basemap
    # numpy
    import numpy as np

    # data
    vv = v3d[0,:,:,0]
    # shift
    vv = np.roll(vv, 64, axis=1)

    # plot surface
    plt.figure(figid)
    # colormap
    cmap = plt.cm.bone_r
    # contour fill
    p1 = plt.contourf(xx, yy, vv, cmap=cmap, levels=levels, origin="lower")#, hold="on")
    plt.clim(cmin, cmax)
    # contour lines
    p2 = plt.contour(xx, yy, vv, levels=levels, linewidths = (1,), colors="k")#, hold="on")
    plt.clabel(p2, fmt = "%2.1f", colors = "k", fontsize = 14)
    #plt.colorbar(p2,shrink=0.8, extend='both')
    # slices
    #s1 = xx[np.mod(slices[0]+64, 128)]
    #s2 = xx[np.mod(slices[1]+64, 128)]
    #s3 = xx[np.mod(slices[2]+64, 128)]
#    print s1, s2, s3
    #plt.vlines([s1, s2, s3], -90, 90, color='k', linestyles='--')
    # set aspect ratio of axes
    plt.gca().set_aspect(aspect)

    # basemap
    m = Basemap(projection="cyl")
    m.drawcoastlines(linewidth = 0.5)

    # xticks
    plt.xticks(range(-180, 181, 45), range(-180, 181, 45))
    plt.xlim([-180, 180])
    plt.xlabel("Longitude [degrees]", labelpad=8)
    # yticks
    plt.yticks(range(-90, 91, 30), range(-90, 91, 30))
    plt.ylim([-90, 90])
    plt.ylabel("Latitude [degrees]")


    # write to file
    plt.savefig("solution-surface", bbox_inches="tight")
    plt.show()