python 中的 kmeans 聚类将数据垂直而不是水平分组

问题描述 投票:0回答:1

我有一个这样的数据集:

coupled_series = [(9.752, 0.0005), (9.9792, 0.0008), (9.8571, 0.0036), (10.5017, 0.0038), (10.4808, 0.0038), (10.6975, 0.003), (12.1378, 0.0008), (12.7328, 0.0005), (14.0357, 0.0035), (11.7431, 0.0039), (10.107, 0.0039), (10.4207, 0.0039), (10.563, 0.003), (11.0856, 0.0009), (11.3304, 0.0005), (11.87, 0.0035), (12.9338, 0.0039), (13.243, 0.0039), (13.4354, 0.0038), (13.14, 0.003), (13.4611, 0.0008), (13.1459, 0.0004), (11.956, 0.0035), (12.4869, 0.0039), (13.2369, 0.004), (13.6368, 0.0039), (14.11, 0.0029), (14.1441, 0.0007), (13.8937, 0.0004), (13.4262, 0.0007)]

我喜欢使用以下代码使用 sklearn 运行 kmeans 集群:

kmeans = KMeans(n_clusters=2, max_iter=50, n_init="auto", random_state=0, algorithm='lloyd')
kmeans.fit(coupled_series)


x=list(zip(*coupled_series))[0]
y=list(zip(*coupled_series))[1]

plt.scatter(x, y, c=kmeans.labels_)
plt.show()

结果如下图:

正如您所看到的,它已将组聚集到左侧和右侧,而通过查看它可以看出它由顶部和底部的两条线组成。也就是说,我希望它们聚集成红色和蓝色两组,如下图所示。

我能做些什么来解决这个问题,以我喜欢的方式进行集群吗?我可以尝试其他类型的聚类吗?只是提一下,我也需要对其他数据组重复这一点,它们大多看起来像这样。谢谢。

python machine-learning scikit-learn cluster-analysis k-means
1个回答
0
投票

您需要对数据进行一些标准化/缩放,例如

MinMaxScaler
。例如,如果您这样做:

from sklearn.preprocessing import MinMaxScaler

# apply minmax scaling to each axes to "normalise" things
scaled = MinMaxScaler().fit_transform(coupled_series)

kmeans = KMeans(n_clusters=2, max_iter=50, n_init="auto", random_state=0, algorithm='lloyd')
kmeans.fit(scaled)

那么你应该得到如下图:

© www.soinside.com 2019 - 2024. All rights reserved.