机器学习----lightGBM安装

本文介绍如何在Windows 7系统上通过pip及GitHub源安装LightGBM,并提供了一个使用LightGBM进行回归预测的案例。案例中包含了数据加载、模型训练、预测评估等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

win7+python2.7

1. install from pip

(1)首先需要安装 VC runtime(可编译C++的小程序):
http://download.youkuaiyun.com/download/zhouwenyuan1015/9944566
VC_redist.x64.exe
(2)安装
pip install lightgbm

2. install from GitHub

 (1) 安装CMake :https://cmake.org/download/ 
      (cmake-3.6.1-win64-x64.msi)
    手动配置path环境变量:C:\Program Files\CMake\bin
 (2) 安装MS Build工具
     https://www.microsoft.com/en-us/download/details.aspx?id=48159  (BuildTools_Full.exe)
 (3) mingw安装
    详细见http://blog.youkuaiyun.com/zhouwenyuan1015/article/details/73776627
 (4) git bash下安装lightgbm
git clone --recursive https://github.com/Microsoft/LightGBM
cd LightGBM/python-package
python setup.py install --mingw
 (5) 测试
       cmd-->python-->import lightgbm
       没有报错则显示成功

3. 案例测试

# coding: utf-8
# pylint: disable = invalid-name, C0111
import lightgbm as lgb
import pandas as pd
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import GridSearchCV

# load or create your dataset
print('Load data...')
df_train = pd.read_csv('../regression/regression.train', header=None, sep='\t')
df_test = pd.read_csv('../regression/regression.test', header=None, sep='\t')

y_train = df_train[0].values
y_test = df_test[0].values
X_train = df_train.drop(0, axis=1).values
X_test = df_test.drop(0, axis=1).values

print('Start training...')
# train
gbm = lgb.LGBMRegressor(objective='regression',
                        num_leaves=31,
                        learning_rate=0.05,
                        n_estimators=20)
gbm.fit(X_train, y_train,
        eval_set=[(X_test, y_test)],
        eval_metric='l1',
        early_stopping_rounds=5)

print('Start predicting...')
# predict
y_pred = gbm.predict(X_test, num_iteration=gbm.best_iteration)
# eval
print('The rmse of prediction is:', mean_squared_error(y_test, y_pred) ** 0.5)

print('Calculate feature importances...')
# feature importances
print('Feature importances:', list(gbm.feature_importances_))

# other scikit-learn modules
estimator = lgb.LGBMRegressor(num_leaves=31)

param_grid = {
    'learning_rate': [0.01, 0.1, 1],
    'n_estimators': [20, 40]
}

gbm = GridSearchCV(estimator, param_grid)

gbm.fit(X_train, y_train)

print('Best parameters found by grid search are:', gbm.best_params_)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值