pca
语法
pca(ds, [colNames], [k], [normalize], [maxIter], [svdSolver],
[randomState])
详情
对数据源中指定特征列执行主成分分析(Principal Component Analysis,PCA),可用于数据降维、特征提取以及缓解特征之间的线性相关性。
该函数对输入的特征数据进行中心化(或标准化)处理后,通过奇异值分解(Singular Value Decomposition,SVD)提取主成分方向,返回一个字典,包含主成分矩阵(主成分系数)、各主成分的方差贡献率以及奇异值。
参数
ds 是输入数据,可以是内存表,也可以是数据源列表。对于分布式表,通常使用 sqlDS 函数基于 SQL 元代码生成数据源列表,sqlDS
会按查询涉及的分区把数据拆成多个数据源。
colNames 可选参数,是字符串标量或向量,表示参与 PCA 的列名。若不指定,默认使用所有数值列。
k 可选参数,是一个正整数,表示要计算的主成分个数,取值范围为 1 ≤ k ≤ 特征列数。若不指定,则计算全部主成分(个数等于特征列数)。
normalize 可选参数,是一个布尔值,表示是否对各特征列进行标准化,默认值为 false。
-
当 normalize=false 时,仅对各特征列减去均值。
-
当 normalize=true 时,对各特征列先减去均值,再除以该列的样本标准差。
maxIter 可选参数,是一个正整数,表示参数 svdSolver =randomized 时的迭代次数。若未指定,当 k
< 0.1 × cols 时为 7,否则为 4。其中 cols 表示参与 PCA
计算的特征列数量(size(colNames))。
svdSolver:可选参数,字符串标量。指定用于奇异值分解的求解器:
-
"auto"(默认值):根据数据规模和 k 自动选择 "full" 或 "randomized"。
-
"full":精确奇异值分解,适用于样本规模较小或 k 接近特征列数的场景。
-
"randomized":随机化近似奇异值分解,适用于数据量大且 k 远小于特征列数的场景
randomState 可选参数,是一个整数,表示随机数种子,仅在参数 svdSolver ="randomized" 时生效,默认为
int(time(now()))。
返回值
-
components:维度为 k × size(colNames) 的主成分矩阵。每一行对应一个主成分,每一列对应一个输入特征。
-
explainedVarianceRatio:长度为 k 的向量,表示前 k 个主成分各自的方差贡献率,即该主成分解释的方差占总方差的比例。
-
singularValues:长度为 k 的向量,表示前 k 个主成分对应的奇异值。
例子
例 1. 使用内存表进行 PCA 并得到降维结果。
下面的示例将两个特征 x 和 y 降为一个主成分。
// 原始数据
x = [1, 2, 3, 4]
y = [10, 20, 30, 40]
t = table(x as x, y as y)
// 计算 PCA,保留 1 个主成分
model = pca(t, k=1)
print model
/* output:
singularValues->[22.472205054244231]
components->#0 #1
------------------ ------------------
-0.099503719020999 -0.995037190209989
explainedVarianceRatio->[1]
*/
// normalize=false 时,先对每列做中心化
xCentered = x - avg(x)
yCentered = y - avg(y)
// 每行表示一个样本,每列表示一个特征
centeredData = matrix(xCentered, yCentered)
// components 的形状为 主成分数 × 特征数
// 因此需要使用 components.transpose()
// 得到 样本数 × 主成分数 的降维结果
projectedData = centeredData.dot(model.components.transpose())
// 输出降维结果
result = table(
x as x,
y as y,
projectedData.flatten() as component1
)
result
| x | y | component1 |
|---|---|---|
| 1 | 10 | 15.074813431681335 |
| 2 | 20 | 5.024937810560446 |
| 3 | 30 | -5.024937810560446 |
| 4 | 40 | -15.074813431681335 |
例 2. 不同量纲下使用 normalize=true。
height = [160, 165, 170, 175, 180]
income = [3000, 5000, 7000, 9000, 11000]
t = table(height as height, income as income)
// 标准化后进行 PCA
model = pca(t, colNames=`height`income, k=1, normalize=true)
// 按与 PCA 相同的规则标准化原始数据
heightZ = (height - avg(height)) / std(height)
incomeZ = (income - avg(income)) / std(income)
standardizedData = matrix(heightZ, incomeZ)
// 得到一维降维结果
projectedData = standardizedData.dot(
model.components.transpose()
)
result = table(height as height,income as income,projectedData.flatten() as component1)
result| height | income | component1 |
|---|---|---|
| 160 | 3,000 | 1.788854381999832 |
| 165 | 5,000 | 0.894427190999916 |
| 170 | 7,000 | 0 |
| 175 | 9,000 | -0.894427190999916 |
| 180 | 11,000 | -1.788854381999832 |
在这种场景下,normalize=true 可以避免 income 因数值范围远大于 height 而对 PCA 结果产生过大的影响。由于本例中 height 与 income 完全线性相关,标准化前后第一个主成分的方差贡献率均为 1;这里主要用于演示如何在不同量纲下使用 normalize 参数。
例 3. 使用分布式表执行 PCA。
当数据存储在分布式表中时,可以通过 sqlDS 创建数据源,将查询按分区拆分,并在集群上并行执行
PCA。本例主要用于演示分布式调用流程;示例数据由随机数生成,主成分方向和方差贡献率会随随机数据而变化。
if(existsDatabase("dfs://pca_demo")){
dropDatabase("dfs://pca_demo")
}
n = 1000000
date = take(2026.01.01..2026.01.03, n)
sym = take(`C`MS`IBM, n)
feature1 = rand(100.0, n)
feature2 = rand(50.0, n)
feature3 = rand(10.0, n)
data = table(
date as date,
sym as sym,
feature1 as feature1,
feature2 as feature2,
feature3 as feature3
)
// 创建 VALUE 分区方案
db1 = database(, partitionType=VALUE, partitionScheme=2026.01.01..2026.01.03)
db2 = database(, partitionType=VALUE, partitionScheme=`C`MS`IBM)
db = database(directory="dfs://pca_demo", partitionType=COMPO, partitionScheme=[db1, db2])
// 创建分区表
dfsTable = db.createPartitionedTable(table=data, tableName=`featureData, partitionColumns=`date`sym)
// 写入数据
dfsTable.append!(data)
// 创建分布式数据源
ds = sqlDS(
<select feature1, feature2, feature3
from dfsTable
where date between 2026.01.01 : 2026.01.03>
)
// 执行 PCA
model = pca(ds, colNames=`feature1`feature2`feature3, k=2, normalize=true)
print model
/* output:
singularValues->[1000.269775542126808,1000.067908663186813]
components->#0 #1 #2
------------------ ----------------- ------------------
0.729242526483112 0.668147977607071 -0.147592742328707
-0.189972340959701 0.404914955336606 0.894401693097148
explainedVarianceRatio->[0.500100916399505,0.499899083600495]
*/
