Python/K-means 聚类:具有随机颜色的颜色质心
Python/K-means Clustering: Color centroids with random colors
我根据从 .txt 文件导入的数据在 Python 中构建了 Kmeans 聚类。我已经生成了 100 个质心,并且用 Matplotlib 绘制了两个图形来显示这些质心:一个图形包含点云(源自 .txt 文件),代表数据 before 聚类,另一个图形包含黑色星星以标记每个质心。
我应该怎么做才能用随机选择的特定颜色绘制每个质心,而不是黑色星星?这意味着每个质心点组将采用不同的颜色。
代码:
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn.cluster import KMeans
#building the 22797x3 array:
#loading the first array from .txt file, 22797x400 long.
array = np.loadtxt('C:\Scripts/final_array_2.txt', usecols=range(400))
array = np.float32(array)
#plotting data before the clustering:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(result[:, 0], result[:, 1], result[:, 2], alpha = 0.1)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
# Initializing KMeans, plotting clusters
kmeans = KMeans(n_clusters=100)
# Fitting with inputs
kmeans = kmeans.fit(result)
# Predicting the clusters
labels = kmeans.predict(result)
# Getting the cluster centers
C = kmeans.cluster_centers_
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(result[:, 0], result[:, 1], result[:, 2])
ax.scatter(C[:, 0], C[:, 1], C[:, 2], marker='*', c='#050505', s=1000)
plt.show()
上面代码的结果
聚类前的数据:
https://i.stack.imgur.com/IXa7R.png
黑色星团后的数据:https://i.stack.imgur.com/1u4JY.png
我需要得到什么(类似的例子,不是同一个点云):
https://i.stack.imgur.com/K5oDT.png
在这种情况下,它将是 100 种颜色,而不仅仅是三种。
有什么帮助吗?
问题是 kmeans.cluster_centers_
returns 每个找到的簇的中心。您需要的是通过标签更改每个数据点的颜色。以 iris 数据集为例
from sklearn import datasets
from sklearn.cluster import KMeans
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
iris = datasets.load_iris()
data = iris.data[:,0:3]
x=data[:,0]
y=data[:,1]
z=data[:,2]
kmeans = KMeans(n_clusters=5)
kmeans = kmeans.fit(data)
labels = kmeans.predict(data)
fig=plt.figure()
ax = fig.add_subplot(111, projection='3d')
ColorsA=plt.cm.viridis(np.linspace(0, 1,5),alpha=0.8) #Equally spaced color
for i in range(5): #Labels of the clusters
xL=[]
yL=[]
zL=[]
for k in range(len(x)):
if labels[k]==i: #Data points of each cluster
xL.append(x[k])
yL.append(y[k])
zL.append(z[k])
ax.scatter(xL,yL,zL,c=ColorsA[i])
希望对您有所帮助
我根据从 .txt 文件导入的数据在 Python 中构建了 Kmeans 聚类。我已经生成了 100 个质心,并且用 Matplotlib 绘制了两个图形来显示这些质心:一个图形包含点云(源自 .txt 文件),代表数据 before 聚类,另一个图形包含黑色星星以标记每个质心。
我应该怎么做才能用随机选择的特定颜色绘制每个质心,而不是黑色星星?这意味着每个质心点组将采用不同的颜色。
代码:
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn.cluster import KMeans
#building the 22797x3 array:
#loading the first array from .txt file, 22797x400 long.
array = np.loadtxt('C:\Scripts/final_array_2.txt', usecols=range(400))
array = np.float32(array)
#plotting data before the clustering:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(result[:, 0], result[:, 1], result[:, 2], alpha = 0.1)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
# Initializing KMeans, plotting clusters
kmeans = KMeans(n_clusters=100)
# Fitting with inputs
kmeans = kmeans.fit(result)
# Predicting the clusters
labels = kmeans.predict(result)
# Getting the cluster centers
C = kmeans.cluster_centers_
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(result[:, 0], result[:, 1], result[:, 2])
ax.scatter(C[:, 0], C[:, 1], C[:, 2], marker='*', c='#050505', s=1000)
plt.show()
上面代码的结果
聚类前的数据: https://i.stack.imgur.com/IXa7R.png
黑色星团后的数据:https://i.stack.imgur.com/1u4JY.png
我需要得到什么(类似的例子,不是同一个点云): https://i.stack.imgur.com/K5oDT.png 在这种情况下,它将是 100 种颜色,而不仅仅是三种。
有什么帮助吗?
问题是 kmeans.cluster_centers_
returns 每个找到的簇的中心。您需要的是通过标签更改每个数据点的颜色。以 iris 数据集为例
from sklearn import datasets
from sklearn.cluster import KMeans
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
iris = datasets.load_iris()
data = iris.data[:,0:3]
x=data[:,0]
y=data[:,1]
z=data[:,2]
kmeans = KMeans(n_clusters=5)
kmeans = kmeans.fit(data)
labels = kmeans.predict(data)
fig=plt.figure()
ax = fig.add_subplot(111, projection='3d')
ColorsA=plt.cm.viridis(np.linspace(0, 1,5),alpha=0.8) #Equally spaced color
for i in range(5): #Labels of the clusters
xL=[]
yL=[]
zL=[]
for k in range(len(x)):
if labels[k]==i: #Data points of each cluster
xL.append(x[k])
yL.append(y[k])
zL.append(z[k])
ax.scatter(xL,yL,zL,c=ColorsA[i])
希望对您有所帮助