.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/mlearning/clustering_model/plot_gaussian_mixture.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_clustering_model_plot_gaussian_mixture.py: Gaussian Mixtures ================= Load Iris dataset and create clusters. .. GENERATED FROM PYTHON SOURCE LINES 28-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.datasets.dataset import Dataset from gemseo.mlearning import create_clustering_model from gemseo.post.dataset.scatter_plot_matrix import ScatterMatrix configure_logger() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 43-46 Create dataset -------------- We import the Iris benchmark dataset through the API. .. GENERATED FROM PYTHON SOURCE LINES 46-48 .. code-block:: Python iris = create_benchmark_dataset("IrisDataset") .. GENERATED FROM PYTHON SOURCE LINES 49-50 Extract inputs as a new dataset .. GENERATED FROM PYTHON SOURCE LINES 50-54 .. code-block:: Python data = iris.get_view(group_names=iris.PARAMETER_GROUP).to_numpy() variables = iris.get_variable_names(iris.PARAMETER_GROUP) variables .. rst-class:: sphx-glr-script-out .. code-block:: none ['petal_length', 'petal_width', 'sepal_length', 'sepal_width'] .. GENERATED FROM PYTHON SOURCE LINES 55-57 .. code-block:: Python dataset = Dataset.from_array(data, variables) .. GENERATED FROM PYTHON SOURCE LINES 58-62 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 62-66 .. code-block:: Python model = create_clustering_model("GaussianMixture", data=dataset, n_components=3) model.learn() model .. raw:: html
GaussianMixture(n_components=3, random_state=0, var_names=None)
  • based on the scikit-learn library
  • built from 150 learning samples


.. GENERATED FROM PYTHON SOURCE LINES 67-70 Predict output -------------- Once it is built, we can use it for prediction. .. GENERATED FROM PYTHON SOURCE LINES 70-79 .. 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 2 .. GENERATED FROM PYTHON SOURCE LINES 80-83 Plot clusters ------------- Show cluster labels .. GENERATED FROM PYTHON SOURCE LINES 83-85 .. code-block:: Python dataset.add_variable("gm_specy", model.labels.reshape((-1, 1)), "labels") ScatterMatrix(dataset, kde=True, classifier="gm_specy").execute(save=False, show=True) .. image-sg:: /examples/mlearning/clustering_model/images/sphx_glr_plot_gaussian_mixture_001.png :alt: plot gaussian mixture :srcset: /examples/mlearning/clustering_model/images/sphx_glr_plot_gaussian_mixture_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [
] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.647 seconds) .. _sphx_glr_download_examples_mlearning_clustering_model_plot_gaussian_mixture.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_gaussian_mixture.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_gaussian_mixture.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_