簡體   English   中英

Debug TypeError:不可用類型:'numpy.ndarray'

[英]Debug TypeError: unhashable type: 'numpy.ndarray'

我正在研究kmeans聚類。 我已經在網上的一些可用引用的幫助下寫下了代碼但是當我運行這段代碼時它會觸發一個錯誤:

    Traceback (most recent call last):
  File "clustering.py", line 16, in <module>
    ds = df[np.where(labels==i)]
  File "/usr/lib/python2.7/dist-packages/pandas/core/frame.py", line 1678, in __getitem__
    return self._getitem_column(key)
  File "/usr/lib/python2.7/dist-packages/pandas/core/frame.py", line 1685, in _getitem_column
    return self._get_item_cache(key)
  File "/usr/lib/python2.7/dist-packages/pandas/core/generic.py", line 1050, in _get_item_cache
    res = cache.get(item)
TypeError: unhashable type: 'numpy.ndarray'

盡管如此,許多以前的線程都可以使用相同的錯誤,但是沒有單一的解決方案可以在我的程序中處理這個錯誤。 我該如何調試此錯誤?

我使用的代碼:

from sklearn import cluster
import pandas as pd

df = [
[0.57,-0.845,-0.8277,-0.1585,-1.616],
[0.47,-0.14,-0.5277,-0.158,-1.716],
[0.17,-0.845,-0.5277,-0.158,-1.616],
[0.27,-0.14,-0.8277,-0.158,-1.716]]

df = pd.DataFrame(df,columns= ["a","b","c","d", "e"])

# df = pd.read_csv("cleaned_remove_cor.csv")

k = 3
kmeans = cluster.KMeans(n_clusters=k)
kmeans.fit(df)
labels = kmeans.labels_
centroids = kmeans.cluster_centers_
from matplotlib import pyplot
import numpy as np

for i in range(k):
    # select only data observations with cluster label == i
    ds = df[np.where(labels==i)]
    # plot the data observations
    pyplot.plot(ds[:,0],ds[:,1],'o')
    # plot the centroids
    lines = pyplot.plot(centroids[i,0],centroids[i,1],'kx')
    # make the centroid x's bigger
    pyplot.setp(lines,ms=15.0)
    pyplot.setp(lines,mew=2.0)
pyplot.show()

我的DataFrame的形狀是(8127x600)

我試過,這對我有用,將pandas df轉換為numpy矩陣:

df = df.as_matrix(columns= ["a","b","c","d", "e"])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM