OA-STREAMS-TASKS - Cluster Analysis

../../../../../../../../_images/MLPro-OA-Cluster_Analyzers_class_diagram.drawio.png

Ver. 0.4.0 (2023-06-03)

This module provides templates for cluster analysis to be used in the context of online adaptivity.

class mlpro.oa.streams.tasks.clusteranalyzers.Cluster(p_id=None, p_visualize: bool = False, **p_kwargs)

Bases: Id, Plottable

Base class for a cluster.

Parameters:
  • p_id – Optional external id.

  • p_visualize (bool) – Boolean switch for visualisation. Default = False.

  • **p_kwargs – Further optional keyword arguments.

C_PLOT_ACTIVE: bool = True
C_PLOT_STANDALONE: bool = False
C_PLOT_VALID_VIEWS: list = ['2D', '3D', 'ND']
C_PLOT_DEFAULT_VIEW: str = 'ND'
get_membership(p_inst: Instance) float

Custom method to compute a scalar membership value for the given instance.

Parameters:

p_inst (Instance) – Instance.

Returns:

Scalar value >= 0 that determines the membership of the given instance to this cluster. A value 0 means that the given instance is not a member of the cluster.

Return type:

float

class mlpro.oa.streams.tasks.clusteranalyzers.ClusterAnalyzer(p_cls_cluster, p_cluster_limit: int = 0, p_name: str = None, p_range_max=1, p_ada: bool = True, p_duplicate_data: bool = False, p_visualize: bool = False, p_logging=True, **p_kwargs)

Bases: OATask

Base class for online cluster analysis. It raises an event when a cluster was added or removed.

Parameters:
  • p_cls_cluster – Cluster class (Class Cluster or a child class).

  • p_cluster_limit (int) – Optional limit for clusters to be created. Default = 0 (no limit).

  • p_name (str) – Optional name of the task. Default is None.

  • p_range_max (int) – Maximum range of asynchonicity. See class Range. Default is Range.C_RANGE_PROCESS.

  • p_ada (bool) – Boolean switch for adaptivitiy. Default = True.

  • p_duplicate_data (bool) – If True, instances will be duplicated before processing. Default = False.

  • p_visualize (bool) – Boolean switch for visualisation. Default = False.

  • p_logging – Log level (see constants of class Log). Default: Log.C_LOG_ALL

  • p_kwargs (dict) – Further optional named parameters.

C_MS_SCOPE_ALL

Membership scope, that includes all clusters

Type:

int = 0

C_MS_SCOPE_NONZERO

Membership scope, that includes just clusters with membership values > 0

Type:

int = 1

C_MS_SCOPE_MAX

Membership scope, that includes just the cluster with the highest membership value.

Type:

int = 2

C_TYPE = 'Cluster Analyzer'
C_EVENT_CLUSTER_ADDED = 'CLUSTER_ADDED'
C_EVENT_CLUSTER_REMOVED = 'CLUSTER_REMOVED'
C_PLOT_ACTIVE: bool = True
C_PLOT_STANDALONE: bool = False
C_MS_SCOPE_ALL: int = 0
C_MS_SCOPE_NONZERO: int = 1
C_MS_SCOPE_MAX: int = 2
_run(p_inst_new: List[Instance], p_inst_del: List[Instance])

Custom method that is called by method run().

Parameters:
  • p_inst_new (set) – Set of new stream instances to be processed.

  • p_inst_del (set) – Set of obsolete stream instances to be removed.

new_cluster_allowed() bool

Determines whether adding a new cluster is allowed.

Returns:

True, if adding a new cluster allowed. False otherwise.

Return type:

bool

get_clusters() List[Cluster]

This method returns the current list of clusters.

Returns:

list_of_clusters – Current list of clusters.

Return type:

List[Cluster]

get_cluster_memberships(p_inst: Instance, p_scope: int = 2) List[Tuple[str, float, Cluster]]

Method to determine the membership of the given instance to each cluster as a value in percent.

Parameters:
  • p_inst (Instance) – Instance to be evaluated.

  • p_scope (int) – Scope of the result list. See class attributes C_MS_SCOPE_* for possible values. Default value is C_MS_SCOPE_MAX.

Returns:

membership – List of membership tuples. A tuple consists of a cluster id, a relative membership value in [0,1] and a reference to the cluster object.

Return type:

List[Tuple[str, float, Cluster]]

update_plot(p_inst_new: List[Instance] = None, p_inst_del: List[Instance] = None, **p_kwargs)

Specialized definition of method update_plot() of class mlpro.bf.plot.Plottable.

Parameters:
  • p_inst_new (List[Instance]) – List of new stream instances to be plotted.

  • p_inst_del (List[Instance]) – List of obsolete stream instances to be removed.

  • p_kwargs (dict) – Further optional plot parameters.

class mlpro.oa.streams.tasks.clusteranalyzers.ClusterCentroid(p_id=None, p_visualize: bool = False, p_cls_centroid=<class 'mlpro.bf.math.geometry.Point'>, **p_kwargs)

Bases: Cluster

Extended cluster class with a centroid.

Parameters:
  • p_id – Optional external id

  • p_visualize (bool) – Boolean switch for visualisation. Default = False.

  • Point (p_cls_centroid =) – Name of a point class. Default = Point

  • **p_kwargs – Further optional keyword arguments.

get_centroid() Point
get_membership(p_inst: Instance) float

Custom method to compute a scalar membership value for the given instance.

Parameters:

p_inst (Instance) – Instance.

Returns:

Scalar value >= 0 that determines the membership of the given instance to this cluster. A value 0 means that the given instance is not a member of the cluster.

Return type:

float