- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个新数据集,其中包含数据集的第一个标准化 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)
我需要这方面的帮助,因为我正在尝试绘制集群。
最佳答案
你不能像那样索引数据帧,你必须使用.iloc。或者:
plt.scatter(x=principalDF["PC1"], y=principalDF["PC2"], c=集群)
关于python-3.x - 标准化 PC 的 KMeans 聚类图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55019516/
某人在 Silverlight posted MVVM 目前缺乏标准化,所以每个人都有自己的风格.. 这就是为什么我和 WPF Disciples 的一些人正在积极讨论每个人都同意的 MVVM 元素。
我有以下数据集: dat tests2 * 20 Score 1 13.333333 2 10.666667 3 2.666667 4 9.333333 5 13.3333
我有一个 double 组,表示某些事件发生的概率,[25,25,25,10,15] 表示事件 A,B..E。这些数字加起来是 100。 通过我的分析,我希望能够排除某个事件发生或不发生的可能性。 所
USPS webtools 很贵,而且 Google Maps 没有某些地址。有没有免费或开源的解决方案? 最佳答案 根据您的业务性质,或者如果您是非营利或教育机构,有一家在线提供商可为您提供 fre
我有一个 json 对象,里面有对象例如 user: {"name": "tim"} 并希望有一种方法将其转换为 "user.name": 'tim' 我已经尝试过:Javascript Recurs
我有一个名为 spam 的数据集,其中包含与垃圾邮件相关的 58 列和大约 3500 行数据。 我计划将来对此数据集运行一些线性回归,但我想事先进行一些预处理并将列标准化为均值和单位方差为零。 我被告
我试图找到一种方法来强调音频中高点和低点之间的差异。我似乎找不到有关如何执行此操作的文档-也许可以使用ffmpeg完成。非常感谢来自对信号处理了解更多的人的一些指导。 最佳答案 从根本上讲,膨胀器与压
如何缩放/规范化每行数据(观察)?像 [-1:1] 这样的 z 分数? 我看过之前的帖子,其中涉及整个数据集的标准化,如下所示 https://stats.stackexchange.com/ques
例如: http://www.google.co.uk www.google.co.uk google.co.uk 将全部转换为: google.co.uk 我本来想使用 System.Uri 类,但
我可以使用此代码从 json 文件导入数据... import requests from pandas.io.json import json_normalize url = "https://da
规范化 Prolog 原子中空白字符(空格、换行符、制表符)的最佳方法是什么,例如在 SWI-Prolog 中。IE。我想要一个规则: normalize_space_in_atom(+Atom1,
如何缩放/规范化每行数据(观察)?像 [-1:1] 这样的 z 分数? 我看过之前的帖子,其中涉及整个数据集的标准化,如下所示 https://stats.stackexchange.com/ques
我正在尝试阅读代码片段,但它对我来说没有任何意义。请帮助我 /** * To make it easy to query for the exact date, we normalize all
我是一名研究人员,拥有大约 17,000 份自由文本文档,其中大约 30-40% 与我的结果相关。是否有一个开源工具可以用来确定与结果相关的最常见的单词(甚至短语,但不是必需的),并对已经出现的单词的
我正在尝试使用 L2 范数(单位长度)对 numpy 矩阵的行进行标准化。 当我这样做时,我发现了一个问题。 假设我的矩阵“b”如下: 现在,当我对第一行进行标准化时,如下所示,它工作正常。 但是当我
我有一个 3 列数组。数组的第一列的值介于 1 和 10 之间。我需要提取第一列为 1 的所有行,并规范化该数组切片的第三列。然后对第一列等于 2 等的所有行重复相同的操作。 如果我运行此代码,它会使
如果我通过许多不同的文件/脚本使用这个结构当需要更改时,如何才能只更改一个地方,而不必在每个文件中更改它。 u = contents incomingUrl = urlparse(u).query o
我有 1MB 的文本文件。我想删除空格、换行符、制表符,并以 4KB 迭代方式将 1MB 文件的字符大小写从小写转换为大写。 我写了这段代码: for (i = 0, j= 0; i 首先,让我们定
这实际上是一个机器学习分类问题,但我想有一种非常好的快速而肮脏的方法来做到这一点。我想将描述 NFL 球队的字符串(例如“San Francisco”或“49ers”或“San Francisco 4
我正在创建游戏评论数据库。我对此很陌生,但我正在尽力。关于数据库的一点点:系统很简单,用户填写 php 表单,在其中插入他的姓名、电子邮件,然后选择反馈是好还是坏并留下评论/建议。我正在添加照片的视觉
我是一名优秀的程序员,十分优秀!