Ich versuche, die Entropie meiner k-Mittel zu nehmen Datenrahmen Ergebnis und ich erhalte den Fehler zurück: Typeerror: ‚numpy.int32‘ Objekt ist nicht iterable I verstehe nicht warum.Python Typeerror: ‚numpy.int32‘ Objekt ist nicht Iterable
from collections import Counter
def calcEntropy(x):
p, lens = Counter(x), np.float(len(x))
return -np.sum(count/lens*np.log2(count/lens) for count in p.values())
k_means_sp['entropy']=[calcEntropy(x) for x in k_means_sp['cluster']]
und dann erhalte ich die Fehlermeldung:
<ipython-input-26-d375ecf00330> in <module>()
----> 1 k_means_sp['entropy']=[calcEntropy(x) for x in k_means_sp['cluster']]
<ipython-input-26-d375ecf00330> in <listcomp>(.0)
----> 1 k_means_sp['entropy']=[calcEntropy(x) for x in k_means_sp['cluster']]
<ipython-input-23-f5508ea8782c> in calcEntropy(x)
1 from collections import Counter
2 def calcEntropy(x):
----> 3 p, lens = Counter(x), np.float(len(x))
4 return -np.sum(count/lens*np.log2(count/lens) for count in p.values())
/Users/mpiercy/anaconda/lib/python3.6/collections/__init__.py in __init__(*args, **kwds)
535 raise TypeError('expected at most 1 arguments, got %d' % len(args))
536 super(Counter, self).__init__()
--> 537 self.update(*args, **kwds)
538
539 def __missing__(self, key):
/Users/mpiercy/anaconda/lib/python3.6/collections/__init__.py in update(*args, **kwds)
622 super(Counter, self).update(iterable) # fast path when counter is empty
623 else:
--> 624 _count_elements(self, iterable)
625 if kwds:
626 self.update(kwds)
TypeError: 'numpy.int32' object is not iterable
k_means_sp.head()
credit debit cluster
0 9.207673 8.198884 1
1 4.248495 8.202181 0
2 8.149668 7.735145 2
3 5.138677 7.859741 0
4 8.058163 7.918614 2
vorausgesetzt, 'k_means_sp' hält' numpy.int32', dann übergibt man eine 'numpy.int32' an' Counter'. "Counter" sollte ein "iterable" nehmen. –
Was bedeutet das, dass ich die Cluster-Spalte Cluster = [0,1,2] und Y = Iter (Cluster) machen sollte oder mache ich das völlig falsch? Vielen Dank! – bananablue1
@ bananablue1 Dies bedeutet, dass Sie keine Ganzzahl an "calcEntropy" übergeben können, wie es derzeit geschrieben wird. Was Sie tun müssen, hängt von Ihrem Ziel ab. Wenn Sie möchten, dass 'calcEntropy' mit Ganzzahlen arbeitet (macht das irgendeinen Sinn?), Dann sollten Sie es reparieren, wenn Sie etwas anderes an 'calcEntropy' übergeben wollen, dann etwas anderes weitergeben, usw. – Goyo