.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/mlearning/classification_model/plot_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_examples_mlearning_classification_model_plot_knn.py: k-nearest neighbors =================== A :class:`.KNNClassifier` is a k-nearest neighbors model based on `scikit-learn `__. We want to classify the Iris dataset using a KNN classifier. .. GENERATED FROM PYTHON SOURCE LINES 30-42 .. code-block:: Python from __future__ import annotations from numpy import array from gemseo import configure_logger from gemseo import create_benchmark_dataset from gemseo.mlearning import create_classification_model configure_logger() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 43-45 Load Iris dataset ----------------- .. GENERATED FROM PYTHON SOURCE LINES 45-47 .. code-block:: Python iris = create_benchmark_dataset("IrisDataset", as_io=True) .. GENERATED FROM PYTHON SOURCE LINES 48-52 Create the classification model ------------------------------- Then, we build the k-NN classification model from the discipline cache and displays this model. .. GENERATED FROM PYTHON SOURCE LINES 52-56 .. code-block:: Python model = create_classification_model("KNNClassifier", data=iris) model.learn() model .. raw:: html
KNNClassifier(input_names=(), n_neighbors=5, output_names=(), parameters={}, transformer={'inputs': <gemseo.mlearning.transformers.scaler.min_max_scaler.MinMaxScaler object at 0x704937e17c50>})
  • based on the scikit-learn library
  • built from 150 learning samples


.. GENERATED FROM PYTHON SOURCE LINES 57-60 Predict output -------------- Once it is built, we can use it for prediction. .. GENERATED FROM PYTHON SOURCE LINES 60-68 .. code-block:: Python input_value = { "sepal_length": array([4.5]), "sepal_width": array([3.0]), "petal_length": array([1.0]), "petal_width": array([0.2]), } output_value = model.predict(input_value) output_value .. rst-class:: sphx-glr-script-out .. code-block:: none {'specy': array([0])} .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.026 seconds) .. _sphx_glr_download_examples_mlearning_classification_model_plot_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_knn.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_knn.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_knn.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_