day02-xgboost和lightgbm简单实现

xgboost

# 从sklearn调入所需要的包
from sklearn import datasets
from sklearn.model_selection import train_test_split
import xgboost as xgb
import numpy as np
import pandas as pd
# 导入精准率和召回率
from sklearn.metrics import precision_score,recall_score

导入鸢尾花数据

iris = datasets.load_iris()
data = iris.data
label = iris.target

数据展示

print(pd.DataFrame(data).head())
    0    1    2    3
0  5.1  3.5  1.4  0.2
1  4.9  3.0  1.4  0.2
2  4.7  3.2  1.3  0.2
3  4.6  3.1  1.5  0.2
4  5.0  3.6  1.4  0.2
print(pd.DataFrame(label).head())
   0
0  0
1  0
2  0
3  0
4  0

pandas格式

data1 = pd.DataFrame(data)
data1.columns = ['sepal_l','sepal_w','petal_l','petal_w']
print(data1.head())

结果:

   sepal_l  sepal_w  petal_l  petal_w
0      5.1      3.5      1.4      0.2
1      4.9      3.0      1.4      0.2
2      4.7      3.2      1.3      0.2
3      4.6      3.1      1.5      0.2
4      5.0      3.6      1.4      0.2

pandas格式

label1 = pd.DataFrame(label)
label1.columns = ['label']
print(label1.head())

结果:

   label
0      0
1      0
2      0
3      0
4      0

划分训练集和测试集

train_x,test_x,train_y,test_y = train_test_split(data1.values,label1.values,test_size=0.3,random_state = 42)
print("训练集长度:",len(train_x))
print("测试集长度:",len(test_x))

结果:

训练集长度: 105
测试集长度: 45

原生态xgboost的使用方式

# 转换为DMatrix数据格式
test_data = xgb.DMatrix(test_x,label = test_y)

# 设置参数
# multi:softmax是使用softmax后产生的分类结果,而multi:softprob是输出的概率矩阵
xgb_params = {
    'eta':0.3, # 学习率
    'silent':True, # 输出运行信息
    'objective':'multi:softprob', # 使用多分类生成概率矩阵
    'num_class':3, #类别
    'max_depth':3 #深度
}
# 步数
num_round = 20

# 模型训练
model = xgb.train(xgb_params,xgb.DMatrix(train_x,label=train_y),num_round)

# 模型预测
test_pre = model.predict(test_data)

print(test_pre[:5])

# 选择表示最高概率的列
test_pre_1 = np.asarray([np.argmax(row) for row in test_pre])
print("test的预测结果:",test_pre_1)

# 模型评估
print("验证集精准率:",precision_score(test_y,test_pre_1,average='macro'))
print("验证集召回率:",recall_score(test_y,test_pre_1,average='macro'))

结果:

[[0.00650662 0.96226966 0.03122366]
 [0.9706414  0.02533223 0.00402638]
 [0.0033913  0.00692109 0.9896876 ]
 [0.00654367 0.967749   0.0257073 ]
 [0.00615655 0.91049767 0.08334578]]
test的预测结果: [1 0 2 1 1 0 1 2 1 1 2 0 0 0 0 1 2 1 1 2 0 2 0 2 2 2 2 2 0 0 0 0 1 0 0 2 1
 0 0 0 2 1 1 0 0]
验证集精准率: 1.0
验证集召回率: 1.0

sklearn接口形式

from xgboost import XGBClassifier
model = XGBClassifier(
    learning_rate=0.01, # 学习率
    n_estimators=3000, # 步长
    max_depth=4, # 深度
    objective='binary:logistic',
    seed=27
)
model.fit(train_x,train_y)
# 预测
# 输出预测结果
test_pre_2 = model.predict(test_x)
print(test_pre_2)

# 模型评估
print("验证集精准率:",precision_score(test_y,test_pre_2,average='macro'))
print("验证集召回率:",recall_score(test_y,test_pre_2,average='macro'))

结果:

[1 0 2 1 1 0 1 2 1 1 2 0 0 0 0 1 2 1 1 2 0 2 0 2 2 2 2 2 0 0 0 0 1 0 0 2 1
 0 0 0 2 1 1 0 0]
验证集精准率: 1.0
验证集召回率: 1.0

lightgbm

原生态lightgbm

# 转换为DMatrix数据格式
train_data = lgb.Dataset(train_x,train_y)
test_data = lgb.Dataset(test_x,test_y)
# 设置参数
lgb_params = {
   'boosting_type': 'gbdt',  
    'objective': 'multiclass',
    'metric': 'multi_error', 
    'verbose': 1 , # <0 显示致命的, =0 显示错误 (警告), >0 显示信息
     'num_class':3 #lightgbm.basic.LightGBMError: b'Number of classes should be specified and greater than 1 for multiclass training'
    }

# 模型训练
clf = lgb.train(lgb_params,train_data,num_boost_round =10,
                valid_sets = [train_data,test_data], 
                verbose_eval = 10)
# 模型预测
test_pre = clf.predict(test_x, num_iteration=clf.best_iteration)
# print(test_pre)
print(test_pre[:5])

# 选择表示最高概率的列
test_pre_1 = np.asarray([np.argmax(row) for row in test_pre])
print("test的预测结果:",test_pre_1)

# 模型评估
print('验证集精准率:',precision_score(test_y, test_pre_1, average='macro')) 
print('验证集召回率:',recall_score(test_y, test_pre_1, average='macro'))

结果:

[10]    training's multi_error: 0.0666667   valid_1's multi_error: 0
[[0.13683286 0.63500393 0.22816321]
 [0.69436834 0.15467706 0.15095461]
 [0.12934308 0.16125127 0.70940565]
 [0.14172417 0.62195656 0.23631927]
 [0.13683286 0.63500393 0.22816321]]
test的预测结果: [1 0 2 1 1 0 1 2 1 1 2 0 0 0 0 1 2 1 1 2 0 2 0 2 2 2 2 2 0 0 0 0 1 0 0 2 1
 0 0 0 2 1 1 0 0]
验证集精准率: 1.0
验证集召回率: 1.0

Sklearn接口形式使用lightgbm

import lightgbm as lgb
lgb_params = {
    'learning_rate':0.1,
    'max_bin':150,
    'num_leaves':32,    
    'max_depth':11,  
    'objective':'multiclass',
    'n_estimators':300
}
model=lgb.LGBMClassifier(**lgb_params)

model.fit(train_x,train_y)
# 预测
#输出预测结果
test_pre2 = model.predict(test_x)
print(test_pre2)
# 模型评估
print('验证集精准率:',precision_score(test_y, test_pre2, average='macro')) 
print('验证集召回率:',recall_score(test_y, test_pre2, average='macro')) 

结果:

[1 0 2 1 1 0 1 2 1 1 2 0 0 0 0 1 2 1 1 2 0 2 0 2 2 2 2 2 0 0 0 0 1 0 0 2 1
 0 0 0 2 1 1 0 0]
验证集精准率: 1.0
验证集召回率: 1.0

总结:
1、lgb.train中正则化参数为"lambda_l1","lambda_l2",sklearn中则为'reg_alpha','reg_lambda'。
2、多分类时lgb.train除了'objective':'multiclass',还要指定"num_class":5,而sklearn接口只需要指定'objective':'multiclass'。
3、迭代次数在sklearn中是'n_estimators':20,在初始化模型时指定。

你可能感兴趣的:(day02-xgboost和lightgbm简单实现)