繁体   English   中英

查找循环数据集群的最小值和最大值

[英]Finding the minimum and maximum value of a cluster for cyclic data

考虑到群集超出了值范围的限制,如何确定周期性数据的群集的最小值和最大值(此处为0到24)?

查看蓝色群集,我想确定值22和2作为群集的边界。 哪种算法可以解决此问题?

聚集的循环数据

我找到了解决问题的方法。 假设数据采用以下格式:

#!/usr/bin/env python3

import numpy as np

data = np.array([0, 1, 2, 12, 13, 14, 15, 21, 22, 23])
labels = np.array([0, 0, 0, 1, 1, 1, 1, 0, 0, 0])
bounds = get_cluster_bounds(data, labels)
print(bounds) # {0: array([21,  2]), 1: array([12, 15])}

您可以在此处找到该功能:

#!/usr/bin/env python3

import numpy as np


def get_cluster_bounds(data: np.ndarray, labels: np.ndarray) -> dict:
    """
    There are five ways in which the points of the cluster can be cyclically
    considered. The points to be determined are marked with an arrow.

    In the first case, the cluster data is distributed beyond the edge of
    the cycle:
         ↓B           ↓A
    |#####____________#####|

    In the second case, the data lies exactly at the beginning of the value
    range, but without exceeding it.
    ↓A        ↓B
    |##########____________|

    In the third case, the data lies exactly at the end of the value
    range, but without exceeding it.
                 ↓A       ↓B
    |____________##########|

    In the fourth, the data lies within the value range
    without touching a border.
            ↓A       ↓B
    |_______##########_____|

    In the fifth and simplest case, the data lies in the entire area without
    another label existing.
     ↓A                   ↓B
    |######################|

    Args:
        data:      (n, 1) numpy array containing all data points.
        labels:    (n, 1) numpy array containing all data labels.

    Returns:
        bounds:   A dictionary whose key is the index of the cluster and
                  whose value specifies the start and end point of the
                  cluster.
    """

    # Sort the data in ascending order.
    shuffle = data.argsort()
    data = data[shuffle]
    labels = labels[shuffle]

    # Get the number of unique clusters.
    labels_unique = np.unique(labels)
    num_clusters = labels_unique.size

    bounds = {}

    for c_index in range(num_clusters):
        mask = labels == c_index
        # Case 1 or 5
        if mask[0] and mask[-1]:
            # Case 5
            if np.all(mask):
                start = data[0]
                end = data[-1]
            # Case 1
            else:
                edges = np.where(np.invert(mask))[0]
                start = data[edges[-1] + 1]
                end = data[edges[0] - 1]

        # Case 2
        elif mask[0] and not mask[-1]:
            edges = np.where(np.invert(mask))[0]
            start = data[0]
            end = data[edges[0] - 1]

        # Case 3
        elif not mask[0] and mask[-1]:
            edges = np.where(np.invert(mask))[0]
            start = data[edges[-1] + 1]
            end = data[-1]

        # Case 4
        elif not mask[0] and not mask[-1]:
            edges = np.where(mask)[0]
            start = data[edges[0]]
            end = data[edges[-1]]

        else:
            raise ValueError('This should not happen.')

        bounds[c_index] = np.array([start, end])

    return bounds

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM