在Python中使用数字之间的相对关系对数字进行分组/聚类

在Python中使用数字之间的相对关系对数字进行分组/聚类,python,numbers,Python,Numbers,我需要类似的东西,而不是问,但更多的是相反的: 给出一个数字列表(上面问题中的示例):[1、6、9、100、102、105、109、134、139]我不想说最大差距(在该示例中为10),而是想说我想要多少组 e、 g。 3组:1,6,9/100,102,105,109/134,139 2组:1,6,9/100,102,105,109,134,139 这应该相对有效,因为我的数字非常非常不同: [0.1, 0.2, 1, 4, 100, 110] => 3组的结果分别为0.1,0.2/1,4/1

我需要类似的东西,而不是问,但更多的是相反的:

给出一个数字列表(上面问题中的示例):[1、6、9、100、102、105、109、134、139]我不想说最大差距(在该示例中为10),而是想说我想要多少组

e、 g。 3组:1,6,9/100,102,105,109/134,139 2组:1,6,9/100,102,105,109,134,139

这应该相对有效,因为我的数字非常非常不同: [0.1, 0.2, 1, 4, 100, 110] => 3组的结果分别为0.1,0.2/1,4/100,110

虽然0.2和1在绝对值上比1和5更接近(0.8对3),但相对于0.2,它离1(5倍)远比离0.1(2倍)远

我希望它以某种方式弄清楚我想要实现什么

import sys

# Sorted numbers.
xs = [0.1, 0.2, 1, 4, 100, 110]
xs.sort()

# Reverse-sorted (RATIO, INDEX) tuples.
tups = sorted(
    (
        (xs[i] / xs[i - 1] if i else 0, i)
        for i, x in enumerate(xs)
    ),
    reverse = True,
)

# Indexes of the boundaries having the largest ratios.
n_groups = int(sys.argv[1])
boundaries = sorted(tup[1] for tup in tups[0 : n_groups - 1])
boundaries.append(None)

# Create the groups, based on those boundaries.
groups = []
i = 0
for j in boundaries:
    groups.append(xs[i:j])
    i = j

# Check.
for g in groups:
    print(g)
示例输出:

# n_groups = 1
[0.1, 0.2, 1, 4, 100, 110]

# n_groups = 2
[0.1, 0.2, 1, 4]
[100, 110]

# n_groups = 3
[0.1, 0.2]
[1, 4]
[100, 110]

# n_groups = 4
[0.1, 0.2]
[1]
[4]
[100, 110]

# n_groups = 5
[0.1]
[0.2]
[1]
[4]
[100, 110]
示例输出:

# n_groups = 1
[0.1, 0.2, 1, 4, 100, 110]

# n_groups = 2
[0.1, 0.2, 1, 4]
[100, 110]

# n_groups = 3
[0.1, 0.2]
[1, 4]
[100, 110]

# n_groups = 4
[0.1, 0.2]
[1]
[4]
[100, 110]

# n_groups = 5
[0.1]
[0.2]
[1]
[4]
[100, 110]

如果数字列表不是巨大的,我会首先计算一个数字与其前一个数字之间的每个比率,然后选择最大的分割

例如:

def split_numbers_list(numbers_list, n_groups):
    # If the number of groups is 1 or less, the whole list the only group
    if n_groups <= 1:
        return [numbers_list]
    n_splits = min(len(numbers_list), n_groups) # Can't have more groups than elements
    # Now we calculate the ratios and store them in (index, ratio) tuples
    ratios = [
        (i, numbers_list[i + 1] / numbers_list[i]) for i in range(len(numbers_list) - 1)
    ]
    sorted_ratios = sorted(ratios, key=lambda r: r[1], reverse=True)
    # `chosen_splits` stores the boundaries of each group
    chosen_splits = (
        [0]
        + sorted([r[0] + 1 for r in sorted_ratios[: n_splits - 1]])
        + [len(numbers_list)]
    )
    return [
        numbers_list[chosen_splits[i] : chosen_splits[i + 1]]
        for i in range(len(chosen_splits) - 1)
    ]

如果数字列表不是巨大的,我会首先计算一个数字与其前一个数字之间的每个比率,然后选择最大的分割

例如:

def split_numbers_list(numbers_list, n_groups):
    # If the number of groups is 1 or less, the whole list the only group
    if n_groups <= 1:
        return [numbers_list]
    n_splits = min(len(numbers_list), n_groups) # Can't have more groups than elements
    # Now we calculate the ratios and store them in (index, ratio) tuples
    ratios = [
        (i, numbers_list[i + 1] / numbers_list[i]) for i in range(len(numbers_list) - 1)
    ]
    sorted_ratios = sorted(ratios, key=lambda r: r[1], reverse=True)
    # `chosen_splits` stores the boundaries of each group
    chosen_splits = (
        [0]
        + sorted([r[0] + 1 for r in sorted_ratios[: n_splits - 1]])
        + [len(numbers_list)]
    )
    return [
        numbers_list[chosen_splits[i] : chosen_splits[i + 1]]
        for i in range(len(chosen_splits) - 1)
    ]

你已经试过什么了?最好有一个起点。找出最大的差距2。在最大间隙3处划分间隙最大的组。当没有足够的组时,一些简单的聚类算法怎么样?你试过了吗?如果你的分组标准是连续数字的比率,那么你的第一个数据样本将被分成如下3组:
[[1]、[6,9]、[100102105109134139]
。如果你想得到你在问题中给出的结果,你必须使用不同的标准-请为我们明确说明!你已经试过什么了?最好有一个起点。找出最大的差距2。在最大间隙3处划分间隙最大的组。当没有足够的组时,一些简单的聚类算法怎么样?你试过了吗?如果你的分组标准是连续数字的比率,那么你的第一个数据样本将被分成如下3组:
[[1]、[6,9]、[100102105109134139]
。如果你想得到你在问题中给出的结果,你必须使用不同的标准-请为我们明确说明!