gpt4 book ai didi

python - 在Python中将向量的值排列到列表中

转载 作者:太空宇宙 更新时间:2023-11-03 17:48:01 27 4
gpt4 key购买 nike

我正在对我的数据运行 k-means 算法,标签的输出如下所示:

[0 5 8 6 1 3 3 2 2 5 5 6 1 1 3 3 1 8 8 3 3 1 1 1 1 5 2 5 1 1 7 3 6 4 3 3 8
1 3 3 5 1 8 8 1 8 7 1 1 8 6]

该向量包含点索引的簇号,例如第一个值是簇号。 0 表示点索引 0,向量的第二个值表示它的簇号。 5,点索引1属于它。

我想要集群的子集:像:

cluster no 0 = { its index numbers}
cluster no 1 = { its index numbers}
..
cluster no 8 = { its index numbers}

例如向量的第一个值为5,我需要列出该向量的值为5的所有索引,反之亦然。我希望每个值都有自己的索引列表。

所以值 5 的列表应该是:

簇 5 = [ 1,9,10,25,27....

以及其他值的所有输出,最终输出应该是8个列表。

最佳答案

如果你愿意使用 numpy,这可以通过 numpy.where 轻松完成

cluster5, = numpy.where( array == 5 )

在“纯”Python 中你可以这样做:

cluster5 = [i for i in range(len(array)) if array[i]==5]

关于python - 在Python中将向量的值排列到列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29501786/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com