API#

Here are some examples of the machine learning API applied to clustering models.

Import#

from __future__ import annotations

from gemseo import configure_logger
from gemseo import create_benchmark_dataset
from gemseo.mlearning import create_clustering_model
from gemseo.mlearning import get_clustering_models
from gemseo.mlearning import get_clustering_options

configure_logger()
<RootLogger root (INFO)>

Get available clustering models#

get_clustering_models()
['GaussianMixture', 'KMeans']

Get clustering model options#

get_clustering_options("GaussianMixture")
+---------------------------+--------------------------------------------------------------------------------------------+---------------------------+
|            Name           |                                        Description                                         |            Type           |
+---------------------------+--------------------------------------------------------------------------------------------+---------------------------+
|         n_clusters        |                    The number of clusters of the clustering algorithm.                     |          integer          |
|         parameters        |                                     Other parameters.                                      |           object          |
|        random_state       |    The random state parameter.  if ``none``, use the global random state instance from     |            None           |
|                           |   ``numpy.random``. creating the model multiple times will produce different results. if   |                           |
|                           |  ``int``, use a new random number generator seeded by this integer. this will produce the  |                           |
|                           |                                       same results.                                        |                           |
|        transformer        |          The strategies to transform the variables.  the values are instances of           |           object          |
|                           |   :class:`.basetransformer` while the keys are the names of either the variables or the    |                           |
|                           |   groups of variables, e.g. ``"inputs"`` or ``"outputs"`` in the case of the regression    |                           |
|                           | algorithms. if a group is specified, the :class:`.basetransformer` will be applied to all  |                           |
|                           |     the variables of this group. if :attr:`.identity`, do not transform the variables.     |                           |
|         var_names         |                                The names of the variables.                                 |           array           |
+---------------------------+--------------------------------------------------------------------------------------------+---------------------------+
    INFO - 08:37:30: +---------------------------+--------------------------------------------------------------------------------------------+---------------------------+
    INFO - 08:37:30: |            Name           |                                        Description                                         |            Type           |
    INFO - 08:37:30: +---------------------------+--------------------------------------------------------------------------------------------+---------------------------+
    INFO - 08:37:30: |         n_clusters        |                    The number of clusters of the clustering algorithm.                     |          integer          |
    INFO - 08:37:30: |         parameters        |                                     Other parameters.                                      |           object          |
    INFO - 08:37:30: |        random_state       |    The random state parameter.  if ``none``, use the global random state instance from     |            None           |
    INFO - 08:37:30: |                           |   ``numpy.random``. creating the model multiple times will produce different results. if   |                           |
    INFO - 08:37:30: |                           |  ``int``, use a new random number generator seeded by this integer. this will produce the  |                           |
    INFO - 08:37:30: |                           |                                       same results.                                        |                           |
    INFO - 08:37:30: |        transformer        |          The strategies to transform the variables.  the values are instances of           |           object          |
    INFO - 08:37:30: |                           |   :class:`.basetransformer` while the keys are the names of either the variables or the    |                           |
    INFO - 08:37:30: |                           |   groups of variables, e.g. ``"inputs"`` or ``"outputs"`` in the case of the regression    |                           |
    INFO - 08:37:30: |                           | algorithms. if a group is specified, the :class:`.basetransformer` will be applied to all  |                           |
    INFO - 08:37:30: |                           |     the variables of this group. if :attr:`.identity`, do not transform the variables.     |                           |
    INFO - 08:37:30: |         var_names         |                                The names of the variables.                                 |           array           |
    INFO - 08:37:30: +---------------------------+--------------------------------------------------------------------------------------------+---------------------------+

{'additionalProperties': False, 'description': 'The settings of the Gaussian mixture model.', 'properties': {'transformer': {'description': 'The strategies to transform the variables.\n\nThe values are instances of :class:`.BaseTransformer`\nwhile the keys are the names of\neither the variables\nor the groups of variables,\ne.g. ``"inputs"`` or ``"outputs"``\nin the case of the regression algorithms.\nIf a group is specified,\nthe :class:`.BaseTransformer` will be applied\nto all the variables of this group.\nIf :attr:`.IDENTITY`, do not transform the variables.', 'title': 'Transformer', 'type': 'object'}, 'parameters': {'description': 'Other parameters.', 'title': 'Parameters', 'type': 'object'}, 'var_names': {'default': [], 'description': 'The names of the variables.', 'items': {'type': 'string'}, 'title': 'Var Names', 'type': 'array'}, 'n_clusters': {'default': 5, 'description': 'The number of clusters of the clustering algorithm.', 'exclusiveMinimum': 0, 'title': 'N Clusters', 'type': 'integer'}, 'random_state': {'anyOf': [{'minimum': 0, 'type': 'integer'}, {'type': 'null'}], 'default': 0, 'description': 'The random state parameter.\n\nIf ``None``, use the global random state instance from ``numpy.random``.\nCreating the model multiple times will produce different results.\nIf ``int``, use a new random number generator seeded by this integer.\nThis will produce the same results.', 'title': 'Random State'}}, 'title': 'GaussianMixture_Settings', 'type': 'object'}

Create clustering model#

iris = create_benchmark_dataset("IrisDataset")

model = create_clustering_model("KMeans", data=iris, n_clusters=3)
model.learn()
model
KMeans(n_clusters=3, parameters={}, random_state=0, transformer={}, var_names=())
  • based on the scikit-learn library
  • built from 150 learning samples


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

Gallery generated by Sphinx-Gallery