.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/mlearning/clustering_model/plot_kmeans.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_mlearning_clustering_model_plot_kmeans.py: K-means ======= Load Iris dataset and create clusters. .. GENERATED FROM PYTHON SOURCE LINES 31-33 Import ------ .. GENERATED FROM PYTHON SOURCE LINES 33-46 .. code-block:: default from __future__ import absolute_import, division, print_function, unicode_literals from future import standard_library from numpy import array from gemseo.api import configure_logger, load_dataset from gemseo.core.dataset import Dataset from gemseo.mlearning.api import create_clustering_model configure_logger() standard_library.install_aliases() .. GENERATED FROM PYTHON SOURCE LINES 47-50 Create dataset -------------- We import the Iris benchmark dataset through the API. .. GENERATED FROM PYTHON SOURCE LINES 50-60 .. code-block:: default iris = load_dataset("IrisDataset") # Extract inputs as a new dataset data = iris.get_data_by_group(iris.PARAMETER_GROUP) variables = iris.get_names(iris.PARAMETER_GROUP) print(variables) dataset = Dataset("sepal_and_petal") dataset.set_from_array(data, variables) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none ['sepal_length', 'sepal_width', 'petal_length', 'petal_width'] .. GENERATED FROM PYTHON SOURCE LINES 61-65 Create clustering model ----------------------- We know that there are three classes of Iris plants. We will thus try to identify three clusters. .. GENERATED FROM PYTHON SOURCE LINES 65-69 .. code-block:: default model = create_clustering_model("KMeans", data=dataset, n_clusters=3) model.learn() print(model) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none KMeans(var_names=None, n_clusters=3, random_state=0) | built from 150 learning samples .. GENERATED FROM PYTHON SOURCE LINES 70-73 Predict output -------------- Once it is built, we can use it for prediction. .. GENERATED FROM PYTHON SOURCE LINES 73-82 .. code-block:: default 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) print(output_value) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 2 .. GENERATED FROM PYTHON SOURCE LINES 83-86 Plot clusters ------------- Show cluster labels .. GENERATED FROM PYTHON SOURCE LINES 86-90 .. code-block:: default dataset.add_variable( "km_specy", model.labels.reshape((-1, 1)), group="labels", cache_as_input=False ) dataset.plot("ScatterMatrix", kde=True, classifier="km_specy") .. image:: /examples/mlearning/clustering_model/images/sphx_glr_plot_kmeans_001.png :alt: plot kmeans :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.773 seconds) .. _sphx_glr_download_examples_mlearning_clustering_model_plot_kmeans.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_kmeans.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_kmeans.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_