.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "packages/scikit-learn/auto_examples/plot_iris_knn.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_packages_scikit-learn_auto_examples_plot_iris_knn.py: Nearest-neighbor prediction on iris ==================================== Plot the decision boundary of nearest neighbor decision on iris, first with a single nearest neighbor, and then using 3 nearest neighbors. .. GENERATED FROM PYTHON SOURCE LINES 8-32 .. code-block:: Python import numpy as np import matplotlib.pyplot as plt from sklearn import neighbors, datasets from matplotlib.colors import ListedColormap # Create color maps for 3-class classification problem, as with iris cmap_light = ListedColormap(["#FFAAAA", "#AAFFAA", "#AAAAFF"]) cmap_bold = ListedColormap(["#FF0000", "#00FF00", "#0000FF"]) iris = datasets.load_iris() X = iris.data[:, :2] # we only take the first two features. We could # avoid this ugly slicing by using a two-dim dataset y = iris.target knn = neighbors.KNeighborsClassifier(n_neighbors=1) knn.fit(X, y) x_min, x_max = X[:, 0].min() - 0.1, X[:, 0].max() + 0.1 y_min, y_max = X[:, 1].min() - 0.1, X[:, 1].max() + 0.1 xx, yy = np.meshgrid(np.linspace(x_min, x_max, 100), np.linspace(y_min, y_max, 100)) Z = knn.predict(np.c_[xx.ravel(), yy.ravel()]) .. GENERATED FROM PYTHON SOURCE LINES 33-34 Put the result into a color plot .. GENERATED FROM PYTHON SOURCE LINES 34-45 .. code-block:: Python Z = Z.reshape(xx.shape) plt.figure() plt.pcolormesh(xx, yy, Z, cmap=cmap_light) # Plot also the training points plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold) plt.xlabel("sepal length (cm)") plt.ylabel("sepal width (cm)") plt.axis("tight") .. image-sg:: /packages/scikit-learn/auto_examples/images/sphx_glr_plot_iris_knn_001.png :alt: plot iris knn :srcset: /packages/scikit-learn/auto_examples/images/sphx_glr_plot_iris_knn_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none (np.float64(4.180808080808081), np.float64(8.019191919191918), np.float64(1.8868686868686868), np.float64(4.513131313131313)) .. GENERATED FROM PYTHON SOURCE LINES 46-47 And now, redo the analysis with 3 neighbors .. GENERATED FROM PYTHON SOURCE LINES 47-64 .. code-block:: Python knn = neighbors.KNeighborsClassifier(n_neighbors=3) knn.fit(X, y) Z = knn.predict(np.c_[xx.ravel(), yy.ravel()]) # Put the result into a color plot Z = Z.reshape(xx.shape) plt.figure() plt.pcolormesh(xx, yy, Z, cmap=cmap_light) # Plot also the training points plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold) plt.xlabel("sepal length (cm)") plt.ylabel("sepal width (cm)") plt.axis("tight") plt.show() .. image-sg:: /packages/scikit-learn/auto_examples/images/sphx_glr_plot_iris_knn_002.png :alt: plot iris knn :srcset: /packages/scikit-learn/auto_examples/images/sphx_glr_plot_iris_knn_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.503 seconds) .. _sphx_glr_download_packages_scikit-learn_auto_examples_plot_iris_knn.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_iris_knn.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_iris_knn.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_iris_knn.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_