本代码以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 ) # 计算真实值和预测值之间的均方根误差
运行之后的结果输出如下:
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: 1 valid_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: 1 valid_0's l2: 0.64447
[LightGBM] [Warning] No further splits with positive gain, best gain: -inf
[3] valid_0's auc: 1 valid_0's l2: 0.591793
[LightGBM] [Warning] No further splits with positive gain, best gain: -inf
[4] valid_0's auc: 1 valid_0's l2: 0.542737
[LightGBM] [Warning] No further splits with positive gain, best gain: -inf
[5] valid_0's auc: 1 valid_0's l2: 0.499044
[LightGBM] [Warning] No further splits with positive gain, best gain: -inf
[6] valid_0's auc: 1 valid_0's l2: 0.458074
Early stopping, best iteration is:
[1] valid_0's auc: 1 valid_0's l2: 0.702787
Save model...
Start predicting...
The rmse of prediction is: 0.8383238691881394
Process finished with exit code 0
参考来源于网络。