V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
QiShine
V2EX  ›  Python

机器学习训练的时候,为啥 CPU 占用率到不了 100%?

  •  
  •   QiShine · 122 天前 · 1525 次点击
    这是一个创建于 122 天前的主题,其中的信息可能已经有所发展或是发生改变。

    使用 scikit-learn ,训练一个随机森林。所有的数据都在内存里,但是训练时间还是很长,但是看任务管理器里面,CPU 占用一直 40%,不是应该 100%吗?是不是 Python GIL 的问题?怎么样才可以提速?

    6 条回复    2023-12-31 15:50:07 +08:00
    czfy
        1
    czfy  
       122 天前
    1. 你用的是什么 CPU
    2. 占用 40%,具体是 1 核有难多核围观,还是每个核都有占用
    3. 最好还是把代码列出来
    Yuanlaoer
        2
    Yuanlaoer  
       122 天前
    所有数据都在“内存”里,“CPU”占用一直 40%

    内存,CPU
    QiShine
        3
    QiShine  
    OP
       121 天前
    ```
    # transform list into array
    train = asarray(train)
    # split into input and output columns
    trainX, trainy = train[:, :-1], train[:, -1]
    # fit model
    model = RandomForestRegressor(n_estimators=1000)
    model.fit(trainX, trainy)
    # make a one-step prediction
    yhat = model.predict([testX])
    return yhat[0]
    ```
    czfy
        4
    czfy  
       121 天前
    你也没说全
    先试试在 import 之后加一行 n_jobs = -1 吧
    或者自己看文档 https://scikit-learn.org/stable/modules/ensemble.html#parallelization
    QiShine
        5
    QiShine  
    OP
       121 天前
    @czfy 感谢回复!改成 model = RandomForestRegressor(n_estimators=1000, n_jobs=-1)之后,速度快了两倍。
    czfy
        6
    czfy  
       118 天前 via Android
    有用就好
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2955 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 13:14 · PVG 21:14 · LAX 06:14 · JFK 09:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.