'why my code get that TypeError: list indices must be integers or slices, not str in Python
kmeans_99 = KMeans(n_clusters = 3).fit(X_99)
print(kmeans_99)
labels_99 = kmeans_99.labels_
#Counting the number of the labels in each cluster and saving the data into clustering_classes
clustering_classes_99 = {
0: [0,0,0],
1: [0,0,0],
2: [0,0,0]
}
for i in range(len(y)):
clustering_classes_99[labels_99[i]][y[i]] #why this line get error
#Finding the most appeared label in each cluster and computing the purity score
purity_score_99 = (max(clustering_classes_99[0])+max(clustering_classes_99[1])+max(clustering_classes_99[2]))/len(y)
print(f"The purity score is {round(purity_score_99*100,2)} %")
I get TypeError: list indices must be integers or slices, not str. I can't find the solution. I try lot of ways.
This is the error I got:
TypeError Traceback (most recent call last)
<ipython-input-31-95be616a06bd> in <module>()
12
13 for i in range(len(y)):
---> 14 clustering_classes_99[labels_99[i]][y[i]]
15
16 #Finding the most appeared label in each cluster and computing the purity score
TypeError: list indices must be integers or slices, not str
please help me to solve this
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
