Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scikit-learn multithreading

Do you know if models from scikit-learn use automatically multithreading or just sequential instructions?

Thanks

like image 380
eezy Avatar asked Jul 13 '18 05:07

eezy


People also ask

Is scikit-learn multithreaded?

Scikit-learn relies heavily on NumPy and SciPy, which internally call multi-threaded linear algebra routines implemented in libraries such as MKL, OpenBLAS or BLIS.

Is sklearn single threaded?

All scikit-learn estimators will by default work on a single thread only.

What does N_jobs =- 1 mean?

None means 1 unless in a joblib. parallel_backend context. -1 means using all processors. This means that the n_jobs parameter can be used to distribute and exploit all the CPUs available in the local computer.


1 Answers

No. All scikit-learn estimators will by default work on a single thread only.

But then again, it all depends on the algorithm and the problem. If the algorithm is such that which want sequential data, we cannot do anything. If the dataset is multi-class or multi-label and algorithm works on a one-vs-rest basis, then yes it can use multi-threading.

Look for a param n_jobs in the utilities or algorithm you want to use, and set it to -1 for using the multi-threading.

For eg.

  • LogisticRegression if working in a binary problem will only train a single model, which will require data sequentially, so here using n_jobs have no effect. But it handles multi-class problems as OvR, so it will have to train those many estimators using the same data. In this case you can use the n_jobs=-1.

  • DecisionTreeClassifier is inherently multi-class enabled and dont need to train multiple models. So we dont have that param there.

  • Ensemble methods like RandomForestClassifier will train multiple estimators (irrespective of problem type) which individually work on some part of data, so here again we can make use of n_jobs.

  • Cross-validation utilities like cross_val_score or GridSearchCV will again work on some part of data or some individual parameters, which is independent of other folds, so here also we can use multi-threading capabilities.

like image 77
Vivek Kumar Avatar answered Oct 04 '22 05:10

Vivek Kumar