使用seaborn和matplotlib着色文本?

时间:2018-04-20 07:06:52

标签: python-3.x matplotlib seaborn

我有一句话,比如说

25 August 2003 League of Extraordinary Gentlemen: Sean Connery is one of the all time greats I have been a fan of his since the 1950's. 25 August 2003 League of Extraordinary Gentlemen

我通过openai情感代码传递给我一些神经元权重,它可以等于或略大于单词数。

神经元重量是

[0.01258736, 0.03544582, 0.05184804, 0.05354257, 0.07339437,
       0.07021661, 0.06993681, 0.06021424, 0.0601177 , 0.04100083,
       0.03557627, 0.02574683, 0.02565657, 0.03435502, 0.04881989,
       0.08868718, 0.06816255, 0.05957553, 0.06767794, 0.06561323,
       0.06339648, 0.06271613, 0.06312297, 0.07370538, 0.08369936,
       0.09008111, 0.09059132, 0.08732472, 0.08742133, 0.08792272,
       0.08504769, 0.08541565, 0.09255819, 0.09240738, 0.09245031,
       0.09080137, 0.08733468, 0.08705935, 0.09201239, 0.113047  ,
       0.14285286, 0.15205048, 0.15249513, 0.14051639, 0.14070784,
       0.14526351, 0.14548902, 0.12730363, 0.11916814, 0.11097522,
       0.11390981, 0.12734678, 0.13625301, 0.13386811, 0.13413942,
       0.13782364, 0.14033082, 0.14971626, 0.14988877, 0.14171578,
       0.13999145, 0.1408006 , 0.1410009 , 0.13423227, 0.16819029,
       0.18822579, 0.18462598, 0.18283379, 0.16304792, 0.1634682 ,
       0.18733767, 0.22205424, 0.22615907, 0.22679318, 0.2353312 ,
       0.24562076, 0.24771859, 0.24478345, 0.25780812, 0.25183237,
       0.24660441, 0.2522405 , 0.26310056, 0.26156184, 0.26127928,
       0.26154354, 0.2380443 , 0.2447366 , 0.24580643, 0.22959644,
       0.23065038, 0.228564  , 0.23980206, 0.23410076, 0.40933537,
       0.436683  , 0.5319608 , 0.5273239 , 0.54030097, 0.55781454,
       0.5665511 , 0.58764166, 0.58651507, 0.5870301 , 0.5893866 ,
       0.58905166, 0.58955604, 0.5872186 , 0.58744675, 0.58569545,
       0.58279306, 0.58205146, 0.6251827 , 0.6278348 , 0.63121724,
       0.7156403 , 0.715524  , 0.714875  , 0.71317464, 0.7630029 ,
       0.75933087, 0.7571995 , 0.7563375 , 0.7583521 , 0.75923103,
       0.8155783 , 0.8082132 , 0.8096348 , 0.8114364 , 0.82923543,
       0.8229595 , 0.8196689 , 0.8070393 , 0.808637  , 0.82305557,
       0.82719535, 0.8210828 , 0.8697561 , 0.8547278 , 0.85224617,
       0.8521625 , 0.84694564, 0.8472206 , 0.8432255 , 0.8431826 ,
       0.8394848 , 0.83804935, 0.83134645, 0.8234757 , 0.82382894,
       0.82562804, 0.80014366, 0.7866942 , 0.78344023, 0.78955245,
       0.7862923 , 0.7851586 , 0.7805863 , 0.780684  , 0.79073226,
       0.79341674, 0.7970072 , 0.7966449 , 0.79455364, 0.7945448 ,
       0.79476243, 0.7928985 , 0.79307675, 0.79677683, 0.79655904,
       0.79619783, 0.7947823 , 0.7915144 , 0.7912799 , 0.795091  ,
       0.8032384 , 0.810835  , 0.8084989 , 0.8094493 , 0.8045582 ,
       0.80466574, 0.8074054 , 0.8075554 , 0.80178404, 0.7978776 ,
       0.78742194, 0.8119776 , 0.8119776 , 0.8119776 , 0.8119776 ,
       0.8119776 , 0.8119776 ]

基本原则是文本的背景颜色应该与w.r.t相同。提供的神经元重量。 (正负重为绿色,负重为红色,有些为黄色,当重量值接近0时)

因此,对于上面的阴影应该是(绿色表示正面,红色表示负面)

enter image description here

但它真正的阴谋是

enter image description here

阴影文本的功能w.r.t.对神经元的权重是

def plot_neuron_heatmap(text, values, n_limit=80, savename='fig1.png',
                        cell_height=0.325, cell_width=0.15, dpi=100):
    text = text.replace('\n', '\\n')
    text = np.array(list(text + ' ' * (-len(text) % n_limit)))
    if len(values) > text.size:
        values = np.array(values[:text.size])
    else:
        t = values
        values = np.zeros(text.shape, dtype=np.int)
        values[:len(t)] = t
    text = text.reshape(-1, n_limit)
    values = values.reshape(-1, n_limit)
    mask = np.zeros(values.shape, dtype=np.bool)
    mask.ravel()[values.size:] = True
    mask = mask.reshape(-1, n_limit)
    plt.figure(figsize=(cell_width * n_limit, cell_height * len(text)))
    hmap = sns.heatmap(values, annot=text,mask=mask, fmt='', vmin=-5, vmax=5, cmap='RdYlGn', xticklabels=False, yticklabels=False, cbar=False)
    plt.subplots_adjust()
    plt.savefig(savename if savename else 'fig1.png', dpi=dpi)

我哪里错了?

以上定义由@Mad Physicist link

完善

1 个答案:

答案 0 :(得分:1)

使用values创建np.zeros数组时,请设置dtype=np.int。因此,即使您将零替换为实际的浮点数据,它们也会舍入为整数,因为这是数组的dtype。这基本上是将它们全部设置为0,因为它们都小于1

你真的想把它们保留为花车,所以如果你改变这一行:

values = np.zeros(text.shape, dtype=np.int)

values = np.zeros(text.shape, dtype=np.float)
一切似乎都运转良好。

enter image description here