首页 分享 python深度学习之用lightgbm算法实现鸢尾花种类的分类任务实战源码

python深度学习之用lightgbm算法实现鸢尾花种类的分类任务实战源码

来源:花匠小妙招 时间:2024-11-06 23:39

本代码以sklearn包中自带的鸢尾花数据集为例,用lightgbm算法实现鸢尾花种类的分类任务。
参考来源:
https://lightgbm.readthedocs.io/en/latest/Python-Intro.html

#!/usr/bin/env python # -*- coding:utf-8 -*- # Author's_name_is_NIKOLA_SS #pip install lightgbm -i https://pypi.mirrors.ustc.edu.cn/simple/ import json import lightgbm as lgb import pandas as pd from sklearn.metrics import mean_squared_error from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.datasets import make_classification iris = load_iris() # 载入鸢尾花数据集 data = iris.data target = iris.target X_train, X_test, y_train, y_test = train_test_split( data, target, test_size=0.2 ) # 加载你的数据 # print('Load data...') # df_train = pd.read_csv('../regression/regression.train', header=None, sep='t') # df_test = pd.read_csv('../regression/regression.test', header=None, sep='t') # # y_train = df_train[0].values # y_test = df_test[0].values # X_train = df_train.drop(0, axis=1).values # X_test = df_test.drop(0, axis=1).values # 创建成lgb特征的数据集格式 lgb_train = lgb.Dataset( X_train, y_train ) # 将数据保存到LightGBM二进制文件将使加载更快 lgb_eval = lgb.Dataset( X_test, y_test, reference=lgb_train ) # 创建验证数据 # 将参数写成字典下形式 params = { 'task': 'train', 'boosting_type': 'gbdt', # 设置提升类型 'objective': 'regression', # 目标函数 'metric': {'l2', 'auc'}, # 评估函数 'num_leaves': 31, # 叶子节点数 'learning_rate': 0.05, # 学习速率 'feature_fraction': 0.9, # 建树的特征选择比例 'bagging_fraction': 0.8, # 建树的样本采样比例 'bagging_freq': 5, # k 意味着每 k 次迭代执行bagging 'verbose': 1 # <0 显示致命的, =0 显示错误 (警告), >0 显示信息 } print( 'Start training...' ) # 训练 cv and train gbm = lgb.train( params, lgb_train, num_boost_round=20, valid_sets=lgb_eval, early_stopping_rounds=5 ) # 训练数据需要参数列表和数据集 print( 'Save model...' ) gbm.save_model( 'model.txt' ) # 训练后保存模型到文件 print( 'Start predicting...' ) # 预测数据集 y_pred = gbm.predict( X_test, num_iteration=gbm.best_iteration ) # 如果在训练期间启用了早期停止,可以通过best_iteration方式从最佳迭代中获得预测 # 评估模型 print( 'The rmse of prediction is:', mean_squared_error( y_test, y_pred ) ** 0.5 ) # 计算真实值和预测值之间的均方根误差

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960

运行之后的结果输出如下:

Start training... [LightGBM] [Warning] Auto-choosing row-wise multi-threading, the overhead of testing was 0.000064 seconds. You can set `force_row_wise=true` to remove the overhead. And if memory is not enough, you can set `force_col_wise=true`. [LightGBM] [Info] Total Bins 90 [LightGBM] [Info] Number of data points in the train set: 120, number of used features: 4 [LightGBM] [Info] Start training from score 1.008333 [LightGBM] [Warning] No further splits with positive gain, best gain: -inf [1]valid_0's auc: 1valid_0's l2: 0.702787 Training until validation scores don't improve for 5 rounds [LightGBM] [Warning] No further splits with positive gain, best gain: -inf [2]valid_0's auc: 1valid_0's l2: 0.64447 [LightGBM] [Warning] No further splits with positive gain, best gain: -inf [3]valid_0's auc: 1valid_0's l2: 0.591793 [LightGBM] [Warning] No further splits with positive gain, best gain: -inf [4]valid_0's auc: 1valid_0's l2: 0.542737 [LightGBM] [Warning] No further splits with positive gain, best gain: -inf [5]valid_0's auc: 1valid_0's l2: 0.499044 [LightGBM] [Warning] No further splits with positive gain, best gain: -inf [6]valid_0's auc: 1valid_0's l2: 0.458074 Early stopping, best iteration is: [1]valid_0's auc: 1valid_0's l2: 0.702787 Save model... Start predicting... The rmse of prediction is: 0.8383238691881394 Process finished with exit code 0

123456789101112131415161718192021222324252627

参考来源于网络。

相关知识

机器学习(三):感知器算法实现鸢尾花分类项目实战
基于YOLOv8深度学习的葡萄病害智能诊断与防治系统【python源码+Pyqt5界面+数据集+训练代码】深度学习实战
基于YOLOv8深度学习的水稻害虫检测与识别系统【python源码+Pyqt5界面+数据集+训练代码】目标检测、深度学习实战
基于YOLOv8深度学习的102种花卉智能识别系统【python源码+Pyqt5界面+数据集+训练代码】目标识别、深度学习实战
基于YOLOv8深度学习的智能玉米害虫检测识别系统【python源码+Pyqt5界面+数据集+训练代码】目标检测、深度学习实战
基于YOLOv8深度学习的智能小麦害虫检测识别系统【python源码+Pyqt5界面+数据集+训练代码】目标检测、深度学习实战
【机器学习】鸢尾花分类:机器学习领域经典入门项目实战
深度学习实战:AlexNet实现花图像分类
【花卉识别系统】Python+卷积神经网络算法+人工智能+深度学习+图像识别+算法模型
深度学习及其应用

网址: python深度学习之用lightgbm算法实现鸢尾花种类的分类任务实战源码 https://www.huajiangbk.com/newsview387333.html

所属分类:花卉
上一篇: 花信园艺公共微信平台...
下一篇: 故宫花信|鸢尾:翩翩对舞风轻,团

推荐分享