Skip to content

TylerAndClaude/Advanced-Dimensionality-Reduction-Classification-Framework

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SpectralInsight: Advanced Dimensionality Reduction & Classification Framework

Python License Machine Learning

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.

📋 Project Overview

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

安装依赖

  1. 克隆项目到本地:
git clone https://github.com/Tyler-Linchenwei/Advanced-Dimensionality-Reduction-Classification-Framework.git
cd github_project
  1. (推荐)创建虚拟环境:
python -m venv venv
# Windows
venv\Scripts\activate
# Linux/Mac
source venv/bin/activate
  1. 安装依赖包:
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: 结果保存路径

📊 输出结果

程序运行后会:

  1. 在终端输出数据加载、预处理和实验过程的详细信息
  2. 显示各维度下的分类准确率
  3. 自动生成并显示对比图表(PCA vs LDA)
  4. 将图表保存到 results/accuracy_curve.png

🔧 代码模块说明

src/data_loader.py

  • DataLoader 类:负责从Excel文件读取数据并进行标签离散化

src/preprocessor.py

  • Preprocessor 类:负责数据划分和标准化

src/models.py

  • KNNClassifier 类:KNN分类器封装
  • evaluate_classification() 函数:评估分类性能
  • create_knn_classifier_fn() 函数:创建分类器函数

src/experiments.py

  • DimensionalityReductionExperiment 类:执行PCA和LDA降维实验

src/visualization.py

  • ResultVisualizer 类:绘制实验结果图表

src/main.py

  • 主程序入口,整合所有模块完成完整流程

📝 使用示例

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 许可证。

👤 作者

  • 林辰炜

🙏 致谢

感谢所有为本项目提供帮助和支持的人!


注意:本项目仅用于学习和研究目的。使用真实数据时,请确保遵守相关数据使用协议和隐私保护规定。

About

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.

Resources

License

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages