遥感&机器学习入门实战教程|Sklearn案例⑪:更多分解方法(KernelPCA / DictionaryLearning等)

在上一讲(案例⑩)里,我们介绍了 PCA / ICA / NMF 三个主流分解方法。 其实 sklearn.decomposition 还有更多工具,适合不同场景:

  • KernelPCA:用核函数把数据映射到高维空间再PCA,适合非线性分布。

  • IncrementalPCA:分批处理大数据(内存有限时特别有用)。

  • DictionaryLearning / SparseCoder:学习一组“字典原子”,用稀疏表示原始数据。

另外,往期内容和数据链接如下:

遥感&机器学习入门实战教程 数据集分享

🧩 方法简述

  1. KernelPCA

    • 非线性降维方法,支持 rbfpolysigmoid 等核函数。

    • 可处理非线性可分数据,但计算成本较高。

  2. IncrementalPCA

    • 适合内存放不下的超大样本,可分批 partial_fit

    • 结果与 PCA 相近。

  3. 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

欢迎大家关注下方公众号获取更多内容!

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

遥感AI实战

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值