• 【工程实践】CatBoost进行多分类


    前言

            工作中有业务涉及到机器学习模型,以CatBoost为例记录学习流程。

    1.CatBoost简介

            CatBoost是俄罗斯的搜索巨头Y andex在2017年开源的机器学习库,和lightgbm、xgboost并成为gbdt三大主流神器库,它是一种基于对称决策树(oblivious trees)算法的参数少、支持类别型变量和高准确性的GBDT框架,主要说解决的痛点是高效合理地处理类别型特征,另外提出了新的方法来处理梯度偏差(Gradient bias)以及预测偏移(Prediction shift)问题,提高算法的准确性和泛化能力。

    2.使用CatBoost进行多分类

    2.1 引入库

    1. import os
    2. import pandas as pd
    3. import numpy as np
    4. import catboost
    5. from catboost.datasets import amazon
    6. from catboost import CatBoostClassifier
    7. from catboost import Pool
    8. from sklearn.model_selection import train_test_split
    9. from tqdm import tqdm
    10. from sklearn.metrics import accuracy_score,f1_score

    2.2 数据处理

    1. def GetNewDataByPandas(data_file):
    2. data = pd.read_csv(data_file)
    3. data = data.dropna() #将所有含空缺值的行都删除
    4. print(data.keys())
    5. y = np.array(data["pkg_label"],dtype=np.int64)
    6. x = np.array(data.drop("pkg_label", axis=1),dtype=np.int64)
    7. return x, y

    2.3 划分数据集

    1. # 划分训练集和测试集
    2. x, y = GetNewDataByPandas(DATASET_FILE)
    3. x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.1)

    2.4 设定cat_features

    1. cat_features = list(range(0,x_test.shape[-1]))
    2. print(cat_features)
    3. #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

    2.5 设定params

    1. params={
    2. 'classes_count':3,
    3. #'auto_class_weights' :'Balanced',
    4. #'task_type': 'GPU',
    5. #'devices': '0:1',
    6. 'loss_function': 'MultiClassOneVsAll', # 损失函数,取值RMSE, Logloss, MAE, CrossEntropy, Quantile, LogLinQuantile, Multiclass, MultiClassOneVsAll, MAPE, Poisson。默认Logloss。
    7. #'custom_loss': 'Logloss', # 训练过程中计算显示的损失函数,取值Logloss、CrossEntropy、Precision、Recall、F、F1、BalancedAccuracy、AUC等等
    8. 'eval_metric': 'AUC', # 用于过度拟合检测和最佳模型选择的指标,取值范围同custom_loss
    9. 'iterations': 10, # 最大迭代次数,默认500. 别名:num_boost_round, n_estimators, num_trees
    10. 'learning_rate': 0.1, # 学习速率,默认0.03 别名:eta
    11. 'random_seed': 123, # 训练的随机种子,别名:random_state
    12. 'l2_leaf_reg': 5, # l2正则项,别名:reg_lambda
    13. 'bootstrap_type': 'Bernoulli', # 确定抽样时的样本权重,取值Bayesian、Bernoulli(伯努利实验)、MVS(仅支持cpu)、Poisson(仅支持gpu)、No(取值为No时,每棵树为简单随机抽样);默认值GPU下为Bayesian、CPU下为MVS
    14. # 'bagging_temperature': 0, # bootstrap_type=Bayesian时使用,取值为1时采样权重服从指数分布;取值为0时所有采样权重均等于1。取值范围[0,inf),值越大、bagging就越激进
    15. 'subsample': 0.6, # 样本采样比率(行采样)
    16. 'sampling_frequency': 'PerTree', # 采样频率,取值PerTree(在构建每棵新树之前采样)、PerTreeLevel(默认值,在子树的每次分裂之前采样);仅支持CPU
    17. 'use_best_model': True, # 让模型使用效果最优的子树棵树/迭代次数,使用验证集的最优效果对应的迭代次数(eval_metric:评估指标,eval_set:验证集数据),布尔类型可取值0,1(取1时要求设置验证集数据)
    18. 'best_model_min_trees': 50, # 最少子树棵树,和use_best_model一起使用
    19. 'depth': 4, # 树深,默认值6
    20. 'grow_policy': 'SymmetricTree', # 子树生长策略,取值SymmetricTree(默认值,对称树)、Depthwise(整层生长,同xgb)、Lossguide(叶子结点生长,同lgb)
    21. 'min_data_in_leaf': 500, # 叶子结点最小样本量
    22. # 'max_leaves': 12, # 最大叶子结点数量
    23. 'one_hot_max_size': 4, # 对唯一值数量
    24. 'rsm': 0.8, # 列采样比率,别名colsample_bylevel 取值(0,1],默认值1
    25. 'nan_mode': 'Max', # 缺失值处理方法,取值Forbidden(不支持缺失值,输入包含缺失时会报错)、Min(处理为该列的最小值,比原最小值更小)、Max(同理)
    26. 'input_borders': None, # 特征数据边界(最大最小边界)、会影响缺失值的处理(nan_mode取值Min、Max时),默认值None、在训练时特征取值的最大最小值即为特征值边界
    27. 'boosting_type': 'Ordered', # 提升类型,取值Ordered(catboost特有的排序提升,在小数据集上效果可能更好,但是运行速度较慢)、Plain(经典提升)
    28. 'max_ctr_complexity': 2, # 分类特征交叉的最高阶数,默认值4
    29. 'logging_level':'Verbose', # 模型训练过程的信息输出等级,取值Silent(不输出信息)、Verbose(默认值,输出评估指标、已训练时间、剩余时间等)、Info(输出额外信息、树的棵树)、Debug(debug信息)
    30. 'metric_period': 1, # 计算目标值、评估指标的频率,默认值1、即每次迭代都输出目标值、评估指标
    31. 'early_stopping_rounds': 20,
    32. 'border_count': 254, # 数值型特征的分箱数,别名max_bin,取值范围[1,65535]、默认值254(CPU下), # 设置提前停止训练,在得到最佳的评估结果后、再迭代n(参数值为n)次停止训练,默认值不启用
    33. 'feature_border_type': 'GreedyLogSum', # 数值型特征的分箱方法,取值Median、Uniform、UniformAndQuantiles、MaxLogSum、MinEntropy、GreedyLogSum(默认值)
    34. }

    2.6 定义模型

    model = CatBoostClassifier(**params)

    2.7 训练

    1. model.fit(x_train, y_train,eval_set=[(x_test,y_test)],cat_features=cat_features,plot=True,)
    2. #model.fit(train_pool,eval_set=validation_pool,cat_features=cat_features,plot=True)

    2.8 预测

    1. pred_y = model.predict(x_test)
    2. y_pred=[list(x).index(max(x)) for x in pred_y]
    3. print("accuracy_score",accuracy_score(y_test,y_pred))
    4. print('f1_score',f1_score(y_test, np.array(y_pred), average='weighted'))

     2.9 画图

    1. import matplotlib.pyplot as plt
    2. fea_ = model.feature_importances_
    3. fea_name = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    4. plt.figure(figsize=(10, 10))
    5. plt.barh(fea_name,fea_,height =0.5)

            注意: 可以将fea_name = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']换成自己数据集中的特征名。

     2.10 特征排序显示

    1. import matplotlib.pyplot as plt
    2. import numpy as np
    3. fea_ = model.feature_importances_
    4. print(fea_)
    5. print(type(fea_))
    6. fea_name = model.feature_names_
    7. print(fea_name)
    8. idx_name = {}
    9. with open('./feature_name.txt', 'r') as rf:
    10. for line in rf:
    11. ln = line.strip().split(' ')
    12. idx_name[ln[0]] = ln[1]
    13. print(idx_name)
    14. # fea_arr = np.array(fea_)
    15. idx = np.argsort(fea_)
    16. print('---idx: ', idx)
    17. print(idx.shape)
    18. final_feat = []
    19. final_name = []
    20. for i in idx:
    21. print('--{0} {1}'.format(i, fea_[i]))
    22. final_feat.append(fea_[i])
    23. final_name.append(idx_name[str(i)])
    24. print(final_feat)
    25. print(final_name)
    26. fig, ax = plt.subplots(figsize=(11, 11))
    27. # Horizontal Bar Plot
    28. ax.barh(final_name, final_feat, height=0.5)
    29. # Remove axes splines
    30. for s in ['top', 'bottom', 'left', 'right']:
    31. ax.spines[s].set_visible(False)
    32. # Add x, y gridlines
    33. ax.grid(b=True, color='grey',
    34. linestyle='-.', linewidth=0.5,
    35. alpha=0.2)
    36. # Add annotation to bars
    37. for i in ax.patches:
    38. plt.text(i.get_width()+0.2, i.get_y()+0.2,
    39. str(round((i.get_width()), 2)),
    40. fontsize=9, fontweight='bold',
    41. color='black')
    42. ax.set_title('feature importance' )
    43. plt.show()

             注意:文件格式

    Reference:

            1.catboost完全指南 - 知乎

  • 相关阅读:
    【AI 测试】分词器
    高性能计算环境下的深度学习异构集群建设与优化实践
    linux基础知识学习
    第五十一天学习记录:C语言进阶:枚举和联合(共用体)
    geecg-uniapp 源码下载运行 修改端口号 修改tabBar 修改展示数据
    Nacos配置管理
    虚拟DOM和真实DOM的区别和联系
    spring、tomcat是如何配合完成websocket
    【HMS Core】【SDK集成】Android Studio中Gradle Version7.1+以上版本如何集成agcp插件?
    Python实现基于用户的协同过滤推荐算法构建电影推荐系统
  • 原文地址:https://blog.csdn.net/weixin_44750512/article/details/132499701