.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/40_advanced/example_run_with_portfolio.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_40_advanced_example_run_with_portfolio.py: ============================================ Tabular Classification with Greedy Portfolio ============================================ The following example shows how to fit a sample classification model with AutoPyTorch using the greedy portfolio .. GENERATED FROM PYTHON SOURCE LINES 9-27 .. code-block:: default import os import tempfile as tmp import warnings os.environ['JOBLIB_TEMP_FOLDER'] = tmp.gettempdir() os.environ['OMP_NUM_THREADS'] = '1' os.environ['OPENBLAS_NUM_THREADS'] = '1' os.environ['MKL_NUM_THREADS'] = '1' warnings.simplefilter(action='ignore', category=UserWarning) warnings.simplefilter(action='ignore', category=FutureWarning) import sklearn.datasets import sklearn.model_selection from autoPyTorch.api.tabular_classification import TabularClassificationTask .. GENERATED FROM PYTHON SOURCE LINES 28-30 Data Loading ============ .. GENERATED FROM PYTHON SOURCE LINES 30-37 .. code-block:: default X, y = sklearn.datasets.fetch_openml(data_id=40981, return_X_y=True, as_frame=True) X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split( X, y, random_state=42, ) .. GENERATED FROM PYTHON SOURCE LINES 38-40 Build and fit a classifier ========================== .. GENERATED FROM PYTHON SOURCE LINES 40-44 .. code-block:: default api = TabularClassificationTask( seed=42, ) .. GENERATED FROM PYTHON SOURCE LINES 45-47 Search for an ensemble of machine learning algorithms ===================================================== .. GENERATED FROM PYTHON SOURCE LINES 47-61 .. code-block:: default api.search( X_train=X_train, y_train=y_train, X_test=X_test.copy(), y_test=y_test.copy(), optimize_metric='accuracy', total_walltime_limit=300, func_eval_time_limit_secs=50, # Setting this option to "greedy" # will make smac run the configurations # present in 'autoPyTorch/configs/greedy_portfolio.json' portfolio_selection="greedy" ) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 62-64 Print the final ensemble performance ==================================== .. GENERATED FROM PYTHON SOURCE LINES 64-72 .. code-block:: default y_pred = api.predict(X_test) score = api.score(y_pred, y_test) print(score) # Print the final ensemble built by AutoPyTorch print(api.show_models()) # Print statistics from search print(api.sprint_statistics()) .. rst-class:: sphx-glr-script-out .. code-block:: none {'accuracy': 0.8786127167630058} | | Preprocessing | Estimator | Weight | |---:|:-------------------------------------------------------------------------------------------------|:-------------------------------------------------------------------|---------:| | 0 | SimpleImputer,Variance Threshold,MinorityCoalescer,OneHotEncoder,MinMaxScaler,PolynomialFeatures | no embedding,ShapedMLPBackbone,FullyConnectedHead,nn.Sequential | 0.32 | | 1 | SimpleImputer,Variance Threshold,NoCoalescer,OneHotEncoder,NoScaler,KernelPCA | no embedding,MLPBackbone,FullyConnectedHead,nn.Sequential | 0.16 | | 2 | SimpleImputer,Variance Threshold,NoCoalescer,OneHotEncoder,StandardScaler,NoFeaturePreprocessing | no embedding,ShapedMLPBackbone,FullyConnectedHead,nn.Sequential | 0.14 | | 3 | SimpleImputer,Variance Threshold,NoCoalescer,OneHotEncoder,MinMaxScaler,FastICA | no embedding,MLPBackbone,FullyConnectedHead,nn.Sequential | 0.12 | | 4 | SimpleImputer,Variance Threshold,NoCoalescer,OneHotEncoder,StandardScaler,NoFeaturePreprocessing | no embedding,ShapedResNetBackbone,FullyConnectedHead,nn.Sequential | 0.08 | | 5 | None | CBLearner | 0.06 | | 6 | None | ETLearner | 0.06 | | 7 | SimpleImputer,Variance Threshold,NoCoalescer,OneHotEncoder,StandardScaler,NoFeaturePreprocessing | no embedding,ShapedResNetBackbone,FullyConnectedHead,nn.Sequential | 0.02 | | 8 | SimpleImputer,Variance Threshold,NoCoalescer,OneHotEncoder,StandardScaler,NoFeaturePreprocessing | no embedding,ShapedResNetBackbone,FullyConnectedHead,nn.Sequential | 0.02 | | 9 | None | RFLearner | 0.02 | autoPyTorch results: Dataset name: 55bb3950-22f3-11ed-8835-b1fa420cf160 Optimisation Metric: accuracy Best validation score: 0.8830409356725146 Number of target algorithm runs: 25 Number of successful target algorithm runs: 23 Number of crashed target algorithm runs: 0 Number of target algorithms that exceeded the time limit: 2 Number of target algorithms that exceeded the memory limit: 0 .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 5 minutes 21.979 seconds) .. _sphx_glr_download_examples_40_advanced_example_run_with_portfolio.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/automl/Auto-PyTorch/development?urlpath=lab/tree/notebooks/examples/40_advanced/example_run_with_portfolio.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: example_run_with_portfolio.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: example_run_with_portfolio.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_