.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/basic/run_n_jobs.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_basic_run_n_jobs.py: Parallelize Julearn ============================ In this example we will parallelize outer cross-validation and/or inner cross-validation for hyperparameter search. .. include:: ../../links.inc .. GENERATED FROM PYTHON SOURCE LINES 10-16 .. code-block:: default # Authors: Sami Hamdan # # License: AGPL from seaborn import load_dataset from julearn import run_cross_validation .. GENERATED FROM PYTHON SOURCE LINES 17-18 prepare some simple standard input .. GENERATED FROM PYTHON SOURCE LINES 18-25 .. code-block:: default df_iris = load_dataset("iris") df_iris = df_iris[df_iris['species'].isin(['versicolor', 'virginica'])] X = ['sepal_length', 'sepal_width', 'petal_length'] y = 'species' .. GENERATED FROM PYTHON SOURCE LINES 26-27 run without any parallelization .. GENERATED FROM PYTHON SOURCE LINES 27-32 .. code-block:: default scores = run_cross_validation( X=X, y=y, data=df_iris, model='svm', model_params=dict(svm__C=[1, 2, 3])) .. GENERATED FROM PYTHON SOURCE LINES 33-38 To add parallelization to the outer cross-validation we will add the n_jobs argument to run_cross_validation. We can use verbose > 0 to get more information about the parallelization done. Here, I will set the parallel jobs to 2. .. GENERATED FROM PYTHON SOURCE LINES 38-45 .. code-block:: default scores = run_cross_validation( X=X, y=y, data=df_iris, model='svm', model_params=dict(svm__C=[1, 2, 3]), n_jobs=2, verbose=3 ) .. GENERATED FROM PYTHON SOURCE LINES 46-51 We can also parallelize over the hyperparameter search/inner cv. This will work by using the n_jobs argument of the searcher itself, e.g. by default `sklearn.model_selection.GridSearchCV`. To adjust the parameters of the search we have to use the search_params inside of the model_params like this: .. GENERATED FROM PYTHON SOURCE LINES 51-62 .. code-block:: default model_params = dict( svm__C=[1, 2, 3], search_params=dict(n_jobs=2, verbose=3) ) scores = run_cross_validation( X=X, y=y, data=df_iris, model='svm', model_params=model_params ) .. rst-class:: sphx-glr-script-out .. code-block:: none Fitting 5 folds for each of 3 candidates, totalling 15 fits Fitting 5 folds for each of 3 candidates, totalling 15 fits Fitting 5 folds for each of 3 candidates, totalling 15 fits Fitting 5 folds for each of 3 candidates, totalling 15 fits Fitting 5 folds for each of 3 candidates, totalling 15 fits Fitting 5 folds for each of 3 candidates, totalling 15 fits Fitting 5 folds for each of 3 candidates, totalling 15 fits Fitting 5 folds for each of 3 candidates, totalling 15 fits Fitting 5 folds for each of 3 candidates, totalling 15 fits Fitting 5 folds for each of 3 candidates, totalling 15 fits Fitting 5 folds for each of 3 candidates, totalling 15 fits Fitting 5 folds for each of 3 candidates, totalling 15 fits Fitting 5 folds for each of 3 candidates, totalling 15 fits Fitting 5 folds for each of 3 candidates, totalling 15 fits Fitting 5 folds for each of 3 candidates, totalling 15 fits Fitting 5 folds for each of 3 candidates, totalling 15 fits Fitting 5 folds for each of 3 candidates, totalling 15 fits Fitting 5 folds for each of 3 candidates, totalling 15 fits Fitting 5 folds for each of 3 candidates, totalling 15 fits Fitting 5 folds for each of 3 candidates, totalling 15 fits Fitting 5 folds for each of 3 candidates, totalling 15 fits Fitting 5 folds for each of 3 candidates, totalling 15 fits Fitting 5 folds for each of 3 candidates, totalling 15 fits Fitting 5 folds for each of 3 candidates, totalling 15 fits Fitting 5 folds for each of 3 candidates, totalling 15 fits .. GENERATED FROM PYTHON SOURCE LINES 63-67 Depending on your resources you can use n_jobs for outer cv, inner cv or even as a model_parameter for some models like `rf`. Additionally, you can also use the scikitlearns `parallel_backend` for parallelization. .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 9.662 seconds) .. _sphx_glr_download_auto_examples_basic_run_n_jobs.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: run_n_jobs.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: run_n_jobs.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_