问题描述
我有一个 shape=(3, 60000, 10) 的 3-d 数组,它需要是 2-D 以便在聚类时能够对其进行可视化.
I have a 3-d array of shape=(3, 60000, 10) which needs to be 2-D so as to be able to visualize it when clustering.
我计划实现从 scikit-learn 到 3-d 数组的 k-means 聚类,并读到它只需要 2-D 形状,我只是想要一些关于是否有正确方法的建议它 ?我正计划制作 (60000,30) ,但在我继续之前想要澄清.
I was planning on implementing the k-means clustering from scikit-learn to the 3-d array and read that it only takes in 2-D shape , I just wanted some advice as to whether there is a right way to do it ? I was planning on making it (60000,30) , but wanted a clarification before I go ahead.
推荐答案
我的理解是你有 10 个特征,每个特征都由 3d 数据组成.您是否打算对所有 10 个功能进行聚类?如果是这样,请重塑它,使您拥有 600000 x 3 个点(假设您想在空间中分开).比如这个
How I read it is that you have 10 features each consisting of 3d data. Do you intend to cluster all 10 features? If so reshape it such that you have 600000 x 3 points (assuming you want to separate in space). For example this
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt, numpy as np
# 3x points
data = np.random.rand(100, 3, 10) + np.arange(10) # add arbitrary offset for "difference" in real data
data = np.moveaxis(data, -1, 1).reshape(-1, 3)
n_clus = 10 # cluster in 10 --> fill in with your goal in mind
km = KMeans(n_clusters = n_clus).fit(data)
fig, ax = plt.subplots(subplot_kw = dict(projection = '3d'))
colors = plt.cm.tab20(np.linspace(0, 1, n_clus))
ax.scatter(*data.T, c = colors[km.labels_])
fig.show()
产量
这篇关于有没有一种特殊的方法可以将 3-d 数组转换为 2-d 数组以进行聚类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!