1. 安装
建议用conda安装。
首先安装miniconda,在官网下载对应的版本。
然后将系统的python和pip定位到miniconda文件夹下。
然后用conda安装lightgbm,在Mac m2芯片上测试可行。(用pip直接安装通不过编译)。
2. 简单case
将lightgbm的github上的代码clone下来。
首先来看simple_example.py。它的对应数据如下:
第一列是标签,后面是数据。
所以用下面的代码划分测试和训练集:
y_train = df_train[0]
y_test = df_test[0]
X_train = df_train.drop(0, axis=1)
X_test = df_test.drop(0, axis=1)
# create dataset for lightgbm
lgb_train = lgb.Dataset(X_train, y_train)
lgb_eval = lgb.Dataset(X_test, y_test, reference=lgb_train)
简单的参数配置如下:
# specify your configurations as a dict
params = {
'boosting_type': 'gbdt',
'objective': 'regression',
'verbose': 0
}
print('Starting training...')
# train
gbm = lgb.train(params,
lgb_train,
valid_sets=lgb_eval)
print('Starting predicting...')
# predict
y_pred = gbm.predict(X_test)
# eval
rmse_test = mean_squared_error(y_test, y_pred) ** 0.5
print(f'The RMSE of prediction is: {rmse_test}')
3. 高级教程
参考advanced_example.py,知识点为:
二分类的参数选择:
params = {
'boosting_type': 'gbdt',
'objective': 'binary',
'metric': 'binary_logloss'
}
这里涉及到了objective的种类,包括 regression, regression_l1, huber, fair, poisson, quantile, mape, gamma, tweedie, binary, multiclass, multiclassova, cross_entropy, cross_entropy_lambda, lambdarank, rank_xendcg