在R中安装lightgbm

5
我尝试了不同的方法来安装`lightgbm`包,但都失败了。我试过在github仓库中的所有方法,但它们都没有起作用。 我运行Windows 10和R 3.5(64位)。有遇到类似的问题。所以我尝试了他的解决方案:
    1. 安装cmake(64位)
    1. 安装Visual Studio(2017)
    1. 安装Rtools(64位)
    1. 更改系统变量中的路径为"C:\Program Files\CMake\bin\cmake;"
    1. 使用预编译的dll/lib文件安装lightgbm

-->

  devtools::install_github("Laurae2/lgbdl", force = TRUE)
  library(lgbdl)
  lgb.dl(commit = "master",
  compiler = "vs",
  repo = "https://github.com/Microsoft/LightGBM")

 *** arch - i386
installing via 'install.libs.R' to C:/Users/X1/Documents/R/win- 
library/3.5/lightgbm
Error in eval(ei, envir) : Cannot find lib_lightgbm.dll
* removing 'C:/Users/XXX/Documents/R/win-library/3.5/lightgbm'
In R CMD INSTALL
installation of package 
�C:/Users/XXX/AppData/Local/Temp/RtmpczNLaN/LightGBM/R-package� had non- 
zero exit status[1] FALSE

有任何想法如何解决这个问题吗?
4个回答

3

对我来说有效,希望能帮到你。

  1. 确保您安装了所有必需的软件。

  2. https://github.com/Microsoft/LightGBM/releases下载预编译的.dll文件并将其放在.\LightGBM\目录下。

  3. 在install.libs.R中,设置use_precompile <- TRUE。

  4. 将根目录中的CMakeLists.txt复制到.\LightGBM\R-package\inst\bin目录中。

  5. 在R控制台中键入: install.packages(file.path("C:\yourdirectory\", "LightGBM", "R-package"), repos = NULL, type = "source")


3

如果有一些不太顺利的人在解决这个问题时,以上提到的解决方法都没有起作用。我所做的是:

最初的回答:

对于一些遇到问题的可怜人,如果上面提到的解决方法都没有起作用。我必须做的是:

  • Ensure you have the following in your path environment variables:

    • Rtools (point to the bin folder in the install directory of rtools)

    • Rtools mingw_64 (point to the mingw_64 folder in the rtools install directory)

    • Base R (point to the bin folder in your base R install directory, i.e. where you install R version whatever)

    • CMake (point to the bin folder in your cmake install directory)

    • Visual Studio (allows you to build with VS Build Tools, otherwise will fallback to RTools or any MinGW64 available as stated on the github page)

    • Git

  • Before installing LightGBM, install the following packages in R itself:

    • data.tools

    • magrittr

    • R6

    • jsonlite

  • Once all the above is done, run with the git installation instructions as on the github page here

    For those not able to access the link (or if it should move), the command are the following:

    git clone --recursive https://github.com/microsoft/LightGBM
    
    cd LightGBM
    
    Rscript build_r.R
    

1

经过“无数次”尝试,我终于成功安装了它。我必须确保只安装了一个 R 版本 - 64 位,并且所有其他步骤都设置了预期的环境变量。


0

所以你想在R中运行LightGBM,但你不是一名软件工程师... 那么,你可以使用R和Python(因为安装R包很麻烦)。

首先,在你的R控制台或R Markdown中写入以下内容:

library(reticulate)
library(knitr)
knitr::knit_engines$set(python3 = reticulate::eng_python)

其次,在您的R控制台或R Markdown中编写以下内容:

Sys.which("python3")
os = import("os")
os$listdir(".")

第三步,打开记事本,将其保存为“script.py”,编写您的函数并调用适当的库。类似这样的代码:
import pandas as pd
import lightgbm as lgb

def python_to_r_lgbm(X_train, y_train, X_test):
    lgb_train = lgb.Dataset(X_train, y_train)

    params = {'bagging_fraction': 0.75, 
              'boosting_type': 'gbdt', 
              'drop_rate': 0.15, 
              'feature_fraction': 1, 
              'lambda_l1': 2.95, 
              'lambda_l2': 2.35, 
              'learning_rate': 0.01, 
              'max_depth': 3, 'max_leaves': 19, 
              'min_data_in_leaf': 8, 
              'objective': 'regression', 
              'metrics': ['l1', 'l2', 'huber'], 
              'verbose': -1}

    gbm = lgb.train(params, lgb_train, 1800)

    light_gbm_test = gbm.predict(X_test)

    return light_gbm_test

然后,在您的R控制台或R Markdown中运行此命令:

source_python('script.py')

最后,在你的 R 控制台(或 R Markdown)中调用你的新函数:
python_to_r_lgbm(xTrain_df, y_train, xTest_df)

这个函数将使用xTrain_df和y_train训练您的模型,并使用xTest_df给出预测结果。

*请确保所有文件都在同一个文件夹中。此外,请确保已安装Python3(显然)和lightgbm库(https://anaconda.org/conda-forge/lightgbm)。

开个玩笑,我希望这可以帮助到您。同时,我也希望不会被愤怒的群众围攻,因为我没有直接回答问题:D

来源:https://rviews.rstudio.com/2019/03/18/the-reticulate-package-solves-the-hardest-problem-in-data-science-people/ 虽然我做了一些修改。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接