目录
  • 一、introduction
    • 1 lightgbm的优点
    • 2 lightgbm的缺点
  • 二、实现过程
    • 1 数据集介绍
    • 2 coding
  • 三、keys
    • lightgbm的重要参数
      • 基本参数调整
      • 针对训练速度的参数调整
      • 针对准确率的参数调整
      • 针对过拟合的参数调整

一、introduction

lightgbm是扩展机器学习系统。是一款基于gbdt(梯度提升决策树)算法的分布梯度提升框架。其设计思路主要集中在减少数据对内存与计算性能的使用上,以及减少多机器并行计算时的通讯代价

1 lightgbm的优点

  • 简单易用。提供了主流的python\c++\r语言接口,用户可以轻松使用lightgbm建模并获得相当不错的效果。
  • 高效可扩展。在处理大规模数据集时高效迅速、高准确度,对内存等硬件资源要求不高。
  • 鲁棒性强。相较于深度学习模型不需要精细调参便能取得近似的效果。
  • lightgbm直接支持缺失值与类别特征,无需对数据额外进行特殊处理

2 lightgbm的缺点

  • 相对于深度学习模型无法对时空位置建模,不能很好地捕获图像、语音、文本等高维数据。
  • 在拥有海量训练数据,并能找到合适的深度学习模型时,深度学习的精度可以遥遥领先lightgbm。

二、实现过程

1 数据集介绍

英雄联盟数据集 提取码:1234

本数据用于lightgbm分类实战。该数据集共有9881场英雄联盟韩服钻石段位以上的排位赛数据,数据提供了在十分钟时的游戏状态,包括击杀数,金币数量,经验值,等级等信息。

2 coding

#导入基本库
import numpy as np 
import pandas as pd

## 绘图函数库
import matplotlib.pyplot as plt
import seaborn as sns
#%% 数据读入:利用pandas自带的read_csv函数读取并转化为dataframe格式
df = pd.read_csv('d:\python\ml\data\high_diamond_ranked_10min.csv')
y = df.bluewins
#%%查看样本数据
#print(y.value_counts())
#标注特征列
drop_cols=['gameid','bluewins']
x=df.drop(drop_cols,axis=1)
#对数字特征进行统计描述
x_des=x.describe()

#%%去除冗余数据,因为红蓝为竞争关系,只需知道一方的情况,对方相反因此去除红方的数据信息
drop_cols = ['redfirstblood','redkills','reddeaths'
             ,'redgolddiff','redexperiencediff', 'bluecspermin',
            'bluegoldpermin','redcspermin','redgoldpermin']
x.drop(drop_cols, axis=1, inplace=true)
#%%可视化描述。为了有一个好的呈现方式,分两张小提琴图展示前九个特征和中间九个特征,后面的相同不再赘述
data = x
data_std = (data - data.mean()) / data.std()
data = pd.concat([y, data_std.iloc[:, 0:9]], axis=1)#将标签与前九列拼接此时的到的data是(9879*10)的metric
data = pd.melt(data, id_vars='bluewins', var_name='features', value_name='values')#将上面的数据melt成(88911*3)的metric

fig, ax = plt.subplots(1,2,figsize=(15,8))

# 绘制小提琴图
sns.violinplot(x='features', y='values', hue='bluewins', data=data, split=true,
               inner='quart', ax=ax[0], palette='blues')
fig.autofmt_xdate(rotation=45)#改变x轴坐标的现实方法,可以斜着表示(倾斜45度),不用平着挤成一堆

data = x
data_std = (data - data.mean()) / data.std()
data = pd.concat([y, data_std.iloc[:, 9:18]], axis=1)
data = pd.melt(data, id_vars='bluewins', var_name='features', value_name='values')

# 绘制小提琴图
sns.violinplot(x='features', y='values', hue='bluewins', 
               data=data, split=true, inner='quart', ax=ax[1], palette='blues')
fig.autofmt_xdate(rotation=45)
plt.show()

#%%画出各个特征之间的相关性热力图
fig,ax=plt.subplots(figsize=(15,18))
sns.heatmap(round(x.corr(),2),cmap='blues',annot=true)
fig.autofmt_xdate(rotation=45)
plt.show()

#%%根据上述特征图,剔除相关性较强的冗余特征(redavglevel,blueavglevel)
# 去除冗余特征
drop_cols = ['redavglevel','blueavglevel']
x.drop(drop_cols, axis=1, inplace=true)

sns.set(style='whitegrid', palette='muted')

# 构造两个新特征
x['wardsplaceddiff'] = x['bluewardsplaced'] - x['redwardsplaced']
x['wardsdestroyeddiff'] = x['bluewardsdestroyed'] - x['redwardsdestroyed']

data = x[['bluewardsplaced','bluewardsdestroyed','wardsplaceddiff','wardsdestroyeddiff']].sample(1000)
data_std = (data - data.mean()) / data.std()
data = pd.concat([y, data_std], axis=1)
data = pd.melt(data, id_vars='bluewins', var_name='features', value_name='values')

plt.figure(figsize=(15,8))
sns.swarmplot(x='features', y='values', hue='bluewins', data=data)
plt.show()

#%%由上图插眼数量的离散图,可以发现插眼数量与游戏胜负之间的显著规律,游戏前十分钟插眼与否对最终的胜负影响不大,故将这些特征去除
## 去除和眼位相关的特征
drop_cols = ['bluewardsplaced','bluewardsdestroyed','wardsplaceddiff',
            'wardsdestroyeddiff','redwardsplaced','redwardsdestroyed']
x.drop(drop_cols, axis=1, inplace=true)
#%%击杀、死亡与助攻数的数据分布差别不大,但是击杀减去死亡、助攻减去死亡的分布与缘分不差别较大,构造两个新的特征
x['killsdiff'] = x['bluekills'] - x['bluedeaths']
x['assistsdiff'] = x['blueassists'] - x['redassists']
x[['bluekills','bluedeaths','blueassists','killsdiff','assistsdiff','redassists']].hist(figsize=(15,8), bins=20)
plt.show()

#%%
data = x[['bluekills','bluedeaths','blueassists','killsdiff','assistsdiff','redassists']].sample(1000)
data_std = (data - data.mean()) / data.std()
data = pd.concat([y, data_std], axis=1)
data = pd.melt(data, id_vars='bluewins', var_name='features', value_name='values')

plt.figure(figsize=(10,6))
sns.swarmplot(x='features', y='values', hue='bluewins', data=data)
plt.xticks(rotation=45)
plt.show()

#%%
data = pd.concat([y, x], axis=1).sample(500)
sns.pairplot(data, vars=['bluekills','bluedeaths','blueassists','killsdiff','assistsdiff','redassists'], 
             hue='bluewins')
plt.show()

#%%一些特征两两组合后对于数据的划分有提升
x['dragonsdiff'] = x['bluedragons'] - x['reddragons']#拿到龙
x['heraldsdiff'] = x['blueheralds'] - x['redheralds']#拿到峡谷先锋
x['elitediff'] = x['blueelitemonsters'] - x['redelitemonsters']#击杀大型野怪
data = pd.concat([y, x], axis=1)
elitegroup = data.groupby(['elitediff'])['bluewins'].mean()
dragongroup = data.groupby(['dragonsdiff'])['bluewins'].mean()
heraldgroup = data.groupby(['heraldsdiff'])['bluewins'].mean()
fig, ax = plt.subplots(1,3, figsize=(15,4))

elitegroup.plot(kind='bar', ax=ax[0])
dragongroup.plot(kind='bar', ax=ax[1])
heraldgroup.plot(kind='bar', ax=ax[2])

print(elitegroup)
print(dragongroup)
print(heraldgroup)

plt.show()

#%%推塔数量与游戏胜负
x['towerdiff'] = x['bluetowersdestroyed'] - x['redtowersdestroyed']
data = pd.concat([y, x], axis=1)
towergroup = data.groupby(['towerdiff'])['bluewins']
print(towergroup.count())
print(towergroup.mean())

fig, ax = plt.subplots(1,2,figsize=(15,5))

towergroup.mean().plot(kind='line', ax=ax[0])
ax[0].set_title('proportion of blue wins')
ax[0].set_ylabel('proportion')

towergroup.count().plot(kind='line', ax=ax[1])
ax[1].set_title('count of towers destroyed')
ax[1].set_ylabel('count')

#%%利用lightgbm进行训练和预测
## 为了正确评估模型性能,将数据划分为训练集和测试集,并在训练集上训练模型,在测试集上验证模型性能。
from sklearn.model_selection import train_test_split
## 选择其类别为0和1的样本 (不包括类别为2的样本)
data_target_part = y
data_features_part = x
## 测试集大小为20%, 80%/20%分
x_train, x_test, y_train, y_test = train_test_split(data_features_part, data_target_part, test_size = 0.2, random_state = 2020)
#%%## 导入lightgbm模型
from lightgbm.sklearn import lgbmclassifier
## 定义 lightgbm 模型 
clf = lgbmclassifier()
# 在训练集上训练lightgbm模型
clf.fit(x_train, y_train)

#%%在训练集和测试集上分别利用训练好的模型进行预测
train_predict = clf.predict(x_train)
test_predict = clf.predict(x_test)
from sklearn import metrics

## 利用accuracy(准确度)【预测正确的样本数目占总预测样本数目的比例】评估模型效果
print('the accuracy of the lightgbm is:',metrics.accuracy_score(y_train,train_predict))
print('the accuracy of the lightgbm is:',metrics.accuracy_score(y_test,test_predict))

## 查看混淆矩阵 (预测值和真实值的各类情况统计矩阵)
confusion_matrix_result = metrics.confusion_matrix(test_predict,y_test)
print('the confusion matrix result:\n',confusion_matrix_result)

# 利用热力图对于结果进行可视化
plt.figure(figsize=(8, 6))
sns.heatmap(confusion_matrix_result, annot=true, cmap='blues')
plt.xlabel('predicted labels')
plt.ylabel('true labels')
plt.show()

#%%利用lightgbm进行特征选择,同样可以用属性feature_importances_查看特征的重要度
sns.barplot(y=data_features_part.columns, x=clf.feature_importances_)

#%%除feature_importances_外,还可以使用lightgbm中的其他属性进行评估(gain,split)
from sklearn.metrics import accuracy_score
from lightgbm import plot_importance

def estimate(model,data):
    ax1=plot_importance(model,importance_type="gain")
    ax1.set_title('gain')
    ax2=plot_importance(model, importance_type="split")
    ax2.set_title('split')
    plt.show()
def classes(data,label,test):
    model=lgbmclassifier()
    model.fit(data,label)
    ans=model.predict(test)
    estimate(model, data)
    return ans
 
ans=classes(x_train,y_train,x_test)
pre=accuracy_score(y_test, ans)
print('acc=',accuracy_score(y_test,ans))

通过调整参数获得更好的效果: lightgbm中重要的参数

  • learning_rate: 有时也叫作eta,系统默认值为0.3。每一步迭代的步长,很重要。太大了运行准确率不高,太小了运行速度慢。
  • num_leaves:系统默认为32。这个参数控制每棵树中最大叶子节点数量。
  • feature_fraction:系统默认值为1。我们一般设置成0.8左右。用来控制每棵随机采样的列数的占比(每一列是一个特征)。
  • max_depth: 系统默认值为6,我们常用3-10之间的数字。这个值为树的最大深度。这个值是用来控制过拟合的。max_depth越大,模型学习的更加具体。
#%%调整参数,获得更好的效果
## 从sklearn库中导入网格调参函数
from sklearn.model_selection import gridsearchcv

## 定义参数取值范围
learning_rate = [0.1, 0.3, 0.6]
feature_fraction = [0.5, 0.8, 1]
num_leaves = [16, 32, 64]
max_depth = [-1,3,5,8]

parameters = { 'learning_rate': learning_rate,
              'feature_fraction':feature_fraction,
              'num_leaves': num_leaves,
              'max_depth': max_depth}
model = lgbmclassifier(n_estimators = 50)

## 进行网格搜索
clf = gridsearchcv(model, parameters, cv=3, scoring='accuracy',verbose=3, n_jobs=-1)
clf = clf.fit(x_train, y_train)
#%%查看最好的参数值分别是多少
print(clf.best_params_)

#%%查看最好的参数值分别是多少
print(clf.best_params_)
#%% 在训练集和测试集上分布利用最好的模型参数进行预测
## 定义带参数的 lightgbm模型 
clf = lgbmclassifier(feature_fraction = 1,
                    learning_rate = 0.1,
                    max_depth= 3,
                    num_leaves = 16)
# 在训练集上训练lightgbm模型
clf.fit(x_train, y_train)

train_predict = clf.predict(x_train)
test_predict = clf.predict(x_test)

## 利用accuracy(准确度)【预测正确的样本数目占总预测样本数目的比例】评估模型效果
print('the accuracy of the lightgbm is:',metrics.accuracy_score(y_train,train_predict))
print('the accuracy of the lightgbm is:',metrics.accuracy_score(y_test,test_predict))

## 查看混淆矩阵 (预测值和真实值的各类情况统计矩阵)
confusion_matrix_result = metrics.confusion_matrix(test_predict,y_test)
print('the confusion matrix result:\n',confusion_matrix_result)

# 利用热力图对于结果进行可视化
plt.figure(figsize=(8, 6))
sns.heatmap(confusion_matrix_result, annot=true, cmap='blues')
plt.xlabel('predicted labels')
plt.ylabel('true labels')
plt.show()

三、keys

lightgbm的重要参数

基本参数调整

  • num_leaves参数 这是控制树模型复杂度的主要参数,一般的我们会使num_leaves小于(2的max_depth次方),以防止过拟合。由于lightgbm是leaf-wise建树与xgboost的depth-wise建树方法不同,num_leaves比depth有更大的作用。
  • min_data_in_leaf 这是处理过拟合问题中一个非常重要的参数. 它的值取决于训练数据的样本个树和 num_leaves参数. 将其设置的较大可以避免生成一个过深的树, 但有可能导致欠拟合. 实际应用中, 对于大数据集, 设置其为几百或几千就足够了.
  • max_depth 树的深度,depth 的概念在 leaf-wise 树中并没有多大作用, 因为并不存在一个从 leaves 到 depth 的合理映射

针对训练速度的参数调整

  • 通过设置 bagging_fraction 和 bagging_freq 参数来使用 bagging 方法。
  • 通过设置 feature_fraction 参数来使用特征的子抽样。
  • 选择较小的 max_bin 参数。使用 save_binary 在未来的学习过程对数据加载进行加速。

针对准确率的参数调整

  • 使用较大的 max_bin (学习速度可能变慢)
  • 使用较小的 learning_rate 和较大的 num_iterations
  • 使用较大的 num_leaves (可能导致过拟合)
  • 使用更大的训练数据
  • 尝试 dart 模式

针对过拟合的参数调整

  • 使用较小的 max_bin
  • 使用较小的 num_leaves
  • 使用 min_data_in_leaf 和 min_sum_hessian_in_leaf
  • 通过设置 bagging_fraction 和 bagging_freq 来使用 bagging
  • 通过设置 feature_fraction 来使用特征子抽样
  • 使用更大的训练数据
  • 使用 lambda_l1, lambda_l2 和 min_gain_to_split 来使用正则
  • 尝试 max_depth 来避免生成过深的树

最近越发觉得良好的coding habits的重要性!debug才是yyds,从刚学c语言的时候就被老师教育过,当时尝到了debug的甜头,到后来大部分写完即使没有bug的代码还是会debug一遍,现在依然是,希望大家也都养成debug的习惯,当然还有就是写注释,annotation是自己当时的思想,不写后期自己返回来看很大程度时间久了都不知道每个步骤的用意。 886~~~

到此这篇关于python机器学习应用之基于lightgbm的分类预测篇解读的文章就介绍到这了,更多相关python lightgbm分类预测内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!