I run this code, and my RFM_scaleddf has float values for recency, frequency and monetary values.
K_clusters = [2,3,4,5,6,7,8,9,10]
silhouette_scores = []
for K in K_clusters:
initialised_clusters = KMeans(n_clusters = K, max_iter = 30, random_state = 10)
initialised_clusters.fit(RFM_scaleddf)
cluster_labels = initialised_clusters.labels_
score = silhouette_score(RFM_scaleddf, cluster_labels, random_state = 10)
silhouette_scores.append(score)
The difference is that with RFM dataset, because it has float values, the sklearn silhouette score is not working.
K_clusters = [2,3,4,5,6,7,8,9,10]
silhouette_scores = []
for K in K_clusters:
initialised_clusters = KMeans(n_clusters = K, max_iter = 30, random_state = 10)
initialised_clusters.fit(RFM_scaleddf)
cluster_labels = initialised_clusters.labels_
score = silhouette_score(RFM_scaleddf, cluster_labels, random_state = 10)
silhouette_scores.append(score)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In [127], line 8
6 initialised_clusters.fit(RFM_scaleddf)
7 cluster_labels = initialised_clusters.labels_
----> 8 score = silhouette_score(RFM_scaleddf, cluster_labels, random_state = 10)
9 silhouette_scores.append(score)
TypeError: 'numpy.float64' object is not callable
The
TypeError
informing us thatnumpy.float64
is not callable suggests thatsilhouette_score
is re-defined from an imported function into a float elsewhere in the code.i.e.: Check the code for something like this:
This (
scikit-learn>=1.2.0
) minimal reproducible example should produce no error:But adding these lines at the end:
Raises: