python-3.x - 由于 TypeError 无法绘制散点图
问题描述
我有一个数据集,我将在其中只使用一个列来应用 kmeans 聚类。然而,在绘制图表时,我得到了“numpy.ndarray”。我尝试转换为浮动,但仍然面临同样的问题
数据框:
Brim
1234.5
345
675.7
120
110
代码:
from sklearn.cluster import KMeans
import numpy as np
km = KMeans(n_clusters=4, init='k-means++',n_init=10)
km.fit(df1)
x = km.fit_predict(df1)
x
array([0, 0, 0, ..., 3, 3, 3])
np.shape(x)
(1097,)
import matplotlib.pyplot as plt
%matplotlib inline
plt.scatter(df1[x ==1,0], df1[x == 0,1], s=100, c='red')
plt.scatter(df1[x ==1,0], df1[x == 1,1], s=100, c='black')
plt.scatter(df1[x ==2,0], df1[x == 2,1], s=100, c='blue')
plt.scatter(df1[x ==3,0], df1[x == 3,1], s=100, c='cyan')
错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-62-5f0966ccc828> in <module>()
1 import matplotlib.pyplot as plt
2 get_ipython().run_line_magic('matplotlib', 'inline')
----> 3 plt.scatter(df1[x ==1,0], df1[x == 0,1], s=100, c='red')
4 plt.scatter(df1[x ==1,0], df1[x == 1,1], s=100, c='black')
5 plt.scatter(df1[x ==2,0], df1[x == 2,1], s=100, c='blue')
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
2137 return self._getitem_multilevel(key)
2138 else:
->2139 return self._getitem_column(key)
2140
2141 def _getitem_column(self, key):
~\AppData\Local\Continuum\anaconda3\lib\site-
packages\pandas\core\frame.py in _getitem_column(self, key)
2144 # get column
2145 if self.columns.is_unique:
-> 2146 return self._get_item_cache(key)
2147
2148 # duplicate columns & possible reduce dimensionality
~\AppData\Local\Continuum\anaconda3\lib\site- packages\pandas\core\generic.py in _get_item_cache(self, item)
1838 """Return the cached item, item represents a label indexer."""
1839 cache = self._item_cache
-> 1840 res = cache.get(item)
1841 if res is None:
1842 values = self._data.get(item)
TypeError: unhashable type: 'numpy.ndarray'
解决方案
如果我正确理解了您的代码,则您正在尝试根据x
. 为此,您应该使用df1.loc[x==1,0]
代替df1[x==1,0]
(对于所有其他切片,依此类推)。
推荐阅读
- php - 如何/在哪里在 PHP 中两次声明一个静态函数?
- python-3.x - 有没有办法通过我的 docker 镜像在 selenium 网格上运行我的 python 测试?
- python - 从 Doc 的迭代器中提取数据
- c++ - Cmake使用目标target_include_directories时无法指定包含目录
- python - 如果某个任务正在任务管理器中运行,如何在 python 中使用 pywin 或 win32com.client 检查?
- android - 如何避免在 realmResults 中进行 n+1 查询?
- neo4j - neo4j foreach 条件总是执行
- python - CP-SAT 的天花板
- python - python json.dumps:无法忽略不可序列化的对象
- python - Tensorflow 线性回归预测返回 [nan]