gpt4 book ai didi

python - 在 Python 中聚类不同长度的直方图

转载 作者:行者123 更新时间:2023-12-04 15:44:18 25 4
gpt4 key购买 nike

如何使用相同数量的 bin (8),但长度不同,对大型直方图数据集进行聚类?具体来说,我想对它们的密度分布进行聚类。我想我可以用 kmeans 或层次聚类来做到这一点,但似乎长度是个问题,或者我的设置导致 ValueError: setting an array element with a sequence.

hist_data[:, 1]
array([
array([3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6,
6, 6, 6, 6, 6, 6, 5, 4, 3, 3, 3, 3, 3, 3, 4, 5, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5,
5], dtype=int64),
...,
array([6, 6, 6, 6, 6, 6, 6, 5, 5, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 4, 4, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 5, 5, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 5, 5, 4, 4, 3, 3, 3, 3, 4, 5, 6, 6, 6, 6, 6,
6, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5], dtype=int64)], dtype=object)

最佳答案

这是否符合要求:

import numpy as np
from sklearn.cluster import KMeans

data = hist_data[:, 1]
data = np.array([np.bincount(datum, minlength=9) for datum in data])
km = KMeans(n_clusters = 10, init="k-means++").fit(data) # 10 clusters
print(km.cluster_centers_) # cluster centres, 10x8 array

关于python - 在 Python 中聚类不同长度的直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56547619/

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