

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# PCA 的運作方式
<a name="how-pca-works"></a>

主成分分析 (PCA) 為無人監管的機器學習演算法，可降低資料集內的維數 (功能數量)，同時仍保留所需的資訊。

PCA 透過找出一組稱為*元件*的新特徵來降低維度，該元件是與另一組特徵無關的複合原始特徵。第一個元件說明資料中最有可能出現的變異、第二個元件中次有可能出現的變異，以此類推。

它是無監督的維數降低演算法。在無監督學習情況下，標籤可能會與訓練資料集內未使用的物件建立關聯。

指定矩陣輸入，其中包含了列 ![\[x_1,…,x_n\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/PCA-39b.png)，每個維度都是 `1 * d`，則資料會分割成列的迷你批次，並分散至訓練節點 (工作者)。每個工作者接著會計算資料的總和。不同工作者的摘要接著會彙整至位於運算尾端的單一解法。

**模式**

Amazon SageMaker AI PCA 演算法視情況而定，使用兩種模式之一來計算這些摘要：
+ **一般**：針對含有稀疏資料的資料集以及中等數量的觀察與特徵。
+ **隨機**：針對含有大量觀察與特徵的資料集。此模式使用近似值演算法。

做為演算法的最後一個步驟，它會在彙整的解法上執行單一值分解，接著主要成分會從此衍生。

## 模式 1：一般
<a name="mode-1"></a>

工作者會共同運算 ![\[Equation in text-form: \sum x_i^T x_i\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/PCA-1b.png) 和 ![\[Equation in text-form: \sum x_i\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/PCA-2b.png)。

**注意**  
由於 ![\[Equation in text-form: x_i\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/PCA-3b.png) 是 `1 * d` 列向量，因此 ![\[Equation in text-form: x_i^T x_i\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/PCA-4b.png) 是一個矩陣 (不是純量)。在程式碼中使用行向量可讓我們獲得有效率的快取。

共變異數矩陣會運算為 ![\[Equation in text-form: \sum x_i^T x_i - (1/n) (\sum x_i)^T \sum x_i\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/PCA-32b.png)，而其位於最前面的 `num_components` 個單一向量會組成模型。

**注意**  
若 `subtract_mean` 是 `False`，我們會避免運算及減去 ![\[Equation in text-form: \sum x_i\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/PCA-2b.png)。

當向量的維度 `d` 尺寸夠小，請使用此演算法使 ![\[Equation in text-form: d^2\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/PCA-7b.png) 能容納在記憶體中。

## 模式 2：隨機
<a name="mode-2"></a>

在輸入資料集內的功能數量較多時，我們使用一種方法來逼近共變異數指標。針對每個維度 `b * d` 的迷你批次 ![\[Equation in text-form: X_t\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/PCA-23b.png)，我們會隨機初始化一個 `(num_components + extra_components) * b` 矩陣，讓我們能夠乘以每個迷你批次，來建立一個 `(num_components + extra_components) * d` 矩陣。這些矩陣的總和由工作者運算，而伺服器會在最終 `(num_components + extra_components) * d` 矩陣上執行 SVD。其中的右上方 `num_components` 單一向量為輸入矩陣的頂端單一維度估算值。

使 ![\[Equation in text-form: \ell\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/PCA-38b.png) ` = num_components + extra_components`。指定維度為 `b * d` 的迷你批次 ![\[Equation in text-form: X_t\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/PCA-23b.png)，工作者會抽取一個維度為 ![\[Equation in text-form: \ell * b\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/PCA-38.png) 的隨機矩陣 ![\[Equation in text-form: H_t\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/PCA-24b.png)。根據環境是使用 GPU 或 CPU 以及維度大小為何，來決定矩陣是隨機正負號矩陣 (其中每個項目都是 `+-1`)，或是一個 *FJLT* (快速詹森-林登史特勞斯轉換；如需相關資訊，請參閱 [FJLT Transforms](https://www.cs.princeton.edu/~chazelle/pubs/FJLT-sicomp09.pdf) 以取得延伸閱讀的論文)。工作者接著會運算 ![\[Equation in text-form: H_t X_t\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/PCA-26b.png)，並保持 ![\[Equation in text-form: B = \sum H_t X_t\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/PCA-27b.png)。工作者也會保持 ![\[Equation in text-form: h^T\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/PCA-28b.png)，即 ![\[Equation in text-form: H_1,..,H_T\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/PCA-29b.png) (其中 `T` 是迷你批次的總數) 欄的總和，以及 `s` (所有輸入列的總和)。在處理整個資料碎片後，工作者會傳送 `B`、`h`、`s` 以及 `n` 給伺服器 (輸入行數量)。

將對伺服器的不同輸入表示為 ![\[Equation in text-form: B^1, h^1, s^1, n^1,…\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/PCA-30b.png)。伺服器會運算 `B`、`h`、`s`、`n`，即個別輸入的總和。它接著會運算 ![\[Equation in text-form: C = B – (1/n) h^T s\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/PCA-31b.png)，然後尋找其單一值的分解。使用 `C` 的右上單一向量與單一值做為對問題的約略解法。