我想运行具有五个功能的Kmeans聚类算法。 (K = 4)但是,我得到一个索引错误说:
> Traceback (most recent call last):
> File
> "C:\....py", line 756,
> in <module>
> plt.plot(X[i][0],X[i][1],colors[labels[i]],markersize=10)
> IndexError: list index out of range
这是一个产生错误的代码段:
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
kmeans = KMeans(n_clusters=4)
kmeans.fit(X)
centroid = kmeans.cluster_centers_
labels = kmeans.labels_
fig = plt.figure(figsize=(9,7), dpi=100)
colors = ["r.","b.","y."]
df_clustering = []
for i in range(len(X)):
print ("ID:", df_features['id'].loc[[i]].values[0], "coordinate:" , X[i], "label:", labels[i])
df_clustering.append((df_features['id'].loc[[i]].values[0], labels[i]))
# below line is generating an error
plt.plot(X[i][0],X[i][1],colors[labels[i]],markersize=10)
答案 0 :(得分:0)
您正在使用4个群集,但colors
的长度为3.因此,对于分配给群集4的任何数据点(即labels[i] == 3
),都会有IndexError