【问题标题】:Seaborn fails to plot heatmap for a particular feature (titanic dataset)Seaborn 无法绘制特定特征的热图(泰坦尼克数据集)
【发布时间】:2021-11-10 12:43:43
【问题描述】:

我正在使用一些神经网络,并且正在努力使用 seaborn 为 Titanic 数据集绘制相关热图。简而言之:在绘图过程中,“n_siblings_spouses”特征似乎存在问题。我不知道问题是由于功能本身(可能是间距?)还是 seaborn 存在固有问题。

是否可以在无需从数据集中删除特征的情况下解决问题?

这是一个 MWE。并提前致谢!

from __future__ import absolute_import,division,print_function,unicode_literals
import numpy as np 
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
from matplotlib import rc, font_manager
%matplotlib inline

from IPython.display import clear_output
from six.moves import urllib
import tensorflow.compat.v2.feature_column as fc 
import tensorflow as tf 
import seaborn as sns

rc('text', usetex=True)
matplotlib.rcParams['text.latex.preamble'] = [r'\usepackage{amsmath}']

# only if needed
#!apt install texlive-fonts-recommended texlive-fonts-extra cm-super dvipng
plt.rc('font', family='serif')

# URL address of data
TRAIN_DATA_URL = "https://storage.googleapis.com/tf-datasets/titanic/train.csv"

# Downloading data
train_file_path = tf.keras.utils.get_file("train.csv", TRAIN_DATA_URL)

# Setting numpy default values.
np.set_printoptions(precision=3, suppress=True)

# Reading data
data_train = pd.read_csv(train_file_path)

print("\n TRAIN DATA SET")
print(data_train.head(),"\n")

def heatMap(df):
    #Create Correlation df
    corr = df.corr()
    #Plot figsize
    fig, ax = plt.subplots(figsize=(10, 10))
    #Generate Color Map
    colormap = sns.diverging_palette(220, 10, as_cmap=True)
    #Generate Heat Map, allow annotations and place floats in map
    sns.heatmap(corr, cmap=colormap, annot=True, fmt=".2f")
    #Apply xticks
    plt.xticks(range(len(corr.columns)), corr.columns);
    #Apply yticks
    plt.yticks(range(len(corr.columns)), corr.columns)
    #show plot
    plt.show()

heatMap(data_train)

这是尝试执行 heatMap 函数时出现的问题(我在 Colab 中工作。但是,这也发生在控制台中):

---------------------------------------------------------------------------
CalledProcessError                        Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/matplotlib/texmanager.py in _run_checked_subprocess(self, command, tex)
    305                                              cwd=self.texcache,
--> 306                                              stderr=subprocess.STDOUT)
    307         except FileNotFoundError as exc:

22 frames
CalledProcessError: Command '['latex', '-interaction=nonstopmode', '--halt-on-error', '/root/.cache/matplotlib/tex.cache/bf616eae1512bede263889c8e1d8fb21.tex']' returned non-zero exit status 1.

The above exception was the direct cause of the following exception:

RuntimeError                              Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/matplotlib/texmanager.py in _run_checked_subprocess(self, command, tex)
    317                     prog=command[0],
    318                     tex=tex.encode('unicode_escape'),
--> 319                     exc=exc.output.decode('utf-8'))) from exc
    320         _log.debug(report)
    321         return report

RuntimeError: latex was not able to process the following string:
b'n_siblings_spouses'

Here is the full report generated by latex:
This is pdfTeX, Version 3.14159265-2.6-1.40.18 (TeX Live 2017/Debian) (preloaded format=latex)
 restricted \write18 enabled.
entering extended mode
(/root/.cache/matplotlib/tex.cache/bf616eae1512bede263889c8e1d8fb21.tex
LaTeX2e <2017-04-15>
Babel <3.18> and hyphenation patterns for 3 language(s) loaded.
(/usr/share/texlive/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo))
(/usr/share/texlive/texmf-dist/tex/latex/type1cm/type1cm.sty)
(/usr/share/texmf/tex/latex/cm-super/type1ec.sty
(/usr/share/texlive/texmf-dist/tex/latex/base/t1cmr.fd))
(/usr/share/texlive/texmf-dist/tex/latex/base/textcomp.sty
(/usr/share/texlive/texmf-dist/tex/latex/base/ts1enc.def))
(/usr/share/texlive/texmf-dist/tex/latex/base/inputenc.sty
(/usr/share/texlive/texmf-dist/tex/latex/base/utf8.def
(/usr/share/texlive/texmf-dist/tex/latex/base/t1enc.dfu)
(/usr/share/texlive/texmf-dist/tex/latex/base/ot1enc.dfu)
(/usr/share/texlive/texmf-dist/tex/latex/base/omsenc.dfu)
(/usr/share/texlive/texmf-dist/tex/latex/base/ts1enc.dfu)))
(/usr/share/texlive/texmf-dist/tex/latex/geometry/geometry.sty
(/usr/share/texlive/texmf-dist/tex/latex/graphics/keyval.sty)
(/usr/share/texlive/texmf-dist/tex/generic/oberdiek/ifpdf.sty)
(/usr/share/texlive/texmf-dist/tex/generic/oberdiek/ifvtex.sty)
(/usr/share/texlive/texmf-dist/tex/generic/ifxetex/ifxetex.sty)

Package geometry Warning: Over-specification in `h'-direction.
    `width' (5058.9pt) is ignored.


Package geometry Warning: Over-specification in `v'-direction.
    `height' (5058.9pt) is ignored.

) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsmath.sty
For additional information on amsmath, use the `?' option.
(/usr/share/texlive/texmf-dist/tex/latex/amsmath/amstext.sty
(/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsgen.sty))
(/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsbsy.sty)
(/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsopn.sty))
(./bf616eae1512bede263889c8e1d8fb21.aux)
(/usr/share/texlive/texmf-dist/tex/latex/base/ts1cmr.fd)
*geometry* driver: auto-detecting
*geometry* detected driver: dvips
! Missing $ inserted.
<inserted text> 
                $
l.19 {\rmfamily n_
                  siblings_spouses}
No pages of output.
Transcript written on bf616eae1512bede263889c8e1d8fb21.log.


---------------------------------------------------------------------------
CalledProcessError                        Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/matplotlib/texmanager.py in _run_checked_subprocess(self, command, tex)
    305                                              cwd=self.texcache,
--> 306                                              stderr=subprocess.STDOUT)
    307         except FileNotFoundError as exc:

21 frames
CalledProcessError: Command '['latex', '-interaction=nonstopmode', '--halt-on-error', '/root/.cache/matplotlib/tex.cache/bf616eae1512bede263889c8e1d8fb21.tex']' returned non-zero exit status 1.

The above exception was the direct cause of the following exception:

RuntimeError                              Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/matplotlib/texmanager.py in _run_checked_subprocess(self, command, tex)
    317                     prog=command[0],
    318                     tex=tex.encode('unicode_escape'),
--> 319                     exc=exc.output.decode('utf-8'))) from exc
    320         _log.debug(report)
    321         return report

RuntimeError: latex was not able to process the following string:
b'n_siblings_spouses'

Here is the full report generated by latex:
This is pdfTeX, Version 3.14159265-2.6-1.40.18 (TeX Live 2017/Debian) (preloaded format=latex)
 restricted \write18 enabled.
entering extended mode
(/root/.cache/matplotlib/tex.cache/bf616eae1512bede263889c8e1d8fb21.tex
LaTeX2e <2017-04-15>
Babel <3.18> and hyphenation patterns for 3 language(s) loaded.
(/usr/share/texlive/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo))
(/usr/share/texlive/texmf-dist/tex/latex/type1cm/type1cm.sty)
(/usr/share/texmf/tex/latex/cm-super/type1ec.sty
(/usr/share/texlive/texmf-dist/tex/latex/base/t1cmr.fd))
(/usr/share/texlive/texmf-dist/tex/latex/base/textcomp.sty
(/usr/share/texlive/texmf-dist/tex/latex/base/ts1enc.def))
(/usr/share/texlive/texmf-dist/tex/latex/base/inputenc.sty
(/usr/share/texlive/texmf-dist/tex/latex/base/utf8.def
(/usr/share/texlive/texmf-dist/tex/latex/base/t1enc.dfu)
(/usr/share/texlive/texmf-dist/tex/latex/base/ot1enc.dfu)
(/usr/share/texlive/texmf-dist/tex/latex/base/omsenc.dfu)
(/usr/share/texlive/texmf-dist/tex/latex/base/ts1enc.dfu)))
(/usr/share/texlive/texmf-dist/tex/latex/geometry/geometry.sty
(/usr/share/texlive/texmf-dist/tex/latex/graphics/keyval.sty)
(/usr/share/texlive/texmf-dist/tex/generic/oberdiek/ifpdf.sty)
(/usr/share/texlive/texmf-dist/tex/generic/oberdiek/ifvtex.sty)
(/usr/share/texlive/texmf-dist/tex/generic/ifxetex/ifxetex.sty)

Package geometry Warning: Over-specification in `h'-direction.
    `width' (5058.9pt) is ignored.


Package geometry Warning: Over-specification in `v'-direction.
    `height' (5058.9pt) is ignored.

) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsmath.sty
For additional information on amsmath, use the `?' option.
(/usr/share/texlive/texmf-dist/tex/latex/amsmath/amstext.sty
(/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsgen.sty))
(/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsbsy.sty)
(/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsopn.sty))
(./bf616eae1512bede263889c8e1d8fb21.aux)
(/usr/share/texlive/texmf-dist/tex/latex/base/ts1cmr.fd)
*geometry* driver: auto-detecting
*geometry* detected driver: dvips
! Missing $ inserted.
<inserted text> 
                $
l.19 {\rmfamily n_
                  siblings_spouses}
No pages of output.
Transcript written on bf616eae1512bede263889c8e1d8fb21.log.


<Figure size 720x720 with 2 Axes>

【问题讨论】:

  • 您的代码完美运行!这似乎是 Colab 的一个问题。尝试在其他地方运行它
  • 有人评论说可能是Colabo错误,所以我尝试运行Colab中的代码。我能够确认同样的错误。我从错误中确定这是一个与 Tex 相关的错误,并通过搜索错误消息找到了解决方法。在this page 上执行安装并禁用字体周围的设置后,显示了热图。
  • @r-beginners 谢谢!解决了这个问题。如果需要,您可以发布代码,以便我可以将其作为答案进行检查!干杯!
  • 我已禁用 Latex 相关设置,如果我不更改就发布有什么问题吗?
  • @r-beginners 不。它的工作原理与您发布的完全一样。所以我认为你可以这样发布(没有乳胶相关设置)

标签: tensorflow matplotlib seaborn


【解决方案1】:

为了解决这个问题,我遇到this information 说 Colab 需要一个与 Tex 相关的模块。 SO也有很好的回答。

您将需要安装以下内容

  • ! sudo apt-get install texlive-latex-recommended
  • ! sudo apt-get install dvipng texlive-fonts-recommended
  • ! wgethttp://mirrors.ctan.org/macros/latex/contrib/type1cm.zip
  • !解压 type1cm.zip -d /tmp/type1cm
  • ! cd /tmp/type1cm/type1cm/ && sudo latex type1cm.ins
  • ! sudo mkdir /usr/share/texmf/tex/latex/type1cm
  • ! sudo cp /tmp/type1cm/type1cm/type1cm.sty /usr/share/texmf/tex/latex/type1cm
  • ! sudo texhash
  • ! sudo apt install cm-super
from __future__ import absolute_import,division,print_function,unicode_literals
import numpy as np 
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
# from matplotlib import rc, font_manager
%matplotlib inline

from IPython.display import clear_output
from six.moves import urllib
import tensorflow.compat.v2.feature_column as fc 
import tensorflow as tf 
import seaborn as sns

# rc('text', usetex=True)
# matplotlib.rcParams['text.latex.preamble'] = [r'\usepackage{amsmath}']

# only if needed
#!apt install texlive-fonts-recommended texlive-fonts-extra cm-super dvipng
# plt.rc('font', family='serif')

# URL address of data
TRAIN_DATA_URL = "https://storage.googleapis.com/tf-datasets/titanic/train.csv"

# Downloading data
train_file_path = tf.keras.utils.get_file("/content/sample_data/train.csv", TRAIN_DATA_URL)

# Setting numpy default values.
np.set_printoptions(precision=3, suppress=True)

# Reading data
data_train = pd.read_csv(train_file_path)

print("\n TRAIN DATA SET")
print(data_train.head(),"\n")

def heatMap(df):
    #Create Correlation df
    corr = df.corr()
    print(corr)
    #Plot figsize
    fig, ax = plt.subplots(figsize=(10, 10))
    #Generate Color Map
    colormap = sns.diverging_palette(220, 10, as_cmap=True)
    #Generate Heat Map, allow annotations and place floats in map
    sns.heatmap(corr, cmap=colormap, annot=True, fmt=".2f")
    #Apply xticks
    plt.xticks(range(len(corr.columns)), corr.columns);
    #Apply yticks
    plt.yticks(range(len(corr.columns)), corr.columns)
    #show plot
    plt.show()

heatMap(data_train)

【讨论】:

  • 我很感激有机会对我不太完美的答案做出回应。
猜你喜欢
  • 2019-03-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-25
  • 2021-07-29
  • 2017-03-08
  • 1970-01-01
  • 1970-01-01
  • 2017-06-27
相关资源
最近更新 更多