
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/exercises/plot_iris_exercise.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_exercises_plot_iris_exercise.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_exercises_plot_iris_exercise.py:


================================
SVM Exercise
================================

A tutorial exercise for using different SVM kernels.

This exercise is used in the :ref:`using_kernels_tut` part of the
:ref:`supervised_learning_tut` section of the :ref:`stat_learn_tut_index`.

.. GENERATED FROM PYTHON SOURCE LINES 12-76



.. rst-class:: sphx-glr-horizontal


    *

      .. image-sg:: /auto_examples/exercises/images/sphx_glr_plot_iris_exercise_001.png
         :alt: linear
         :srcset: /auto_examples/exercises/images/sphx_glr_plot_iris_exercise_001.png
         :class: sphx-glr-multi-img

    *

      .. image-sg:: /auto_examples/exercises/images/sphx_glr_plot_iris_exercise_002.png
         :alt: rbf
         :srcset: /auto_examples/exercises/images/sphx_glr_plot_iris_exercise_002.png
         :class: sphx-glr-multi-img

    *

      .. image-sg:: /auto_examples/exercises/images/sphx_glr_plot_iris_exercise_003.png
         :alt: poly
         :srcset: /auto_examples/exercises/images/sphx_glr_plot_iris_exercise_003.png
         :class: sphx-glr-multi-img





.. code-block:: default


    import matplotlib.pyplot as plt
    import numpy as np

    from sklearn import datasets, svm

    iris = datasets.load_iris()
    X = iris.data
    y = iris.target

    X = X[y != 0, :2]
    y = y[y != 0]

    n_sample = len(X)

    np.random.seed(0)
    order = np.random.permutation(n_sample)
    X = X[order]
    y = y[order].astype(float)

    X_train = X[: int(0.9 * n_sample)]
    y_train = y[: int(0.9 * n_sample)]
    X_test = X[int(0.9 * n_sample) :]
    y_test = y[int(0.9 * n_sample) :]

    # fit the model
    for kernel in ("linear", "rbf", "poly"):
        clf = svm.SVC(kernel=kernel, gamma=10)
        clf.fit(X_train, y_train)

        plt.figure()
        plt.clf()
        plt.scatter(
            X[:, 0], X[:, 1], c=y, zorder=10, cmap=plt.cm.Paired, edgecolor="k", s=20
        )

        # Circle out the test data
        plt.scatter(
            X_test[:, 0], X_test[:, 1], s=80, facecolors="none", zorder=10, edgecolor="k"
        )

        plt.axis("tight")
        x_min = X[:, 0].min()
        x_max = X[:, 0].max()
        y_min = X[:, 1].min()
        y_max = X[:, 1].max()

        XX, YY = np.mgrid[x_min:x_max:200j, y_min:y_max:200j]
        Z = clf.decision_function(np.c_[XX.ravel(), YY.ravel()])

        # Put the result into a color plot
        Z = Z.reshape(XX.shape)
        plt.pcolormesh(XX, YY, Z > 0, cmap=plt.cm.Paired)
        plt.contour(
            XX,
            YY,
            Z,
            colors=["k", "k", "k"],
            linestyles=["--", "-", "--"],
            levels=[-0.5, 0, 0.5],
        )

        plt.title(kernel)
    plt.show()


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

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


.. _sphx_glr_download_auto_examples_exercises_plot_iris_exercise.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/exercises/plot_iris_exercise.ipynb
        :alt: Launch binder
        :width: 150 px



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

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

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

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


.. only:: html

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

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