1.categorical_feature(类别特征)使用
lightGBM比XGBoost的1个改进之处在于对类别特征的处理, 不再需要将类别特征转为one-hot形式, 具体可参考这里.

在使用python API时(参考官方文档)
1.1可以使用pd.DataFrame存放特征X, 每一列表示1个特征, 将类别特征设置为X[cat_cols].astype('category'). 这样模型在fit时会自动识别类别特征.
1.2在模型的fit方法中传入参数categorical_feature, 指明哪些列是类别特征.
1.3类别特征的值必须是从0开始的连续整数, 比如0,1,2,..., 不能是负数.

下面是官方文档对fit方法中categorical_feature参数的说明:
categorical_feature (list of strings or int, or 'auto', optional (default='auto')) – Categorical features. If list of int, interpreted as indices. If list of strings, interpreted as feature names (need to specify feature_name as well). If ‘auto’ and data is pandas DataFrame, pandas unordered categorical columns are used. All values in categorical features should be less than int32 max value (2147483647). Large values could be memory consuming. Consider using consecutive integers starting from zero. All negative values in categorical features will be treated as missing values. The output cannot be monotonically constrained with respect to a categorical feature.

之前在使用sklearn的GridSearchCV时就发现一个问题, x_train和x_test中的cat_cols列已经设置好了是category类型, 但是concat之后类型又变成了int:

x_search = pd.concat([x_train, x_test], 0)
y_search = np.concatenate([train_y, test_y],0)
gsearch.fit(x_search, y_search) # 搜索最优参数, gsearch是sklearn的GridSearchCV实例

所以, 需要在concat之后重新设置一下类型

x_search = pd.concat([x_train, x_test], 0)
x_search[cat_cols] = x_search[cat_cols].astype('category')
y_search = np.concatenate([train_y, test_y],0)
gsearch.fit(x_search, y_search) # 搜索最优参数

2.init_score使用
init_score是estimator的初始得分, 在regression任务中用init_score可以帮助模型更快收敛.
使用时只需要在fit方法中设置init_score参数即可, 最后predict时, 需要加上设置的这个init_score


model.fit(
#        pd.concat([x_train,x_val],0),
#        np.concatenate([train_y, val_y],0),x_train, train_y,init_score=y_train_base_avg1,eval_metric=['mape'], eval_set=(x_val, val_y),early_stopping_rounds=20,eval_init_score=[y_val_base_avg1],verbose=True)
...
y_train_pre = model.predict(x_train) + y_train_base_avg1 # 加上init_score

上面是regression的情况, 那么对于classification呢?以及配合GridSearchCV时怎么用(因为predict时必须手动加上init_score)?
参考这里和这里(init_score is the raw score, before any transformation.).

▲▲▲使用ParameterGrid代替GridSearchCV, 中间可加入更多的自定义操作
1.可以使用early_stopping, 而GridSearchCV做不到
2.可以支持lightGBM的init_score

...
from sklearn.model_selection import ParameterGrid
...
parameters = {'objective': ['regression', 'regression_l1'],'max_depth': [2,3,4,5],'num_leaves': [20,25,30,35],'n_estimators': [20,25,30,35,40,50],'min_child_samples': [15,20,25,30],
#        'subsample_freq': [0,2,5],
#        'subsample': [0.7,0.8,0.9,1],
#        'colsample_bytree': [0.8,0.9,1]}
...default_params = model.get_params() # 得到前面预定义的参数
best_score = np.inf
best_params = None
best_idx = 0
param_grid = list(ParameterGrid(parameters)) # 生成所有参数组合的list
for idx,param_ in enumerate(param_grid):param = default_params.copy()param.update(param_)model = LGBMRegressor(**param)model.fit(x_train, train_y,init_score=y_train_base_avg1,eval_metric=['mape'], eval_set=(x_val, val_y),early_stopping_rounds=20,eval_init_score=[y_val_base_avg1],verbose=False)score_ = model.best_score_['valid_0']['mape'] # 当前模型在val set上的最好得分print('for %d/%d, score: %.6f, best idx in %d/%d'%(idx+1, len(param_grid), score_, best_idx, len(param_grid)))if score_<best_score:best_params = parambest_score = score_best_idx = idx+1print('find best score: {}, \nbest params: {}'.format(best_score, best_params))
print('\nbest score: {}, \nbest params: {}\n'.format(best_score, best_params))
#raise ValueErrormodel = LGBMRegressor(**best_params)
model.fit(x_train, train_y,init_score=y_train_base_avg1,eval_metric=['mape'], eval_set=(x_val, val_y),early_stopping_rounds=20,eval_init_score=[y_val_base_avg1],verbose=True)

lightGBM使用相关推荐

  1. 多分类 数据不平衡的处理 lightgbm

    前言 数据不平衡问题在机器学习分类问题中很常见,尤其是涉及到"异常检测"类型的分类.因为异常一般指的相对不常见的现象,因此发生的机率必然要小很多.因此正常类的样本量会远远高于异常类 ...

  2. lightgbm 决策树 可视化 graphviz

    决策树模型,XGBoost,LightGBM和CatBoost模型可视化 安装 graphviz 参考文档 http://graphviz.readthedocs.io/en/stable/manua ...

  3. lightgbm保存模型参数

    20210205 params = {'task': 'train', # 执行的任务类型'boosting_type': 'gbrt', # 基学习器'objective': 'lambdarank ...

  4. xgboost lightgbm catboost 多分类 多标签

    xgboost 与 lightgbm 官方均支持多分类任务,但不直接支持多标签分类任务,实现多标签任务的方法之一是结合sklearn 提供的 multiclass 子类,如OneVsRestClass ...

  5. 梯度提升决策树(GBDT)与XGBoost、LightGBM

    20211224 [机器学习算法总结]XGBoost_yyy430的博客-CSDN博客_xgboost xgboost参数 默认:auto.XGBoost中使用的树构造算法.可选项:auto,exac ...

  6. 30分钟搞定数据竞赛刷分夺冠神器LightGBM!

    作者 | 梁云1991 来源 | Python与算法之美(ID:Python_Ai_Road) [导读]LightGBM可以看成是XGBoost的升级加强版本,2017年经微软推出后,便成为各种数据竞 ...

  7. 大战三回合:XGBoost、LightGBM和Catboost一决高低 | 程序员硬核算法评测

    作者 | LAVANYA 译者 | 陆离 责编 | Jane 出品 | AI科技大本营(ID: rgznai100) [导读]XGBoost.LightGBM 和 Catboost 是三个基于 GBD ...

  8. 基于LightGBM算法实现数据挖掘!

    ↑↑↑关注后"星标"Datawhale 每日干货 & 每月组队学习,不错过 Datawhale干货 作者:黄雨龙,中国科学技术大学 对于回归问题,Datawhale已经梳理 ...

  9. Kaggle神器LightGBM最全解读!

    ↑↑↑关注后"星标"Datawhale 每日干货 & 每月组队学习,不错过 Datawhale干货 来源:Microstrong,编辑:AI有道 本文主要内容概览: 1. ...

  10. 比赛杀器LightGBM常用操作总结!

    Datawhale干货 作者:阿水,北京航空航天大学,Datawhale成员 LightGBM是基于XGBoost的一款可以快速并行的树模型框架,内部集成了多种集成学习思路,在代码实现上对XGBoos ...

最新文章

  1. ssd测试软件cy,目标检测实践_tensorflow版SSD模型测试
  2. 数据库的dml、ddl和dcl的概念
  3. glassfish_重写到边缘–充分利用它! 在GlassFish上!
  4. mysql 实施索引_MySQL 索引实现
  5. iphone以旧换新活动_一年当中什么时候买手机最便宜?|手机|优惠券|购物节|苹果手机|iphone...
  6. 感觉养老金越涨差距越大,有人提议高于5000的不再上涨,合理吗?
  7. GDI+ Graphics类
  8. [转载] python __import__ 搜索路径详解
  9. Android中的短信收不到问题
  10. 会场安排问题(贪心算法) Comparator类排序的学习
  11. 蓝桥杯 算法训练 数的潜能 正整数分解使得乘积最大问题
  12. java post 注册_使用post request python注册帐户
  13. 科来网络分析系统(实用)
  14. 机器人方队解说词_创想机器人博物馆介绍配音解说词
  15. 交错、反交错与IVTC —— 从入门到放弃
  16. 智能语音对话处理过程
  17. 成都拓嘉启远:拼多多评论置顶该怎样去弄
  18. 亲自操作,有用的win10遇到“已禁用输入法”无法启动中文输入法的问题-提示已禁用输入法解决方案
  19. 学 C 语言,最经典的书有这样几本
  20. vue 控制某个元素的显示与隐藏之v-if属性

热门文章

  1. 重装系统后计算机无法启动,电脑小白一键重装系统后无法开机
  2. APP运行时Crash自动修复系统
  3. 广东医科大学计算机网络,广东海洋大学计算机网络历年考题(直接阅读版6套可编辑)课件.doc...
  4. This beta version of Typora is expired, please download and install a newer version.
  5. json和jsonb类型——PostgreSQL
  6. iPhone手机使用:手机上面的App Store突然变成英文(iTunes也是英文),然后把英文还原成中文的方法
  7. 用GitHub Actions自动部署Hexo
  8. JAVA 编写一个员工类,成员变量和成员方法自拟,编写一个测试类
  9. css里dotted,CSS中dashed和dotted的区别有哪些
  10. 系统辨识(六):最小二乘法的修正算法