lightgbm分类问题的实例

lightgbm解决分类问题实例

详细说明见注释

import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split, GridSearchCV
import lightgbm
from sklearn.metrics import *
import matplotlib.pyplot as plt
import re
import warnings

warnings.filterwarnings('ignore')
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

df = pd.read_csv('feature6_label7.csv')


def etl(df):
    # age中没匹配到为-1,此处将异常值定义为-2
    df.loc[(df['age'] < -1) | (df['age'] == 0) | (df['age'] > 85), 'age'] = -2
    df.loc[df['rwsc'] > 6000, 'rwsc'] = 6000
    # df['rwsc'] = df['rwsc'].apply(lambda x: round(x/10))

    # echannel_nums接触上线渠道次数,严重的长尾分布,因为尾巴甩的太长,这里选择log10进行前缩,可以尽量压缩后面的尾巴,让数据尽量集中
    # -1 -> 0.1,0 -> 0.316,取log10后分别变成了-1,-0.5
    df['echannel_nums'].fillna(-1, inplace=True)
    df['echannel_nums'].replace(-1, 0.1, inplace=True)
    df['echannel_nums'].replace(0, 0.316, inplace=True)
    df['echannel_nums'] = np.log10(df['echannel_nums'])
    df['echannel_nums'] = round(df['echannel_nums'], 1)
    df.loc[df['echannel_nums'] > 1.7, 'echannel_nums'] = 1.7

    # channel_nums线下渠道,随着次数增加疯狂下降,此处直接取30卡死
    df['channel_nums'].fillna(-1, inplace=True)
    df.loc[df['channel_nums'] > 30, 'channel_nums'] = 30

    # 后续考虑删除
    df['bxl_lltsb'] = df['bxl_lltsb'].fillna(-1)

    # 处理10086呼叫次数
    df['kf_call_cnts'] = df['kf_call_cnts'].fillna(0)
    df['kf_call_cnts'] = df['kf_call_cnts'].apply(lambda x: 5 if x >= 5 else x)
    # 处理呼叫时长
    df['kf_call_duration'].fillna(0.316, inplace=True)
    df['kf_call_duration'] = np.log10(df['kf_call_duration'])
    df['kf_call_duration'] = round(df['kf_call_duration'], 2)
    df.loc[df['kf_call_duration'] > 2.9, 'kf_call_duration'] = 2.9

    df['volte_voice_total_count'].fillna(-1, inplace=True)
    df['video_req_count'].fillna(-1, inplace=True)
    # video_total_time视频总通话时长有很多负数,此处将负数强打为-1,即缺失值
    df['video_total_time'].fillna(-1, inplace=True)
    df.loc[df['video_total_time'] < 0, 'video_total_time'] = -1

    df['game_resp_total_count'].fillna(-1, inplace=True)
    df['im_login_total_time'].fillna(-1, inplace=True)

    # 'im_flow', 'web_flow', 'video_flow', 'game_flow' 这四种业务流量的缺失值数量相同,spearman相关系数比较高
    df['im_flow'].fillna(-1, inplace=True)
    df['web_flow'].fillna(-1, inplace=True)
    df['video_flow'].fillna(-1, inplace=True)
    df['game_flow'].fillna(-1, inplace=True)

    df['exceeding_traffic_cost'].fillna(-1, inplace=True)
    df['max_kf_call_cnts'].fillna(-1, inplace=True)
    df['pk_saturation_traffic'].fillna(-1, inplace=True)
    df['stop_num'].fillna(-1, inplace=True)

    df['chongzhi_cnts'].fillna(-1, inplace=True)
    df['chongzhi_fee'].fillna(-1, inplace=True)
    df.loc[df['chongzhi_fee'] < 0, 'chongzhi_fee'] = -1

    return df


# %%time
# 特征处理
df = etl(df)

df.drop([
    'traffic_add_num',
    'traffic_add_amount', 'exceeding_traffic_cost', 'max_kf_call_cnts',
    'pk_saturation_call', 'pk_saturation_traffic', 'balance', 'stop_num',
    'chongzhi_cnts', 'chongzhi_fee'
], axis=1, inplace=True)
# 特征
X = df.drop(['help', 'msisdn'], axis=1)
# 标签
y = df['help']
# 拆分训练集和测试集
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=6666)

# lgbm = lightgbm.LGBMClassifier(random=666, n_estimators=50)
lgbm = lightgbm.LGBMClassifier(random_state=666)
# 训练模型
lgbm.fit(x_train, y_train)
# 预测
y_pro_test = lgbm.predict_proba(x_test)
# print(y_pro_test)
# y_pro_test
fpr, tpr, thresholds = roc_curve(y_test, y_pro_test[:, -1])
print('auc: ', (roc_auc_score(y_test, y_pro_test[:, -1])))
# 绘制auc曲线
# 绘制面积
plt.stackplot(fpr, tpr, color='steelblue', alpha=0.5, edgecolor='black')
# 绘制曲线
plt.plot(fpr, tpr, color='black', lw=1)
# 添加对角线
plt.plot([0, 1], [0, 1], color='red', linestyle='--')
# 添加文本信息
plt.text(0.5, 0.3, 'ROC curve (area=%0.2f)' % roc_auc_score(y_test, y_pro_test[:, -1]))
# 添加x轴和y轴
plt.xlabel('1-Specificity')
plt.ylabel('Sensitivity')
# 显示图形
plt.show()
# 计算ks,至少要大于0.2
print("ks: ", max(tpr - fpr))
print('------------------------------------')
print(classification_report(y_test, np.where(y_pro_test[:, -1] > 0.5, 1, 0)))
print('-----------------混淆矩阵-----------------------')
print(confusion_matrix(y_test, np.where(y_pro_test[:, -1] > 0.5, 1, 0)))

# 查看pr曲线
precisions, recalls, thresholds = precision_recall_curve(y_test, y_pro_test[:, -1])
plt.plot(thresholds, precisions[:-1], "b--", label="Precision")
plt.plot(thresholds, recalls[:-1], "g-", label="Recall")
plt.xlabel("Threshold")
plt.legend(loc="upper left")
plt.ylim([0, 1])
plt.grid(True, linestyle="--", alpha=0.5)

fpr, tpr, thresholds = roc_curve(y_test, y_pro_test[:, -1])


def plot_roc_curve(fpr, tpr, label=None):
    plt.plot(fpr, tpr, linewidth=2, label=label)
    plt.plot([0, 1], [0, 1], 'k--')
    plt.axis([0, 1, 0, 1])
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')


# plot_roc_curve(fpr, tpr)
plt.show()

import joblib
# 存储模型
joblib.dump(lgbm, 'lgbm_fee_F2_F3_v2.pkl')

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值