Пример #1
0
def kernel_to_heatmap(K, title, max_scale, save=True):

    # Generate a mask for the upper triangle
    #mask = np.triu(np.ones_like(K, dtype=np.bool))
    # Generate a custom diverging colormap
    cmap = "YlGn"
    rotation = 0
    x = [0.5, 1.5, 2.5]
    name_dict = get_name_dict()
    handles = name_dict.values()

    # Draw the heatmap with the mask and correct aspect ratio
    sns.heatmap(
        K,
        #mask=mask,
        cmap=cmap,
        vmax=max_scale,
        #center=0,
        square=True,
        linewidths=.75,
        cbar_kws={
            'shrink': .75,
            'label': 'Kernel values'
        },
        xticklabels=True,
        yticklabels=True,
        annot=True,
    )
    sns.set(font_scale=2)
    sns.set(style="white")

    plt.xticks(x, handles, rotation=rotation)
    plt.yticks(x, handles, rotation=0)
    plt.title(title)
    if save:
        filename = clean_filename(title, 'png', plot_directory)
        print(f'saving plot to {filename}')
        plt.savefig(filename)
    plt.show()
Пример #2
0
def array_to_matrix(arr):
    mat = np.zeros(shape=(3, 3))
    k = 0
    for i in range(3):
        for j in range(i, 3):
            if i == j: mat[i][j] = 1.0
            else:
                mat[i][j] = arr[k]
                mat[j][i] = mat[i][j]
                k += 1
    return mat


if __name__ == '__main__':
    t0 = time.time()
    name_dict = get_name_dict()

    stemmer_flag = False
    max_ngram = 2

    # Load tweets from csv files
    names = list()
    handles = get_handles()
    for handle in handles:
        names.append(name_dict[handle])

    raw_corpus, raw_labels = load_all_tweets()
    corpus, labels = iterate_preprocess(raw_corpus, raw_labels, handles,
                                        stemmer_flag)

    for ngram in range(1, max_ngram + 1):  # loop through ngrams