标准化 PC 的 KMeans 聚类图
Posted
技术标签:
【中文标题】标准化 PC 的 KMeans 聚类图【英文标题】:KMeans clustering plot of standardized PCs 【发布时间】:2019-07-27 21:45:39 【问题描述】:我有一个新数据集,其中包含数据集的第一个标准化 PC 值(PC1 到 PC5)。现在我想使用这个新数据集创建一个包含 3 个集群的 kmeans 图。数据集名称principalDf,如下图:
PC1 PC2 PC3 PC4 PC5
4.220974 -2.270272 0.757259 -1.597269 4.238792
13.464907 -3.685775 -2.142520 -0.889321 -0.217543
5.900341 -2.368060 0.093671 0.484737 0.243810
-1.884293 1.370640 -0.221722 3.304978 0.292733
2.631881 1.782549 0.575880 -2.894564 -0.848573
我尝试使用下面的代码来绘制它:
model = KMeans(n_clusters = 3)
model.fit(principalDf)
#get clusters
clusters = model.predict(principalDf)
print(clusters)
#plot based on cluster
for i in range(len(clusters)):
if clusters[i] == 0:
c1 = plt.scatter(principalDf[i, 0], principalDf[i, 1], c='r', marker='+')
elif clusters[i] == 1:
c2 = plt.scatter(principalDf[i, 0], principalDf[i, 1], c='g', marker='o')
elif clusters[i] == -1:
c3 = plt.scatter(principalDf[i, 0], principalDf[i, 1], c='b', marker='*')
elif clusters[i] == 2:
c3 = plt.scatter(principalDf[i, 0], principalDf[i, 1], c='black', marker='-')
plt.legend([c1, c2, c3], ['Cluster 1', 'Cluster 2', 'Cluster3'])
plt.title('There are 3 cluster in our data but we can only plot in 2 dimensions')
plt.show()
但我不断收到此错误:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-22-ae7667a531f7> in <module>()
3 for i in range(len(clusters)):
4 if clusters[i] == 0:
----> 5 c1 = plt.scatter(principalDf[i, 0], principalDf[i, 1], c='r', marker='+')
6 elif clusters[i] == 1:
7 c2 = plt.scatter(principalDf[i, 0], principalDf[i, 1], c='g', marker='o')
C:\Users\user\Anaconda2\lib\site-packages\pandas\core\frame.pyc in __getitem__(self, key)
2686 return self._getitem_multilevel(key)
2687 else:
-> 2688 return self._getitem_column(key)
2689
2690 def _getitem_column(self, key):
C:\Users\user\Anaconda2\lib\site-packages\pandas\core\frame.pyc in _getitem_column(self, key)
2693 # get column
2694 if self.columns.is_unique:
-> 2695 return self._get_item_cache(key)
2696
2697 # duplicate columns & possible reduce dimensionality
C:\Users\user\Anaconda2\lib\site-packages\pandas\core\generic.pyc in _get_item_cache(self, item)
2487 res = cache.get(item)
2488 if res is None:
-> 2489 values = self._data.get(item)
2490 res = self._box_item_values(item, values)
2491 cache[item] = res
C:\Users\user\Anaconda2\lib\site-packages\pandas\core\internals.pyc in get(self, item, fastpath)
4113
4114 if not isna(item):
-> 4115 loc = self.items.get_loc(item)
4116 else:
4117 indexer = np.arange(len(self.items))[isna(self.items)]
C:\Users\user\Anaconda2\lib\site-packages\pandas\core\indexes\base.pyc in get_loc(self, key, method, tolerance)
3078 return self._engine.get_loc(key)
3079 except KeyError:
-> 3080 return self._engine.get_loc(self._maybe_cast_indexer(key))
3081
3082 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: (0, 0)
在我试图绘制集群时,我需要帮助。
【问题讨论】:
【参考方案1】:您不能像那样索引数据帧,您必须使用 .iloc。或者:
plt.scatter(x=principalDF["PC1"], y=principalDF["PC2"], c=clusters)
【讨论】:
【参考方案2】:principalDF 是一个熊猫数据框。您将其视为 numpy 数组。 简单的解决方法是执行 principalDF.values,它为您提供数据帧的 numpy 数组。
【讨论】:
以上是关于标准化 PC 的 KMeans 聚类图的主要内容,如果未能解决你的问题,请参考以下文章