基于LightGBM的高光谱图像分类与可视化:完整教程与代码讲解

高光谱图像分类python代码 参考高光谱图像分类python代码 我建立了两个python文件: 1.data.py (负责加载.mat文件,将它转化为,csv文件) 2.train and classify.py (训练并观察分类效果) 下面分别是两段代码(对前面的引用代码做了轻微改动) 1.data.py import matplotlib.pyplot as plt import numpy as np from scipy.io import loadmat import spectral from sklearn impor 阅读详情

✍️ 作者注:本文聚焦于高光谱图像分类任务,使用 WHU-HI-HanChuan 数据集,通过 LightGBM 构建分类模型,并实现整图预测与可视化。全程只使用 Python 和主流机器学习库,强调代码逐段分析,方便初学者的读者理解和上手。

一、背景与数据来源

高光谱遥感图像因其每个像素拥有上百个波段,在地物识别、农业监测、城市分类等方面具有广泛应用价值。本文所使用的数据集来自武汉大学提供的开放数据集:

📌 数据名称:WHU-Hi-HanChuan
📌 下载地址:https://rsidea.whu.edu.cn/resource_WHUHi_sharing.htm
📌 图像尺寸:height × width × bands
📌 标签说明:每个像素属于一个地物类别,标签为0的像素表示无标注区域。

参考文献:

[1] Y. Zhong, X. Hu, C. Luo, X. Wang, J. Zhao, and L. Zhang, “WHU-Hi: UAV-borne hyperspectral with high spatial resolution (H2) benchmark datasets and classifier for precise crop identification based on deep convolutional neural network with CRF”, Remote Sens. Environ., vol. 250, pp. 112012, 2020.

[2] Y. Zhong, X. Wang, Y. Xu, S. Wang, T. Jia, X. Hu, J. Zhao, L. Wei, and L. Zhang, “Mini-UAV-borne hyperspectral remote sensing: From observation and processing to applications”, IEEE Geosci. Remote Sens. Mag., vol. 6, no. 4, pp. 46-62, Dec. 2018.

大家也可以替换成自己或者其他数据集进行实验。

二、实验目标与思路

我们将实现以下内容:

  1. 加载并预处理 .mat 格式的高光谱图像与标签;
  2. 训练一个 LightGBM 分类器;
  3. 在测试集上评估分类性能;
  4. 对整幅图像进行像素级预测;
  5. 可视化分类结果。

三、完整代码与详细讲解

(1)环境配置与中文字体支持

为避免中文显示乱码,需在绘图前设置中文字体:

import matplotlib
matplotlib.rcParams['font.family'] = 'SimHei'        # 设置为黑体
matplotlib.rcParams['axes.unicode_minus'] = False    # 正确显示负号

(2)导入基础模块

import scipy.io
import os
import numpy as np
from lightgbm import LGBMClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
import matplotlib.pyplot 
基于Phython利用CatBoost与PCA解析高光谱图像分类:WHU-HI-HanChuan数据集实战教程 本文以武汉大学WHU-Hi-HanChuan高光谱遥感数据集为例,提出了一套完整的遥感分类流程。首先采用PCA对274个波段降维至30维(保留99%以上信息),然后构建CatBoost分类模型,在测试集上取得较高准确率。研究重点包括:1)通过主成分特征重要性分析识别关键光谱特征;2)利用网格搜索评估参数敏感性;3)实现全图分类可视化。结果展示了CatBoost在高光谱分类中的优势,并提供了特征可解释性分析。代码开源,包含数据预处理、降维、建模、评估全流程,可作为高光谱遥感分类的入门教程。 阅读详情

相关推荐

利用 Python + t-SNE 可视化高光谱遥感影像特征分布:WHU-HI-HanChuan 案例实战

本文基于武汉大学WHU-Hi-HanChuan高光谱遥感数据集,演示了使用PCA、LLE和t-SNE降维算法进行遥感影像特征可视化的方法。文章详细介绍了数据预处理、随机采样、特征提取(PCA和LLE)及t-SNE降维的完整Python实现流程,并对比了不同降维方法的可视化效果。结果表明,t-SNE能有效展示高光谱数据的聚类趋势和类别边界,为遥感影像分析提供了直观的特征分布可视化工具。文末还建议了UMAP、AutoEncoder等进阶研究方向。

lestatlu的博客 896

高光谱图像分类源代码(UI,波段选择,独立成分分析,人工蜂群,SVM,)

工程效果 介绍 用pyqt搭建了一个UI,使用了ICA,FCM-ABC两种方法做了波段选择,和全波段三种数据对比,通过SVM(支持向量机)做了高光谱图像分类,并计算OA 涉及 1. Pavia University 图像分类 用的是支持向量机分类,简单效果好 2. pyqt界面 本来想用qt,但是写分类这些,python挺好用的,就用python qt写的界面,并且UI和逻辑是分类的。ui.py是整个界面的文件,progressBar.py是进度圈的代码 3. 波段选择算法 3.1 模糊C均值聚类+人工蜂

YH_24H的博客 2212

Python处理高光谱数据#2 图像分类

基于Python的高光谱图像监督分类与非监督分类 高光谱数据:点击此处可下载 用到的库:matplotlib、scipy、spectral、numpy 主要内容:监督分类(最大似然法)与非监督分类(K-means)及精度评定 欢迎有兴趣的朋友交流指点。最后,废话不多说直接上代码???? import matplotlib.pyplot as plt from scipy.io import loadmat import spectral as spy import numpy as np def

qq_273058791的博客 6863

图像理解与智能处理高光谱图像分类代码+课程报告

基于PaviaU数据集的高光谱图像分类,包含两种方法,第一种是MATLAB代码,通过PCA降维+garbor特征提取+svm分类,第二种是python代码,通过cnn网络进行特征提取分类。

高光谱分类全套代码3D_CNN模型(pytorch)

1、包含训练代码、预测代码、数据划分代码、网络代码等,采用pytorch框架所写。 2、换个数据集或者自己数据就可以使用。 3、保证运行成功。

从噪声中提取真相:LabVIEW振动信号积分处理的降噪艺术

本文深入探讨了LabVIEW在振动信号积分处理中的降噪技术,揭示了如何通过选择性频率加权从噪声中提取关键故障特征。详细介绍了时域和频域积分方法的实现原理及优化策略,并结合工业案例展示了多轴信号联合降噪的实际应用效果,为设备状态监测提供了高效解决方案。

weixin_29164185的博客 294

为什么RingMo能成为遥感领域的首个生成式自监督基础模型?

RingMo作为遥感领域首个生成式自监督基础模型,通过创新的图像掩码建模技术解决了传统方法在遥感图像处理中的挑战。该模型利用两百万张多元数据集进行预训练,在小目标检测、场景分类等任务上表现卓越,显著提升了性能指标。RingMo的技术突破为遥感图像解译提供了高效解决方案,降低了标注成本并增强了模型泛化能力。

ll5678的博客 686

PHP证书图片生成

$realname = "姓名"; $schoolname = "学校"; $idcard = "身份证号"; $image = imagecreatefrompng('certification.png'); // 证书模版图片文件的路径 $red = imagecolorallocate($image,00,00,00); // 字体颜色

4032

ZYNQ烧写程序到flash

1.打开SDK创建FBSl项目 点击finish 2.创建BootImage 导入fsbl.elf、.bin、.elf三个文件,Create Image 生成BOOT.bin文件 3.将BOOT文件烧录到FLASH image File选择2中生成的BOOT.bin文件,FSBL File选择fsbl.elf文件,连接好ZYNQ的Jtag,点击Program,即可烧录。 4.烧录程序到FLASH失败 如果是第一次烧录,需更改创建的fsbl工程下的main函数,在如下代码处加入BootModeRegi

qq_40603614的博客 8237

MATLAB极坐标伪彩色圆图绘制之polarPcolor函数

polarPcolor函数介绍 我们经常在论文或期刊上看到如下图这种以色调冷暖代表数值大小的极坐标圆图, 而function [varargout] = polarPcolor(R,theta,Z,varargin)可以很好的帮助我们快速绘制这类极坐标圆图。它非常适合雷达或激光雷达的平面位置指示器。使用polarPcolor函数时,我们只需根据手中数据构造相应的二维矩阵Z即可,十分便捷。varargin值为绘制模式选择。R值为半径坐标一维矩阵,theta值为角坐标一维矩阵。 代码示例 这里使用了海洋表面耀光

qq_19512613的博客 1万+

激光雷达与相机时间戳同步:原理、触发方案与工程实现

目录一、引言:时间不同步,多传感器融合必出问题二、核心原理:相机成像机制与触发模式(同步的基础)2.1 相机成像的两个核心阶段:曝光与读出(1)非交叠曝光(2)交叠曝光2.2 相机触发模式:内触发 vs 外触发(1)内触发(2)外触发2.3 关键信号:Strobe信号与预输出(同步的核心技巧)三、三种主流时间戳同步方案(工程落地首选)3.1 方案一:统一硬触发(精度最高,高端场景首选)核心原理核心配置要点优缺点适用场景3.2 方案二:软触发+硬触发(Strobe预输出,车载量产首选)核心原理核心配置要点优缺

getusushu的博客 194

【深度学习新浪潮】OpenClaw技术架构详解:从核心组件到生产级优化

OpenClaw 以三层解耦架构 + 四大核心模块为基础,实现了本地优先、易部署、易扩展、易优化的 AI 智能体平台。开发者可通过极简部署快速落地,再通过量化、缓存、并发、分布式等手段持续优化性能,适合从个人助理到企业级自动化的全场景应用。

专注:太空算力、AI infra、异构调度、大模型成本工程 2914

跨应用服务器的自定义 IDoc 传输实战:以 CATSDB 工时为例,打通两套 SAP 系统的数据通道

本文介绍了在企业多SAP系统环境下,使用SAP ALE/IDoc技术实现工时数据跨系统传输的解决方案。文章从技术选型、系统架构到具体配置步骤进行了详细说明,重点阐述了IDoc/ALE在异步传输、全链路追踪和数据重放方面的优势。 文章详细讲解了端到端实施流程,包括Logical System配置、RFC Destination创建、Message Type定义、Segment和Basic Type设计等关键步骤。同时提供了出站封装的ABAP代码示例,强调调用MASTER_IDOC_DISTRIBUTE函数后执

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 164

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

上一篇: 遥感数据(GEE平台)介绍及简单可视化(六)|基于GHSL的城市非住宅建设用地识别与空间分析
下一篇: 遥感数据(GEE平台)介绍及简单可视化(七)|基于GLCF的Landsat全球内陆水体数据集的提取、分类与面积统计分析
遥感AI实战
博客等级 码龄9年 1002粉丝 107原创
评论
成就一亿技术人!
拼手气红包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、付费专栏及课程。

余额充值