如何在 LaTeX 中进行 PCA 并将其全部可视化?

如何在 LaTeX 中进行 PCA 并将其全部可视化?

假设我有 3 个向量:

$$
    \begin{pmatrix}
    1 \\
    1 \\
    1 \\
    \end{pmatrix}

    \begin{pmatrix}
    1 \\
    4 \\
    5 \\
    \end{pmatrix}

    \begin{pmatrix}
    3 \\
    4 \\
    7 \\
    \end{pmatrix}
$$

我想在 TikZ/PGF 中绘制它们的 2d PCA,是否可以不退出 latex(通过某些包)或者必须在外部计算 PCA 并仅将数据提供给 TikZ/pgf?

答案1

我不确定这是否是你要找的答案。对我来说,从这篇维基百科文章看起来你需要做的就是能够反转一般的坐标变换。这就是下面的代码所做的。

在此处输入图片描述

\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{subcaption}
\usepackage{tikz}
\usepackage{tikz-3dplot}
\def\myparse(#1,#2,#3){\xdef\myx{#1}\xdef\myy{#2}\xdef\myz{#3}}
\tikzset{define x/.code={\myparse#1%
\xdef\globxx{\myx}%
\xdef\globxy{\myy}%
\xdef\globxz{\myz}},
define y/.code={\myparse#1%
\xdef\globyx{\myx}%
\xdef\globyy{\myy}%
\xdef\globyz{\myz}},
define z/.code={\myparse#1%
\xdef\globzx{\myx}%
\xdef\globzy{\myy}%
\xdef\globzz{\myz}},
install trafo/.code={\tikzset{x={(\globxx,\globxy,\globxz)},%
y={(\globyx,\globyy,\globyz)},z={(\globzx,\globzy,\globzz)}}},
install inverse trafo/.code={%
\pgfmathsetmacro{\mydet}{\globxx*(\globzz*\globyy-\globzy*\globyz)
- \globyx*(\globzz*\globxy-\globzy*\globxz)
+ \globzx*(\globyz*\globxy-\globyy*\globxz)}
%\typeout{det=\mydet}        
\pgfmathsetmacro{\myxx}{(\globzz*\globyy-\globzy*\globyz)/\mydet}
\pgfmathsetmacro{\myxy}{-(\globzz*\globyx-\globzx*\globyz)/\mydet}   
\pgfmathsetmacro{\myxz}{(\globzy*\globyx-\globzx*\globyy)/\mydet}
\tikzset{x={(\myxx,\myxy,\myxz)}}  
\pgfmathsetmacro{\myyx}{-(\globzz*\globxy-\globzy*\globxz)/\mydet}  
\pgfmathsetmacro{\myyy}{(\globzz*\globxx-\globzx*\globxz)/\mydet}
\pgfmathsetmacro{\myyz}{-(\globzy*\globxx-\globzx*\globxy)/\mydet}   
\tikzset{y={(\myyx,\myyy,\myyz)}}  
\pgfmathsetmacro{\myzx}{(\globyz*\globxy-\globyy*\globxz)/\mydet} 
\pgfmathsetmacro{\myzy}{-(\globyz*\globxx-\globyx*\globxz)/\mydet}
\pgfmathsetmacro{\myzz}{(\globyy*\globxx-\globyx*\globxy)/\mydet} 
\tikzset{z={(\myzx,\myzy,\myzz)}}  
%\typeout{(\myxx,\myxy,\myxz),(\myyx,\myyy,\myyz),(\myzx,\myzy,\myzz)}
}}
\begin{document}
This is an attempt to provide some sort of an answer. Let's start by recalling
what Ti\emph{k}Z can do. Together with \texttt{tikz-3dplot} it allows you to
draw vectors in 3d. \texttt{tikz-3dplot} has the virtue of providing us with
proections of objects described by 3d orthogonal coordinate systems, see
figure~\ref{fig:3d1}.

Generally, regardless of whether or not we use \texttt{tikz-3dplot}, we can
install arbitrary coordinate systems with e.g. 
\begin{quote}
\texttt{\textbackslash
begin\{scope\}[x=\{($x_1$,$x_2$,$x_3$)\},y=\{($y_1$,$y_2$,$y_3$)\},z=\{($z_1$,$z_2$,$z_3$)\}]}
\end{quote}
In this answer, we keep track of these transformations in order to be able to
invert them. Therefore, coordinate transformations now proceed in two steps:
\begin{enumerate}
 \item We define the vectors e.g.\ with
 \begin{quote}
\texttt{\textbackslash
begin\{scope\}[define x=\{($x_1$,$x_2$,$x_3$)\},\dots]}
\end{quote}
 where the omission stands for analogous expressions for \texttt{y} and
 \texttt{z}.
 \item Then we can install a coordinate system simply with the key
 \texttt{install trafo}, see the red vectors in figure~\ref{fig:3d1}. (The red
 vectors got rescaled because your vectors have largish entries.)
 \item  And, this is the potentially new point here, we can install the inverse
 transformation with the \texttt{install inverse trafo} key. This is illustrated
 by the blue vectors in figure~\ref{fig:3d2}. (Note that if the
 vectors do not span the full 3d space, the determinant vanishes. However, at
 present there is no real error handler at work. 
\end{enumerate}

\begin{figure}[htb]
\hfill
\begin{subfigure}{0.4\textwidth}
\centering
\tdplotsetmaincoords{70}{110} % sets the view angles, see the tik-3dplot manual for details 
\begin{tikzpicture}[tdplot_main_coords]
\draw[-latex] (0,0,0) -- (2,0,0) node[pos=1.1]{$x$};
\draw[-latex] (0,0,0) -- (0,2,0) node[pos=1.1]{$y$};
\draw[-latex] (0,0,0) -- (0,0,2) node[pos=1.1]{$z$};
\begin{scope}[define x={(1,1,1)},define y={(1,4,5)},define z={(3,4,7)},
install trafo]
\begin{scope}[red,scale=1/25]
\draw[-latex] (0,0,0) -- (2,0,0) node[pos=1.1]{$x'$};
\draw[-latex] (0,0,0) -- (0,2,0) node[pos=1.1]{$y'$};
\draw[-latex] (0,0,0) -- (0,0,2) node[pos=1.1]{$z'$};
\end{scope}
\end{scope}
\end{tikzpicture}
\caption{Vectors in 3d and non--orthogonal basis vectors (red).}
\label{fig:3d1}
\end{subfigure}
\hfill
\begin{subfigure}{0.4\textwidth}
\centering
\tdplotsetmaincoords{70}{110}
\begin{tikzpicture}[tdplot_main_coords]
\draw[-latex] (0,0,0) -- (2,0,0) node[pos=1.1]{$x$};
\draw[-latex] (0,0,0) -- (0,2,0) node[pos=1.1]{$y$};
\draw[-latex] (0,0,0) -- (0,0,2) node[pos=1.1]{$z$};
\begin{scope}[define x={(1,1,1)},define y={(1,4,5)},define z={(3,4,7)},
install inverse trafo,blue]
\draw[-latex] (0,0,0) -- (2,0,0) node[pos=1.1]{$x'$};
\draw[-latex] (0,0,0) -- (0,2,0) node[pos=1.1]{$y'$};
\draw[-latex] (0,0,0) -- (0,0,2) node[pos=1.1]{$z'$};
\end{scope}
\end{tikzpicture}
\caption{Vectors in 3d and non--orthogonal basis vectors (blue).}
\label{fig:3d2}
\end{subfigure}
\hfill
\caption{Non--orthogonal 3d coordinate systems.}
\end{figure}
\end{document}

相关内容