基于PyQt6 + Scikit-learn开发的桌面应用程序,一键识别鸢尾花品种。
| 类别 |
技术 |
| UI框架 |
PyQt6 |
| 机器学习 |
scikit-learn (RandomForest) |
| 打包 |
PyInstaller |
| 依赖管理 |
requirements.txt |
二、项目结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| iris-classifier/ ├── main.py ├── main_window.py ├── train_model.py ├── requirements.txt ├── iris_classifier.spec ├── build.bat / build.sh ├── models/ │ ├── iris_rf_model.joblib │ └── model_metadata.json ├── utils/ │ ├── model_utils.py │ └── style_utils.py └── components/ ├── result_chart.py ├── history_panel.py └── about_dialog.py
|
三、核心功能
界面布局

快捷键
| 键 |
功能 |
F5 |
智能预测 |
F9 |
随机测试 |
Ctrl+L |
清空输入 |
Ctrl+T |
切换主题 |
Ctrl+E |
导出CSV |
四、核心代码
1. 主窗口入口
1 2 3 4 5 6 7 8 9
| import sys from PyQt6.QtWidgets import QApplication
app = QApplication(sys.argv) from main_window import IrisClassifierApp window = IrisClassifierApp(app) window.show() sys.exit(app.exec())
|
2. 预测逻辑
1 2 3 4 5 6 7
| classifier = IrisClassifier() classifier.load_model()
features = [SL.value, SW.value, PL.value PW.value] result = classifier.predict(features)
|
3. 多线程预测
1 2 3 4 5 6
| class PredictionThread(QThread): prediction_ready = pyqtSignal(dict) def run(self): result = self.classifier.predict(self.features) self.prediction_ready.emit(result)
|
4. 模型训练
1 2 3 4 5 6
| from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(n_estimators=100, max_depth=5, n_jobs=-1) model.fit(X_train, y_train) joblib.dump(model, 'models/iris_rf_model.joblib')
|
5. 可视化组件
1 2 3 4 5
| class ResultChartWidget(QWidget): def update(self, probs, imp=None): self.pie.set_probs(probs) self.bar.set_imp(imp)
|
五、特色功能
| 功能 |
说明 |
| 双主题 |
浅色/深色一键切换 |
| 快速预设 |
Setosa/Versicolor/Virginica一键加载 |
| 随机测试 |
随机抽取样本测试 |
| 历史记录 |
保存最近100条预测 |
| CSV导出 |
导出预测历史 |
| 特征重要性 |
显示各特征贡献度 |
六、打包发布
1 2 3 4 5 6 7 8
| build.bat
chmod +x build.sh && ./build.sh
pyinstaller iris_classifier.spec --windowed
|
七、运行
1 2 3 4 5 6 7 8
| pip install -r requirements.txt
python train_model.py
python main.py
|
#AI学习 #Python #PyQt6 #桌面开发 #机器学习