借助 Tikz 实现词向量

借助 Tikz 实现词向量

我正在尝试了解如何在 tikz 中重建所附图片。不幸的是,我根本不擅长使用 Tikz,但我仍然认为这是构建它的最佳方式。或者有没有更简单的方法来创建这样的图形?感谢您的任何建议。在此处输入图片描述

答案1

tikz

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{chains, positioning}

\begin{document}
\begin{tikzpicture}[
node distance = 0mm and 5mm,
  start chain = going below,
   box/.style = {draw, minimum width=12mm, minimum height=6mm,
                 inner sep=0pt, outer sep=0pt,
                 on chain}
                  ]
  \node (a) [box,pin=left:Royalty] {};                   
  \node[box,pin=left:Masculinity] {};
  \node[box,pin=left:Femininity] {};
  \node[box,pin=left:Age,
            label=below:$\vdots$] {};              
%
  \node (b) [box,right=of a,label=:King] {0.99};   
  \node[box] {0.99};
  \node[box] {0.05};
  \node[box,label=below:$\vdots$] {0.7};        
%
  \node (c) [box,right=of b,label=:???] {0.99};    
  \node[box] {0.99};
  \node[box] {0.93};
  \node[box,label=below:$\vdots$] {0.7};        
%
  \node (d) [box,right=of c,label=:Women] {0.99};  
  \node[box] {0.99};
  \node[box] {0.05};
  \node[box,label=below:$\vdots$] {0.7};        
%
  \node[box,right=of d,label=:Princess] {0.98};  
  \node[box] {0.02};
  \node[box] {0.94};
  \node[box,label=below:$\vdots$] {0.1};        
\end{tikzpicture}
\end{document}

在此处输入图片描述

你需要自己完成图像,因为我看不懂你的文字。正如所说成本加运费,tikz 可能对您的图像来说有点过度了。

代码稍微高级一些,所以我鼓励你阅读tikz 和 pgf 手册,至少第 3 部分:TikZ 不是一个绘画程序

编辑:谢谢亨利·孟克您的笔迹已被破译并包含在上面的图像和代码中。这些数字仍未得到验证,这个我留给您了。(作为练习,它们在代码中)。

答案2

我认为使用矩阵比 Zarko 的链式解决方案更容易一些。

\documentclass{article}
\pagestyle{empty}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}

\begin{tikzpicture}
  \matrix[
  matrix of math nodes,
  nodes in empty cells,
  column sep=1em,
  nodes={
    draw,
    anchor=south,
    minimum width=3em,
    minimum height=5ex
  }] (m) {
    & 0.99   & 0.99   & 0.02   & 0.98    \\
    & 0.99   & 0.05   & 0.01   & 0.02    \\
    & 0.05   & 0.93   & 0.999  & 0.94    \\
    & 0.7    & 0.6    & 0.5    & 0.1     \\
    & \vdots & \vdots & \vdots & \vdots  \\
  };
  \begin{scope}[every node/.style={green!75!black,above=4ex,anchor=base}]
    \node at (m-1-2) {King};
    \node at (m-1-3) {Queen};
    \node at (m-1-4) {Woman};
    \node at (m-1-5) {Princess};
  \end{scope}
  \begin{scope}[every node/.style=blue!75!white]
    \node[pin={left:Royalty}]     at (m-1-1.west) {};
    \node[pin={left:Masculinity}] at (m-2-1.west) {};
    \node[pin={left:Femininity}]  at (m-3-1.west) {};
    \node[pin={left:Age}]         at (m-4-1.west) {};
  \end{scope}
\end{tikzpicture}

\end{document}

在此处输入图片描述

我们还可以制作一个底部没有边框的新节点形状,我称之为house

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\makeatletter
\pgfdeclareshape{house}
{
  \inheritsavedanchors[from=rectangle] % this is nearly a rectangle
  \inheritanchorborder[from=rectangle]
  \inheritanchor[from=rectangle]{north}
  \inheritanchor[from=rectangle]{north west}
  \inheritanchor[from=rectangle]{north east}
  \inheritanchor[from=rectangle]{center}
  \inheritanchor[from=rectangle]{west}
  \inheritanchor[from=rectangle]{east}
  \inheritanchor[from=rectangle]{mid}
  \inheritanchor[from=rectangle]{mid west}
  \inheritanchor[from=rectangle]{mid east}
  \inheritanchor[from=rectangle]{base}
  \inheritanchor[from=rectangle]{base west}
  \inheritanchor[from=rectangle]{base east}
  \inheritanchor[from=rectangle]{south}
  \inheritanchor[from=rectangle]{south west}
  \inheritanchor[from=rectangle]{south east}
  \foregroundpath{
    % store lower right in xa/ya and upper right in xb/yb
    \southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
    \northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
    \pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@ya}}
    \pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@yb}}
    \pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@yb}}
    \pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@ya}}
    \pgfsetarrowsstart{}
    \pgfsetarrowsend{}
 }
}
\makeatother

\begin{document}

\begin{tikzpicture}
  \matrix[
  matrix of math nodes,
  nodes in empty cells,
  column sep=1em,
  nodes={
    draw,
    house,
    anchor=south,
    minimum width=3em,
    minimum height=5ex
  }] (m) {
    & 0.99   & 0.99   & 0.02   & 0.98    \\
    & 0.99   & 0.05   & 0.01   & 0.02    \\
    & 0.05   & 0.93   & 0.999  & 0.94    \\
    & 0.7    & 0.6    & 0.5    & 0.1     \\
    & \vdots & \vdots & \vdots & \vdots  \\
  };
  \begin{scope}[every node/.style={green!75!black,above=4ex,anchor=base}]
    \node at (m-1-2) {King};
    \node at (m-1-3) {Queen};
    \node at (m-1-4) {Woman};
    \node at (m-1-5) {Princess};
  \end{scope}
  \begin{scope}[every node/.style=blue!75!white]
    \node[pin={left:Royalty}]     at (m-1-1.west) {};
    \node[pin={left:Masculinity}] at (m-2-1.west) {};
    \node[pin={left:Femininity}]  at (m-3-1.west) {};
    \node[pin={left:Age}]         at (m-4-1.west) {};
    \node[pin={[pin edge={draw=none}]left:$\cdots$}] at (m-5-1.west) {};
  \end{scope}
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案3

在常规tabular环境下

在此处输入图片描述

\documentclass{article}
\usepackage{xcolor}
\usepackage{tabularx}
\newcolumntype{P}[1]{>{\centering\arraybackslash}m{#1}}
\renewcommand{\tabularxcolumn}[1]{>{\raggedleft\arraybackslash}m{#1}}
\newcommand\T{\rule{0pt}{6ex}}

\begin{document}

\begin{table}[h]
\begin{tabularx}{\linewidth}{@{}l P{6ex}*{9}{c}@{}}
\multicolumn{1}{r}{}             &                       &                       & \multicolumn{1}{>{\hsize=0.18\hsize}X}{\color{green} King\hfill} &                       & \multicolumn{1}{>{\hsize=0.18\hsize}X}{\color{green} Queen}                       &                       & \multicolumn{1}{l}{\color{green} Woman}                        &                       & \multicolumn{1}{l}{\color{green} Princess}                   \\[2ex] \cline{2-2} \cline{4-4} \cline{6-6} \cline{8-8} \cline{10-10} 
\multicolumn{1}{r|}{\color{blue}Royalty ---}     & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{$0.99$} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{$0.99$} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{$0.02$}  & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{$0.98$} \T\\[3ex] \cline{2-2} \cline{4-4} \cline{6-6} \cline{8-8} \cline{10-10} 
\multicolumn{1}{r|}{\color{blue}Masculinity ---} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{$0.99$} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{$0.05$} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{$0.01$}  & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{$0.02$} \T\\[3ex] \cline{2-2} \cline{4-4} \cline{6-6} \cline{8-8} \cline{10-10} 
\multicolumn{1}{r|}{\color{blue}Feminity ---}    & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{$0.05$} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{$0.93$} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{$0.999$} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{$0.94$} \T\\[3ex] \cline{2-2} \cline{4-4} \cline{6-6} \cline{8-8} \cline{10-10} 
\multicolumn{1}{r|}{\color{blue}Age ---}         & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{$0.07$} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{$0.6$}  & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{$0.5$}   & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{$0.1$} \T\\[3ex] \cline{2-2} \cline{4-4} \cline{6-6} \cline{8-8} \cline{10-10} 
\multicolumn{1}{c|}{$\color{blue}\cdots$}        &  \multicolumn{1}{c|}{$\vdots$}   &  \multicolumn{1}{c|}{}     & \multicolumn{1}{c|}{$\vdots$}            &      \multicolumn{1}{c|}{}                 & \multicolumn{1}{c|}{$\vdots$}                    &      \multicolumn{1}{c|}{}                 & \multicolumn{1}{c|}{$\vdots$}                     &        \multicolumn{1}{c|}{}               & \multicolumn{1}{c|}{$\vdots$} \T             
\end{tabularx}
\end{table}

\end{document}

相关内容