|
| 1 | +# -*- encoding: utf-8 -*- |
| 2 | +""" |
| 3 | +====================== |
| 4 | +Obtain run information |
| 5 | +====================== |
| 6 | +
|
| 7 | +The following example shows how to obtain information from a finished |
| 8 | +Auto-sklearn run. In particular, it shows: |
| 9 | +* how to query which models were evaluated by Auto-sklearn |
| 10 | +* how to query the models in the final ensemble |
| 11 | +* how to get general statistics on the what Auto-sklearn evaluated |
| 12 | +
|
| 13 | +Auto-sklearn is a wrapper on top of |
| 14 | +the sklearn models. This example illustrates how to interact |
| 15 | +with the sklearn components directly, in this case a PCA preprocessor. |
| 16 | +""" |
| 17 | +import sklearn.datasets |
| 18 | +import sklearn.metrics |
| 19 | + |
| 20 | +import autosklearn.classification |
| 21 | + |
| 22 | +############################################################################ |
| 23 | +# Data Loading |
| 24 | +# ============ |
| 25 | + |
| 26 | +X, y = sklearn.datasets.load_breast_cancer(return_X_y=True) |
| 27 | +X_train, X_test, y_train, y_test = \ |
| 28 | + sklearn.model_selection.train_test_split(X, y, random_state=1) |
| 29 | + |
| 30 | +############################################################################ |
| 31 | +# Build and fit the classifier |
| 32 | +# ============================ |
| 33 | + |
| 34 | +automl = autosklearn.classification.AutoSklearnClassifier( |
| 35 | + time_left_for_this_task=30, |
| 36 | + per_run_time_limit=10, |
| 37 | + disable_evaluator_output=False, |
| 38 | + # To simplify querying the models in the final ensemble, we |
| 39 | + # restrict auto-sklearn to use only pca as a preprocessor |
| 40 | + include_preprocessors=['pca'], |
| 41 | +) |
| 42 | +automl.fit(X_train, y_train, dataset_name='breast_cancer') |
| 43 | + |
| 44 | +############################################################################ |
| 45 | +# Predict using the model |
| 46 | +# ======================= |
| 47 | + |
| 48 | +predictions = automl.predict(X_test) |
| 49 | +print("Accuracy score:{}".format( |
| 50 | + sklearn.metrics.accuracy_score(y_test, predictions)) |
| 51 | +) |
| 52 | + |
| 53 | + |
| 54 | +############################################################################ |
| 55 | +# Report the models found by Auto-Sklearn |
| 56 | +# ======================================= |
| 57 | +# |
| 58 | +# Auto-sklearn uses |
| 59 | +# `Ensemble Selection <https://www.cs.cornell.edu/~alexn/papers/shotgun.icml04.revised.rev2.pdf>`_ |
| 60 | +# to construct ensembles in a post-hoc fashion. The ensemble is a linear |
| 61 | +# weighting of all models constructed during the hyperparameter optimization. |
| 62 | +# This prints the final ensemble. It is a list of tuples, each tuple being |
| 63 | +# the model weight in the ensemble and the model itself. |
| 64 | + |
| 65 | +print(automl.show_models()) |
| 66 | + |
| 67 | +########################################################################### |
| 68 | +# Report statistics about the search |
| 69 | +# ================================== |
| 70 | +# |
| 71 | +# Print statistics about the auto-sklearn run such as number of |
| 72 | +# iterations, number of models failed with a time out etc. |
| 73 | +print(automl.sprint_statistics()) |
| 74 | + |
| 75 | +############################################################################ |
| 76 | +# Detailed statistics about the search - part 1 |
| 77 | +# ============================================= |
| 78 | +# |
| 79 | +# Auto-sklearn also keeps detailed statistics of the hyperparameter |
| 80 | +# optimization procedurce, which are stored in a so-called |
| 81 | +# `run history <https://automl.github.io/SMAC3/master/apidoc/smac. |
| 82 | +# runhistory.runhistory.html#smac.runhistory# .runhistory.RunHistory>`_. |
| 83 | + |
| 84 | +print(automl._automl[0].runhistory_) |
| 85 | + |
| 86 | +############################################################################ |
| 87 | +# Runs are stored inside an ``OrderedDict`` called ``data``: |
| 88 | + |
| 89 | +print(len(automl._automl[0].runhistory_.data)) |
| 90 | + |
| 91 | +############################################################################ |
| 92 | +# Let's iterative over all entries |
| 93 | + |
| 94 | +for run_key in automl._automl[0].runhistory_.data: |
| 95 | + print('#########') |
| 96 | + print(run_key) |
| 97 | + print(automl._automl[0].runhistory_.data[run_key]) |
| 98 | + |
| 99 | +############################################################################ |
| 100 | +# and have a detailed look at one entry: |
| 101 | + |
| 102 | +run_key = list(automl._automl[0].runhistory_.data.keys())[0] |
| 103 | +run_value = automl._automl[0].runhistory_.data[run_key] |
| 104 | + |
| 105 | +############################################################################ |
| 106 | +# The ``run_key`` contains all information describing a run: |
| 107 | + |
| 108 | +print("Configuration ID:", run_key.config_id) |
| 109 | +print("Instance:", run_key.instance_id) |
| 110 | +print("Seed:", run_key.seed) |
| 111 | +print("Budget:", run_key.budget) |
| 112 | + |
| 113 | +############################################################################ |
| 114 | +# and the configuration can be looked up in the run history as well: |
| 115 | + |
| 116 | +print(automl._automl[0].runhistory_.ids_config[run_key.config_id]) |
| 117 | + |
| 118 | +############################################################################ |
| 119 | +# The only other important entry is the budget in case you are using |
| 120 | +# auto-sklearn with |
| 121 | +# `successive halving <examples/60_search/example_successive_halving.py>`_. |
| 122 | +# The remaining parts of the key can be ignored for auto-sklearn and are |
| 123 | +# only there because the underlying optimizer, SMAC, can handle more general |
| 124 | +# problems, too. |
| 125 | + |
| 126 | +############################################################################ |
| 127 | +# The ``run_value`` contains all output from running the configuration: |
| 128 | + |
| 129 | +print("Cost:", run_value.cost) |
| 130 | +print("Time:", run_value.time) |
| 131 | +print("Status:", run_value.status) |
| 132 | +print("Additional information:", run_value.additional_info) |
| 133 | +print("Start time:", run_value.starttime) |
| 134 | +print("End time", run_value.endtime) |
| 135 | + |
| 136 | +############################################################################ |
| 137 | +# Cost is basically the same as a loss. In case the metric to optimize for |
| 138 | +# should be maximized, it is internally transformed into a minimization |
| 139 | +# metric. Additionally, the status type gives information on whether the run |
| 140 | +# was successful, while the additional information's most interesting entry |
| 141 | +# is the internal training loss. Furthermore, there is detailed information |
| 142 | +# on the runtime available. |
| 143 | + |
| 144 | +############################################################################ |
| 145 | +# Detailed statistics about the search - part 2 |
| 146 | +# ============================================= |
| 147 | +# |
| 148 | +# To maintain compatibility with scikit-learn, Auto-sklearn gives the |
| 149 | +# same data as |
| 150 | +# `cv_results_ <https://scikit-learn.org/stable/modules/generated/sklearn. |
| 151 | +# model_selection.GridSearchCV.html>`_. |
| 152 | + |
| 153 | +print(automl.cv_results_) |
| 154 | + |
| 155 | +############################################################################ |
| 156 | +# Inspect the components of the best model |
| 157 | +# ======================================== |
| 158 | +# |
| 159 | +# Iterate over the components of the model and print |
| 160 | +# The explained variance ratio per stage |
| 161 | +for i, (weight, pipeline) in enumerate(automl.get_models_with_weights()): |
| 162 | + for stage_name, component in pipeline.named_steps.items(): |
| 163 | + if 'preprocessor' in stage_name: |
| 164 | + print( |
| 165 | + "The {}th pipeline has a explained variance of {}".format( |
| 166 | + i, |
| 167 | + # The component is an instance of AutoSklearnChoice. |
| 168 | + # Access the sklearn object via the choice attribute |
| 169 | + # We want the explained variance attributed of |
| 170 | + # each principal component |
| 171 | + component.choice.preprocessor.explained_variance_ratio_ |
| 172 | + ) |
| 173 | + ) |
0 commit comments