如何限制 sklearn 中岭回归模型使用的处理器数量? [英] How do I restrict the number of processors used by the ridge regression model in sklearn?

查看:79
本文介绍了如何限制 sklearn 中岭回归模型使用的处理器数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对不同的机器学习模型进行公平的比较.但是,我发现岭回归模型会自动使用多个处理器,并且没有参数可以限制使用的处理器数量(例如 n_jobs).有没有办法解决这个问题?

I want to make a fair comparison between different machine learning models. However, I find that the ridge regression model will automatically use multiple processors and there is no parameter that I can restrict the number of used processors (such as n_jobs). Is there any possible way to solve this problem?

一个最小的例子:

from sklearn.datasets import make_regression
from sklearn.linear_model import RidgeCV

features, target = make_regression(n_samples=10000, n_features=1000)
r = RidgeCV()
r.fit(features, target)
print(r.score(features, target))

推荐答案

如果您将环境变量 OMP_NUM_THREADS 设置为 n,您将获得预期的行为.例如.在 linux 上,在终端中执行 export OMP_NUM_THREADS=1 以限制使用 1 cpu.

If you set the environmental variable OMP_NUM_THREADS to n, you will get the expected behaviour. E.g. on linux, do export OMP_NUM_THREADS=1 in the terminal to restrict the use to 1 cpu.

根据你的系统,你也可以直接在python中设置.见例如如何在 Python 中设置环境变量?

Depending on your system, you can also set it directly in python. See e.g. How to set environment variables in Python?

这篇关于如何限制 sklearn 中岭回归模型使用的处理器数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆