y_anisin 2024-03-13 11:30 采纳率: 100%
浏览 421
已结题

运行LGBMClassifier.fit()报错cpu_count() got an unexpected keyword argument 'only_physical_cores'

报错TypeError: cpu_count() got an unexpected keyword argument 'only_physical_cores'

具体代码如下

from lightgbm import LGBMClassifier
from sklearn.datasets import load_iris
from lightgbm import plot_importance
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

# 加载样本数据集
iris = load_iris()
X,y = iris.data,iris.target
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=12343)

model = LGBMClassifier(
    max_depth=3,
    learning_rate=0.1,
    n_estimators=200, # 使用多少个弱分类器
    objective='multiclass',
    num_class=3,
    booster='gbtree',
    min_child_weight=2,
    subsample=0.8,
    colsample_bytree=0.8,
    reg_alpha=0,
    reg_lambda=1,
    seed=0 # 随机数种子
)
model.fit(X_train,y_train, eval_set=[(X_train, y_train), (X_test, y_test)])

# 对测试集进行预测
y_pred = model.predict(X_test)
model.predict_proba
#计算准确率
accuracy = accuracy_score(y_test,y_pred)
print('accuracy:%3.f%%'%(accuracy*100))

# 显示重要特征
plot_importance(model)
plt.show()

img

img

一运行就报错,我的lightgbm版本是4.3.0,python版本为3.7.4,所以这究竟是啥原因导致的

  • 写回答

3条回答 默认 最新

  • 檀越@新空间 2024-03-13 11:31
    关注

    y_anisin 上午好☀️☀️☀️️
    本答案参考ChatGPT-3.5

    这个报错是因为在LightGBM模型中,使用了cpu_count()函数来获取CPU核心数,但是在你的版本中,cpu_count()函数没有only_physical_cores这个参数。

    要解决这个问题,你可以尝试以下几个解决方案:

    1. 升级LightGBM版本: LightGBM是一个持续开发和维护的工具,可能存在一些Bug或API变动。尝试升级到最新版本:pip install --upgrade lightgbm

    2. 降低LightGBM版本: 如果无法升级LightGBM或升级后仍然报错,尝试降低LightGBM的版本为稳定可用的版本。可以使用命令降级:pip install lightgbm==3.2.1

    3. 手动修改代码: 如果你不想升级或降级版本,你可以手动修改代码来解决这个问题。找到compat.py文件,位于lightgbm包中的compat文件夹下。将LGBMCpuCount函数中的only_physical_cores参数移除,改为return cpu_count()即可。修改后的代码如下所示:

    def LGBMCpuCount(only_physical_cores: bool = False) -> int:
        return cpu_count()
    

    请注意,这个修改可能会导致一些性能问题,因为它将默认使用所有的CPU核心。

    总结起来,你可以尝试升级或降级LightGBM版本来解决这个问题,或者手动修改代码以适应当前版本。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 3月21日
  • 已采纳回答 3月13日
  • 创建了问题 3月13日