SpectralInsight is an advanced machine learning framework for spectral data analysis, implementing PCA (Principal Component Analysis) and LDA (Linear Discriminant Analysis) dimensionality reduction techniques combined with KNN (K-Nearest Neighbors) classification to evaluate classification performance across different dimensional spaces.
SpectralInsight is designed for binary classification tasks on high-dimensional spectral data. By comparing PCA and LDA dimensionality reduction methods across different dimensions, it provides insights into how dimensionality reduction affects classification performance.
- ✅ 数据加载与标签离散化:从Excel文件读取光谱数据,自动解析并离散化标签
- ✅ 数据预处理:训练/测试集划分、特征标准化
- ✅ 降维实验:PCA(无监督)和LDA(有监督)降维
- ✅ 分类评估:使用KNN分类器评估不同维度下的分类准确率
- ✅ 结果可视化:绘制降维维度与分类准确率的关系曲线
github_project/
├── src/ # 源代码目录
│ ├── __init__.py
│ ├── data_loader.py # 数据加载模块
│ ├── preprocessor.py # 数据预处理模块
│ ├── models.py # 模型定义模块
│ ├── experiments.py # 实验执行模块
│ ├── visualization.py # 可视化模块
│ └── main.py # 主程序入口
├── config/ # 配置文件目录
│ └── config.py # 项目配置参数
├── data/ # 数据目录
│ └── excel.xlsx # 光谱数据文件(需自行准备)
├── results/ # 结果输出目录
│ └── .gitkeep
├── requirements.txt # Python依赖包
├── .gitignore # Git忽略文件配置
└── README.md # 项目说明文档
- Python 3.8+
- pip
- 克隆项目到本地:
git clone https://github.com/Tyler-Linchenwei/Advanced-Dimensionality-Reduction-Classification-Framework.git
cd github_project- (推荐)创建虚拟环境:
python -m venv venv
# Windows
venv\Scripts\activate
# Linux/Mac
source venv/bin/activate- 安装依赖包:
pip install -r requirements.txt将你的光谱数据文件 excel.xlsx 放置在 data/ 目录下。
数据格式要求:
- 第一行为表头
- 第一列为标签(可以是数值或形如
1-2-4的字符串格式) - 从第二列开始为光谱特征数据(数值型)
💡 提示:如果未找到数据文件,程序会自动生成模拟数据用于演示。
python src/main.py或者:
python -m src.main项目参数集中在 config/config.py 文件中,你可以根据需要修改:
-
数据配置 (
DATA_CONFIG):file_path: 数据文件路径threshold: 分类阈值(默认50.0)
-
预处理配置 (
PREPROCESSING_CONFIG):test_size: 测试集比例(默认0.2)random_state: 随机种子(默认42)
-
模型配置 (
MODEL_CONFIG):n_neighbors: KNN邻居数(默认3)
-
实验配置 (
EXPERIMENT_CONFIG):max_dim: 最大测试维度(默认10)
-
可视化配置 (
VISUALIZATION_CONFIG):figsize: 图表尺寸save_path: 结果保存路径
程序运行后会:
- 在终端输出数据加载、预处理和实验过程的详细信息
- 显示各维度下的分类准确率
- 自动生成并显示对比图表(PCA vs LDA)
- 将图表保存到
results/accuracy_curve.png
DataLoader类:负责从Excel文件读取数据并进行标签离散化
Preprocessor类:负责数据划分和标准化
KNNClassifier类:KNN分类器封装evaluate_classification()函数:评估分类性能create_knn_classifier_fn()函数:创建分类器函数
DimensionalityReductionExperiment类:执行PCA和LDA降维实验
ResultVisualizer类:绘制实验结果图表
- 主程序入口,整合所有模块完成完整流程
from src.data_loader import DataLoader
from src.preprocessor import Preprocessor
from src.models import create_knn_classifier_fn
from src.experiments import DimensionalityReductionExperiment
# 1. 加载数据
loader = DataLoader(threshold=50.0)
X, y = loader.load_and_discretize("data/excel.xlsx")
# 2. 预处理
preprocessor = Preprocessor(test_size=0.2, random_state=42)
X_train, X_test, y_train, y_test = preprocessor.preprocess(X, y)
# 3. 运行实验
classifier_fn = create_knn_classifier_fn(n_neighbors=3)
experiment = DimensionalityReductionExperiment(max_dim=10, classifier_fn=classifier_fn)
pca_results, lda_results = experiment.run_full_experiment(X_train, y_train, X_test, y_test)欢迎提交 Issue 和 Pull Request!
本项目采用 MIT 许可证。
- 林辰炜
感谢所有为本项目提供帮助和支持的人!
注意:本项目仅用于学习和研究目的。使用真实数据时,请确保遵守相关数据使用协议和隐私保护规定。