
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/datasets/plot_random_multilabel_dataset.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_datasets_plot_random_multilabel_dataset.py>`
        to download the full example code or to run this example in your browser via Binder

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

.. _sphx_glr_auto_examples_datasets_plot_random_multilabel_dataset.py:


==============================================
Plot randomly generated multilabel dataset
==============================================

This illustrates the :func:`~sklearn.datasets.make_multilabel_classification`
dataset generator. Each sample consists of counts of two features (up to 50 in
total), which are differently distributed in each of two classes.

Points are labeled as follows, where Y means the class is present:

    =====  =====  =====  ======
      1      2      3    Color
    =====  =====  =====  ======
      Y      N      N    Red
      N      Y      N    Blue
      N      N      Y    Yellow
      Y      Y      N    Purple
      Y      N      Y    Orange
      Y      Y      N    Green
      Y      Y      Y    Brown
    =====  =====  =====  ======

A star marks the expected sample for each class; its size reflects the
probability of selecting that class label.

The left and right examples highlight the ``n_labels`` parameter:
more of the samples in the right plot have 2 or 3 labels.

Note that this two-dimensional example is very degenerate:
generally the number of features would be much greater than the
"document length", while here we have much larger documents than vocabulary.
Similarly, with ``n_classes > n_features``, it is much less likely that a
feature distinguishes a particular class.

.. GENERATED FROM PYTHON SOURCE LINES 37-107



.. image-sg:: /auto_examples/datasets/images/sphx_glr_plot_random_multilabel_dataset_001.png
   :alt: n_labels=1, length=50, n_labels=3, length=50
   :srcset: /auto_examples/datasets/images/sphx_glr_plot_random_multilabel_dataset_001.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    The data was generated from (random_state=73):
    Class   P(C)    P(w0|C) P(w1|C)
    red     0.38    0.71    0.29
    blue    0.32    0.46    0.54
    yellow  0.30    0.22    0.78






|

.. code-block:: default


    import matplotlib.pyplot as plt
    import numpy as np

    from sklearn.datasets import make_multilabel_classification as make_ml_clf

    COLORS = np.array(
        [
            "!",
            "#FF3333",  # red
            "#0198E1",  # blue
            "#BF5FFF",  # purple
            "#FCD116",  # yellow
            "#FF7216",  # orange
            "#4DBD33",  # green
            "#87421F",  # brown
        ]
    )

    # Use same random seed for multiple calls to make_multilabel_classification to
    # ensure same distributions
    RANDOM_SEED = np.random.randint(2**10)


    def plot_2d(ax, n_labels=1, n_classes=3, length=50):
        X, Y, p_c, p_w_c = make_ml_clf(
            n_samples=150,
            n_features=2,
            n_classes=n_classes,
            n_labels=n_labels,
            length=length,
            allow_unlabeled=False,
            return_distributions=True,
            random_state=RANDOM_SEED,
        )

        ax.scatter(
            X[:, 0], X[:, 1], color=COLORS.take((Y * [1, 2, 4]).sum(axis=1)), marker="."
        )
        ax.scatter(
            p_w_c[0] * length,
            p_w_c[1] * length,
            marker="*",
            linewidth=0.5,
            edgecolor="black",
            s=20 + 1500 * p_c**2,
            color=COLORS.take([1, 2, 4]),
        )
        ax.set_xlabel("Feature 0 count")
        return p_c, p_w_c


    _, (ax1, ax2) = plt.subplots(1, 2, sharex="row", sharey="row", figsize=(8, 4))
    plt.subplots_adjust(bottom=0.15)

    p_c, p_w_c = plot_2d(ax1, n_labels=1)
    ax1.set_title("n_labels=1, length=50")
    ax1.set_ylabel("Feature 1 count")

    plot_2d(ax2, n_labels=3)
    ax2.set_title("n_labels=3, length=50")
    ax2.set_xlim(left=0, auto=True)
    ax2.set_ylim(bottom=0, auto=True)

    plt.show()

    print("The data was generated from (random_state=%d):" % RANDOM_SEED)
    print("Class", "P(C)", "P(w0|C)", "P(w1|C)", sep="\t")
    for k, p, p_w in zip(["red", "blue", "yellow"], p_c, p_w_c.T):
        print("%s\t%0.2f\t%0.2f\t%0.2f" % (k, p, p_w[0], p_w[1]))


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

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


.. _sphx_glr_download_auto_examples_datasets_plot_random_multilabel_dataset.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-learn/scikit-learn/1.3.X?urlpath=lab/tree/notebooks/auto_examples/datasets/plot_random_multilabel_dataset.ipynb
        :alt: Launch binder
        :width: 150 px



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

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

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

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


.. only:: html

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

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