.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/classification/plot_feature_importances_script.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_auto_examples_classification_plot_feature_importances_script.py>`
        to download the full example code. or to run this example in your browser via JupyterLite or Binder

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_classification_plot_feature_importances_script.py:


plot_feature_importances with examples
======================================

An example showing the :py:func:`~scikitplot.api.estimators.plot_feature_importances` function
used by a scikit-learn classifier.

.. GENERATED FROM PYTHON SOURCE LINES 8-54

.. code-block:: Python


    # Authors: The scikit-plots developers
    # SPDX-License-Identifier: BSD-3-Clause

    from sklearn.datasets import (
      make_classification,
      load_breast_cancer as data_2_classes,
      load_iris as data_3_classes,
      load_digits as data_10_classes,
    )
    from sklearn.model_selection import train_test_split
    from sklearn.linear_model import LogisticRegression
    from sklearn.naive_bayes import GaussianNB
    from sklearn.svm import LinearSVC
    from sklearn.ensemble import RandomForestClassifier
    from sklearn.model_selection import cross_val_predict

    import numpy as np; np.random.seed(0)  # reproducibility
    # importing pylab or pyplot
    import matplotlib.pyplot as plt

    # Import scikit-plot
    import scikitplot as sp

    # Load the data
    X, y = data_3_classes(return_X_y=True, as_frame=False)
    X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.5, random_state=0)

    # Create an instance of the LogisticRegression
    model = RandomForestClassifier(random_state=0).fit(X_train, y_train)

    # Plot!
    ax, features = sp.estimators.plot_feature_importances(
      model, 
      feature_names=['petal length', 'petal width', 'sepal length', 'sepal width'],
    );

    # Adjust layout to make sure everything fits
    plt.tight_layout()

    # Save the plot with a filename based on the current script's name
    # sp.api._utils.save_plot()

    # Display the plot
    plt.show(block=True)




.. image-sg:: /auto_examples/classification/images/sphx_glr_plot_feature_importances_script_001.png
   :alt: RandomForestClassifier Feature Importances
   :srcset: /auto_examples/classification/images/sphx_glr_plot_feature_importances_script_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 55-68

.. admonition:: References

   The use of the following functions, methods, classes and modules is shown
   in this example:

   - https://scikit-learn.org/stable/auto_examples/ensemble/plot_forest_importances.html

.. tags::

   model-type: classification
   model-workflow: model evaluation
   plot-type: bar
   level: beginner
   purpose: showcase


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 0.557 seconds)


.. _sphx_glr_download_auto_examples_classification_plot_feature_importances_script.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/scikit-plots/scikit-plots/0.4.X?urlpath=lab/tree/notebooks/auto_examples/classification/plot_feature_importances_script.ipynb
        :alt: Launch binder
        :width: 150 px

    .. container:: lite-badge

      .. image:: images/jupyterlite_badge_logo.svg
        :target: ../../lite/lab/index.html?path=auto_examples/classification/plot_feature_importances_script.ipynb
        :alt: Launch JupyterLite
        :width: 150 px

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: plot_feature_importances_script.ipynb <plot_feature_importances_script.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: plot_feature_importances_script.py <plot_feature_importances_script.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: plot_feature_importances_script.zip <plot_feature_importances_script.zip>`


.. include:: plot_feature_importances_script.recommendations


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_