在上一讲(案例⑩)里,我们介绍了 PCA / ICA / NMF 三个主流分解方法。 其实 sklearn.decomposition 还有更多工具,适合不同场景:
-
KernelPCA:用核函数把数据映射到高维空间再PCA,适合非线性分布。
-
IncrementalPCA:分批处理大数据(内存有限时特别有用)。
-
DictionaryLearning / SparseCoder:学习一组“字典原子”,用稀疏表示原始数据。
另外,往期内容和数据链接如下:
🧩 方法简述
-
KernelPCA
-
非线性降维方法,支持
rbf、poly、sigmoid等核函数。 -
可处理非线性可分数据,但计算成本较高。
-
-
IncrementalPCA
-
适合内存放不下的超大样本,可分批
partial_fit。 -
结果与 PCA 相近。
-
-
DictionaryLearning / SparseCoder
-
假设数据可由一组“稀疏基向量”组合而成。
-
遥感中常用于“端元提取”或纹理特征建模。
-
💻 示例代码:多方法降维对比
# -*- coding: utf-8 -*-
"""
Sklearn案例⑪:更多分解方法
演示 KernelPCA / IncrementalPCA / DictionaryLearning
"""
import os, numpy as np, scipy.io as sio, matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import KernelPCA, IncrementalPCA, DictionaryLearning
# ===== 参数 =====
DATA_DIR = "your_path" # ← 修改为你的路径
SEED = 42
# ===== 1. 加载数据 =====
X = sio.loadmat(os.path.join(DATA_DIR,"KSC.mat"))["KSC"].astype(np.float32)
Y = sio.loadmat(os.path.join(DATA_DIR,"KSC_gt.mat"))["KSC_gt"].astype(int)
coords = np.argwhere(Y!=0)
X_all = X[coords[:,0], coords[:,1]]
y_all = Y[coords[:,0], coords[:,1]]-1
scaler = StandardScaler().fit(X_all)
X_all = scaler.transform(X_all)
# ===== 2. 定义方法 =====
models = {
"KernelPCA(rbf)": KernelPCA(n_components=2, kernel="rbf", gamma=0.01, random_state=SEED),
"IncrementalPCA": IncrementalPCA(n_components=2, batch_size=200),
"DictionaryLearning": DictionaryLearning(n_components=2, random_state=SEED, max_iter=500)
}
# ===== 3. 拟合与变换 =====
results = {}
for name, model in models.items():
Xt = model.fit_transform(X_all)
results[name] = Xt
# ===== 4. 可视化 =====
plt.figure(figsize=(12,4))
for i,(name,Xt) in enumerate(results.items(),1):
plt.subplot(1,3,i)
plt.scatter(Xt[:,0], Xt[:,1], c=y_all, s=6, cmap="tab20")
plt.title(name); plt.axis("off")
plt.suptitle("Sklearn.decomposition 更多方法对比", fontsize=14)
plt.tight_layout(rect=[0,0,1,0.95])
plt.show()
🔍 结果观察
-
KernelPCA:非线性映射后,类间可分性可能更强。
-
IncrementalPCA:结果类似 PCA,但能处理百万级数据,不怕内存不足。
-
DictionaryLearning:结果更像是“稀疏特征空间”,有时不一定利于可视化,但在端元/纹理提取里很常用。

✅ 总结
-
sklearn.decomposition不止有 PCA / ICA / NMF,KernelPCA / IncrementalPCA / DictionaryLearning 等也很实用。 -
如何选择?
-
数据量小、先做尝试 → PCA
-
数据非线性明显 → KernelPCA
-
数据太大、内存吃不消 → IncrementalPCA
-
需要稀疏表示 / 端元分析 → DictionaryLearning
-
欢迎大家关注下方公众号获取更多内容!

被折叠的 条评论
为什么被折叠?



