When I compute the silhouette score with the same data and same prediction values while using Spark and sklearn I get different results.
Here is the code using for Spark:
>>> prediction.show()
+---+---+---------+----------+
| a| b| features|prediction|
+---+---+---------+----------+
| 1| 1|[1.0,1.0]| 1|
| 2| 2|[2.0,2.0]| 1|
| 3| 3|[3.0,3.0]| 0|
| 4| 4|[4.0,4.0]| 0|
+---+---+---------+----------+
>>> from pyspark.ml.evaluation import ClusteringEvaluator
>>> evaluator = ClusteringEvaluator()
>>> silhouette = evaluator.evaluate(prediction)
>>> silhouette
0.7230769230769223
Here is the code using for sklearn:
>>> from sklearn.cluster import KMeans
>>> from sklearn import metrics
>>> x=[[1,1],[2,2],[3,3],[4,4]]
>>> prediction = KMeans(n_clusters=2,max_iter=1000,random_state=123).fit_predict(x)
>>> prediction
array([1, 1, 0, 0], dtype=int32)
>>> silhouette = metrics.silhouette_score(x, prediction)
>>> silhouette
0.46666666666666673
As shown above, the scores are very different when though the inputs are the same. Why is that?