一、报错内容

在Jupyter notebook中导入lightgbm时报错:

Referenced from: /opt/homebrew/anaconda3/envs/biligame/lib/python3.7/site-packages/lightgbm/lib_lightgbm.so
  Reason: tried: '/usr/local/opt/libomp/lib/libomp.dylib' (no such file), '/usr/local/lib/libomp.dylib' (no such file), '/usr/lib/libomp.dylib' (no such file)

二、尝试失败

1.一些解决方法

找了很多文章,尝试了无数次,无果。。。最详细的是这一篇:
[python] [lightgbm] Mac下安装lightgbm
其他参考:
M1芯片mac安装xgboost和lightgbm
MAC m1安装lightgbm时报错
三步在MacOS Anaconda安装ligthGBM
python3——解决MAC在import lightgbm时报错image not found
Mac环境下安装LightGBM的闯关记
MacBook Pro中安装LightGBM的超详细步骤

2.我的问题

我每次执行到这一步就会失败

git clone --recursive https://github.com/Microsoft/LightGBM  

所以我放弃了

尝试在jupyter里重新用pip安装:

安装没问题√

pip install lightgbm

导入没问题√

import lightgbm as lgb

可是,运行模型会提示内核挂掉 。。。。

在这里插入图片描述

三、解决办法

功夫不负有心人,在我准备放弃之时,居然找到了解决方法!
超简单的,为什么我一开始没想到!

1.在jupyter里运行:

conda install lightgbm

运行结果很长,看上去很曲折,但是成功了!

The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    _anaconda_depends-2021.11  |           py39_0           7 KB
    anaconda-custom            |           py39_1           4 KB
    ca-certificates-2022.4.26  |       hecd8cb5_0         124 KB
    lightgbm-3.2.1             |   py39h23ab428_0         996 KB
    openssl-1.1.1o             |       hca72f7f_0         2.2 MB
    wheel-0.37.1               |     pyhd3eb1b0_0          33 KB
    ------------------------------------------------------------
                                           Total:         3.3 MB

The following NEW packages will be INSTALLED:

  _anaconda_depends  pkgs/main/osx-64::_anaconda_depends-2021.11-py39_0
  lightgbm           pkgs/main/osx-64::lightgbm-3.2.1-py39h23ab428_0
  wheel              pkgs/main/noarch::wheel-0.37.1-pyhd3eb1b0_0

The following packages will be UPDATED:

  ca-certificates                     2021.10.26-hecd8cb5_2 --> 2022.4.26-hecd8cb5_0
  openssl                                 1.1.1l-h9ed2024_0 --> 1.1.1o-hca72f7f_0

The following packages will be DOWNGRADED:

  anaconda                                   2021.11-py39_0 --> custom-py39_1



Downloading and Extracting Packages
wheel-0.37.1         | 33 KB     | ##################################### | 100% 
lightgbm-3.2.1       | 996 KB    | ##################################### | 100% 
anaconda-custom      | 4 KB      | ##################################### | 100% 
_anaconda_depends-20 | 7 KB      | ##################################### | 100% 
ca-certificates-2022 | 124 KB    | ##################################### | 100% 
openssl-1.1.1o       | 2.2 MB    | ##################################### | 100% 
Preparing transaction: done
Verifying transaction: done
Executing transaction: done

Note: you may need to restart the kernel to use updated packages.

2.测试代码:

运行看看是否能正常使用

# coding: utf-8
# pylint: disable = invalid-name, C0111
 
# 函数的更多使用方法参见LightGBM官方文档:http://lightgbm.readthedocs.io/en/latest/Python-Intro.html
 
import json
import lightgbm as lgb
import pandas as pd
from sklearn.metrics import mean_squared_error
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.datasets import  make_classification
from lightgbm import early_stopping
 
iris = load_iris()   # 载入鸢尾花数据集
data=iris.data
target = iris.target
X_train,X_test,y_train,y_test =train_test_split(data,target,test_size=0.2)
 
    
# 加载你的数据
# 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
 
# 创建成lgb特征的数据集格式
lgb_train = lgb.Dataset(X_train, y_train) # 将数据保存到LightGBM二进制文件将使加载更快
lgb_eval = lgb.Dataset(X_test, y_test, reference=lgb_train)  # 创建验证数据
 
# 将参数写成字典下形式
params = {
    'task': 'train',
    'boosting_type': 'gbdt',  # 设置提升类型
    'objective': 'regression', # 目标函数
    'metric': {'l2', 'auc'},  # 评估函数
    'num_leaves': 31,   # 叶子节点数
    'learning_rate': 0.05,  # 学习速率
    'feature_fraction': 0.9, # 建树的特征选择比例
    'bagging_fraction': 0.8, # 建树的样本采样比例
    'bagging_freq': 5,  # k 意味着每 k 次迭代执行bagging
    'verbose': 1 # <0 显示致命的, =0 显示错误 (警告), >0 显示信息
}
 
print('Start training...')
# 训练 cv and train
callbacks = [early_stopping(stopping_rounds=5)]

gbm = lgb.train(params,lgb_train,num_boost_round=20,valid_sets=lgb_eval,callbacks=callbacks) # 训练数据需要参数列表和数据集
 
print('Save model...') 
 
gbm.save_model('model.txt')   # 训练后保存模型到文件
 
print('Start predicting...')
# 预测数据集
y_pred = gbm.predict(X_test, num_iteration=gbm.best_iteration) #如果在训练期间启用了早期停止,可以通过best_iteration方式从最佳迭代中获得预测
# 评估模型
print('The rmse of prediction is:', mean_squared_error(y_test, y_pred) ** 0.5) # 计算真实值和预测值之间的均方根误差

成功了!

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐