.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/40_advanced/example_feature_types.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_feature_types.py: ============= Feature Types ============= In *auto-sklearn* it is possible to specify the feature types of a dataset when calling the method :meth:`fit() ` by specifying the argument ``feat_type``. The following example demonstrates a way it can be done. Additionally, you can provide a properly formatted pandas DataFrame, and the feature types will be automatically inferred, as demonstrated in :ref:`sphx_glr_examples_40_advanced_example_pandas_train_test.py`. .. GENERATED FROM PYTHON SOURCE LINES 16-25 .. code-block:: default import numpy as np import sklearn.model_selection import sklearn.datasets import sklearn.metrics import autosklearn.classification .. GENERATED FROM PYTHON SOURCE LINES 26-29 Data Loading ============ Load Australian dataset from https://www.openml.org/d/40981 .. GENERATED FROM PYTHON SOURCE LINES 29-46 .. code-block:: default bunch = data = sklearn.datasets.fetch_openml(data_id=40981, as_frame=True) y = bunch["target"].to_numpy() X = bunch["data"].to_numpy(np.float) X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split( X, y, random_state=1 ) # Auto-sklearn can automatically recognize categorical/numerical data from a pandas # DataFrame. This example highlights how the user can provide the feature types, # when using numpy arrays, as there is no per-column dtype in this case. # feat_type is a list that tags each column from a DataFrame/ numpy array / list # with the case-insensitive string categorical or numerical, accordingly. feat_type = [ "Categorical" if x.name == "category" else "Numerical" for x in bunch["data"].dtypes ] .. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/work/auto-sklearn/auto-sklearn/examples/40_advanced/example_feature_types.py:31: DeprecationWarning: `np.float` is a deprecated alias for the builtin `float`. To silence this warning, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here. Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations X = bunch["data"].to_numpy(np.float) .. GENERATED FROM PYTHON SOURCE LINES 47-49 Build and fit a classifier ========================== .. GENERATED FROM PYTHON SOURCE LINES 49-59 .. code-block:: default cls = autosklearn.classification.AutoSklearnClassifier( time_left_for_this_task=30, # Bellow two flags are provided to speed up calculations # Not recommended for a real implementation initial_configurations_via_metalearning=0, smac_scenario_args={"runcount_limit": 1}, ) cls.fit(X_train, y_train, X_test, y_test, feat_type=feat_type) .. rst-class:: sphx-glr-script-out .. code-block:: none AutoSklearnClassifier(ensemble_class=, initial_configurations_via_metalearning=0, per_run_time_limit=3, smac_scenario_args={'runcount_limit': 1}, time_left_for_this_task=30) .. GENERATED FROM PYTHON SOURCE LINES 60-62 Get the Score of the final ensemble =================================== .. GENERATED FROM PYTHON SOURCE LINES 62-65 .. code-block:: default predictions = cls.predict(X_test) print("Accuracy score", sklearn.metrics.accuracy_score(y_test, predictions)) .. rst-class:: sphx-glr-script-out .. code-block:: none Accuracy score 0.8786127167630058 .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 25.381 seconds) .. _sphx_glr_download_examples_40_advanced_example_feature_types.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-sklearn/master?urlpath=lab/tree/notebooks/examples/40_advanced/example_feature_types.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: example_feature_types.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: example_feature_types.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_