k-means
k-means is one of the most commonly used clustering algorithms that clusters the data points into a predefined number of clusters.
import org.apache.spark.ml.clustering.KMeans
// Loads data.
val dataset = spark.read.format("libsvm").load("file:///opt/spark/data/mllib/sample_libsvm_data.txt")
// Trains a k-means model.
val kmeans = new KMeans().setK(2).setSeed(1L)
val model = kmeans.fit(dataset)
// Evaluate clustering by computing Within Set Sum of Squared Errors.
val WSSSE = model.computeCost(dataset)
println(s"Within Set Sum of Squared Errors = $WSSSE")
// Shows the result.
println("Cluster Centers: ")
model.clusterCenters.foreach(println)
/*
Output:
Within Set Sum of Squared Errors = 2.1480729824555594E8
Cluster Centers:
[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.3658536585365855,6.024390243902439,2.951219512195122,1.7073170731707317,6.219512195121951,4.024390243902439,2.780487804878049,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.5365853658536586,5.926829268292683,13.048780487804878,43.6829268292683,65.19512195121952,76.5609756097561,91.14634146341464,97.29268292682927,92.75609756097562,65.4390243902439,25.414634146341463,6.804878048780488,1.926829268292683,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.7317073170731707,8.414634146341463,25.731707317073173,55.87804878048781,88.0,123.21951219512195,166.4390243902439,195.51219512195124,192.3170731707317,173.39024390243904,142.3658536585366,100.53658536585367,48.07317073170732,18.097560975609756,3.4634146341463414,2.073170731707317,0.34146341463414637,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.219512195121951,13.682926829268293,34.07317073170732,69.5609756097561,87.07317073170732,129.95121951219514,171.70731707317074,205.1951219512195,224.02439024390245,219.26829268292684,199.9268292682927,188.70731707317074,152.85365853658539,96.1219512195122,43.21951219512195,13.707317073170731,6.097560975609756,3.5609756097560976,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.1951219512195124,16.073170731707318,36.29268292682927,62.19512195121951,87.39024390243902,125.87804878048782,167.09756097560975,211.8048780487805,211.26829268292684,202.14634146341464,211.90243902439025,223.4390243902439,208.58536585365854,205.58536585365854,159.3170731707317,78.90243902439025,21.317073170731707,7.024390243902439,6.097560975609756,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.5609756097560976,11.731707317073171,28.926829268292686,60.951219512195124,83.19512195121952,129.09756097560975,167.0,187.1219512195122,198.6829268292683,171.609756097561,147.0,149.1951219512195,183.85365853658539,187.65853658536585,209.4878048780488,189.7560975609756,122.3658536585366,48.24390243902439,15.658536585365853,6.414634146341464,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.7073170731707314,23.34146341463415,42.65853658536586,77.8048780487805,111.8780487804878,150.6341463414634,190.07317073170734,173.53658536585365,156.07317073170734,125.41463414634147,93.31707317073172,91.09756097560975,110.51219512195122,134.2926829268293,179.65853658536585,196.39024390243904,148.85365853658536,89.5609756097561,26.878048780487806,9.951219512195122,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,7.878048780487805,34.80487804878049,63.65853658536586,96.26829268292683,142.6341463414634,167.6341463414634,165.02439024390245,150.1219512195122,120.0,89.26829268292683,55.463414634146346,50.829268292682926,66.8780487804878,91.92682926829269,142.3658536585366,191.0487804878049,161.8048780487805,112.92682926829269,48.0,14.902439024390244,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,20.853658536585368,55.78048780487805,84.78048780487805,114.90243902439025,158.3170731707317,155.4878048780488,147.1219512195122,122.4390243902439,80.8780487804878,47.63414634146341,26.634146341463417,25.243902439024392,37.146341463414636,64.65853658536585,104.41463414634147,162.21951219512195,160.14634146341464,133.1219512195122,72.65853658536585,23.731707317073173,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,33.390243902439025,72.4390243902439,101.19512195121952,144.7560975609756,177.95121951219514,150.14634146341464,120.02439024390245,84.51219512195122,50.487804878048784,22.70731707317073,11.609756097560975,11.878048780487806,26.975609756097562,53.951219512195124,81.51219512195122,149.8780487804878,158.1219512195122,143.0487804878049,91.48780487804878,32.65853658536585,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,36.87804878048781,89.2439024390244,126.58536585365854,161.82926829268294,173.14634146341464,130.0,91.04878048780488,61.87804878048781,22.878048780487806,8.78048780487805,6.682926829268293,3.3414634146341466,16.439024390243905,56.53658536585366,79.1219512195122,143.51219512195124,162.2439024390244,152.09756097560975,93.75609756097562,37.36585365853659,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.4634146341463415,46.31707317073171,107.82926829268293,147.07317073170734,174.02439024390245,152.0487804878049,98.17073170731707,63.51219512195122,25.585365853658537,6.731707317073171,2.2439024390243905,1.7317073170731707,0.24390243902439024,16.097560975609756,46.19512195121951,89.14634146341464,139.3170731707317,157.8780487804878,135.53658536585365,93.53658536585367,41.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.8292682926829269,66.3658536585366,129.4878048780488,162.1951219512195,184.8780487804878,145.0487804878049,82.97560975609757,30.195121951219512,7.0,0.0,0.0,0.0,0.0,14.609756097560975,45.951219512195124,97.51219512195122,147.58536585365854,147.85365853658536,123.48780487804879,87.21951219512195,36.51219512195122,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.2926829268292683,80.46341463414635,139.95121951219514,179.3658536585366,183.02439024390245,129.3170731707317,66.63414634146342,21.21951219512195,6.341463414634147,0.0,0.0,0.0,2.048780487804878,13.804878048780488,62.390243902439025,114.6829268292683,150.2926829268293,149.2926829268293,100.51219512195122,76.3658536585366,33.73170731707317,0.0,0.0,0.0,0.0,0.0,0.0,0.0,6.658536585365854,84.1219512195122,144.2926829268293,186.97560975609755,178.90243902439025,130.3170731707317,55.78048780487805,19.04878048780488,9.097560975609756,0.2682926829268293,0.7560975609756098,2.4634146341463414,13.707317073170731,33.97560975609756,90.70731707317073,137.7560975609756,161.41463414634146,130.95121951219514,87.21951219512195,67.02439024390245,26.878048780487806,0.0,0.0,0.0,0.0,0.0,0.0,0.0,6.731707317073171,74.97560975609757,135.2439024390244,193.8780487804878,188.78048780487805,132.1707317073171,54.6829268292683,17.365853658536587,9.634146341463415,5.146341463414634,8.170731707317074,15.829268292682928,36.707317073170735,88.02439024390245,132.41463414634146,156.5609756097561,145.78048780487805,99.34146341463415,73.92682926829268,48.24390243902439,16.146341463414636,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.463414634146342,56.268292682926834,123.78048780487805,186.39024390243904,207.3658536585366,163.2926829268293,95.60975609756098,50.5609756097561,26.243902439024392,23.024390243902438,34.02439024390244,51.53658536585366,89.39024390243902,139.0487804878049,161.07317073170734,160.78048780487805,117.51219512195122,90.5609756097561,57.80487804878049,33.58536585365854,6.170731707317073,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.4878048780487805,31.75609756097561,98.70731707317074,160.58536585365854,210.1219512195122,197.3170731707317,156.07317073170734,120.46341463414635,105.73170731707317,113.41463414634147,115.78048780487805,135.9268292682927,155.7560975609756,175.70731707317074,162.3658536585366,125.04878048780488,100.09756097560975,66.78048780487805,43.36585365853659,14.878048780487806,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.7804878048780488,13.463414634146343,57.146341463414636,132.46341463414635,191.41463414634148,224.7560975609756,217.4878048780488,193.51219512195124,187.63414634146343,181.2926829268293,185.97560975609755,196.4390243902439,197.4390243902439,177.1707317073171,141.8780487804878,95.17073170731707,70.41463414634147,46.0,21.146341463414636,1.6585365853658538,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.5121951219512195,7.75609756097561,22.48780487804878,68.82926829268293,135.82926829268294,191.82926829268294,226.53658536585365,232.0,222.97560975609755,223.82926829268294,216.70731707317074,201.90243902439025,175.1707317073171,130.7317073170732,89.09756097560975,65.65853658536585,42.268292682926834,18.29268292682927,4.073170731707317,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.024390243902439,8.682926829268293,22.0,52.41463414634146,106.78048780487805,154.7560975609756,183.82926829268294,190.26829268292684,175.82926829268294,163.70731707317074,117.6829268292683,83.29268292682927,64.75609756097562,43.268292682926834,27.463414634146343,11.02439024390244,2.3414634146341466,0.24390243902439024,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.878048780487805,11.951219512195122,12.634146341463415,18.365853658536587,26.34146341463415,43.487804878048784,49.951219512195124,49.926829268292686,47.926829268292686,33.80487804878049,20.146341463414636,16.0,8.902439024390244,2.658536585365854]
[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0847457627118644,3.23728813559322,4.0,5.440677966101695,5.627118644067797,1.305084745762712,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.4406779661016949,4.288135593220339,2.23728813559322,0.15254237288135594,6.372881355932203,27.050847457627118,38.186440677966104,44.220338983050844,45.440677966101696,49.983050847457626,67.94915254237289,53.847457627118644,34.983050847457626,10.033898305084746,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.3898305084745763,4.084745762711864,4.254237288135593,4.254237288135593,3.3220338983050848,10.779661016949152,45.6271186440678,64.30508474576271,69.83050847457628,81.05084745762711,89.86440677966101,103.59322033898304,80.0677966101695,64.37288135593221,22.89830508474576,3.830508474576271,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.9661016949152543,4.254237288135593,4.254237288135593,4.254237288135593,5.389830508474576,18.864406779661017,46.813559322033896,66.72881355932203,85.76271186440678,95.83050847457628,117.0,113.91525423728814,94.64406779661017,66.88135593220339,21.71186440677966,6.389830508474576,0.13559322033898305,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.22033898305084745,3.457627118644068,4.254237288135593,5.491525423728813,8.254237288135593,21.1864406779661,48.86440677966102,68.20338983050847,99.01694915254237,109.13559322033898,132.72881355932202,113.64406779661017,96.98305084745763,61.50847457627118,20.16949152542373,7.186440677966102,1.152542372881356,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.6610169491525424,4.254237288135593,7.338983050847458,10.271186440677965,21.966101694915253,47.559322033898304,74.22033898305085,117.03389830508475,140.9322033898305,142.64406779661016,117.30508474576271,93.1864406779661,45.42372881355932,9.94915254237288,3.4915254237288136,1.694915254237288,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.2542372881355932,2.8983050847457625,9.711864406779661,19.0,25.322033898305083,47.067796610169495,79.42372881355932,139.74576271186442,159.4237288135593,150.77966101694915,109.69491525423729,76.47457627118644,29.8135593220339,7.169491525423728,2.7796610169491527,1.152542372881356,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.3559322033898305,5.491525423728813,11.322033898305085,14.847457627118644,18.45762711864407,42.0,90.27118644067797,167.72881355932202,184.66101694915255,148.64406779661016,95.08474576271186,45.96610169491525,15.76271186440678,1.728813559322034,3.8983050847457625,4.305084745762712,3.152542372881356,0.23728813559322035,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.4915254237288136,7.271186440677966,10.067796610169491,8.915254237288135,14.847457627118644,16.016949152542374,34.28813559322034,107.77966101694915,192.72881355932202,199.15254237288136,137.4237288135593,82.0,28.8135593220339,6.6440677966101696,0.0,0.559322033898305,3.0338983050847457,4.288135593220339,2.3728813559322033,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.7966101694915254,7.186440677966102,11.322033898305085,8.067796610169491,5.915254237288136,10.271186440677965,10.694915254237289,35.47457627118644,126.57627118644068,215.3728813559322,204.94915254237287,131.23728813559322,61.79661016949152,12.983050847457626,4.288135593220339,0.0,0.0,0.4067796610169492,3.542372881355932,3.7796610169491527,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.864406779661017,8.542372881355933,4.237288135593221,0.9830508474576272,1.1355932203389831,4.915254237288136,5.0,40.57627118644068,173.4406779661017,231.1864406779661,206.27118644067798,115.33898305084746,34.50847457627118,6.220338983050848,3.9152542372881354,0.0,0.0,0.0,2.305084745762712,4.288135593220339,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,6.135593220338983,4.322033898305085,0.2542372881355932,0.0,1.6610169491525424,4.305084745762712,4.305084745762712,60.30508474576271,201.1864406779661,234.28813559322035,197.35593220338984,79.15254237288136,22.23728813559322,2.8135593220338984,2.6271186440677967,0.0,0.0,0.0,2.305084745762712,3.6440677966101696,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.322033898305085,1.4067796610169492,0.9322033898305084,0.0,1.6610169491525424,4.694915254237288,12.898305084745763,100.89830508474576,217.79661016949152,234.03389830508473,165.91525423728814,48.57627118644068,10.779661016949152,4.033898305084746,2.6271186440677967,0.0,0.0847457627118644,2.152542372881356,4.169491525423728,2.2542372881355934,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.559322033898305,2.4745762711864407,0.3898305084745763,0.0,1.6610169491525424,9.796610169491526,41.6271186440678,124.40677966101696,214.20338983050848,223.23728813559322,134.32203389830508,36.559322033898304,5.966101694915254,7.711864406779661,5.47457627118644,0.288135593220339,2.542372881355932,4.186440677966102,1.542372881355932,0.15254237288135594,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,5.271186440677966,4.203389830508475,0.0,0.0,2.457627118644068,25.0,75.59322033898304,136.27118644067798,211.98305084745763,196.94915254237287,105.45762711864407,31.93220338983051,7.220338983050848,11.559322033898304,10.677966101694915,5.762711864406779,3.6610169491525424,1.423728813559322,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.88135593220339,4.254237288135593,1.4745762711864407,0.0,6.491525423728813,49.559322033898304,102.88135593220339,149.15254237288136,202.20338983050848,166.71186440677965,89.42372881355932,33.610169491525426,16.88135593220339,17.033898305084747,13.576271186440678,8.033898305084746,1.7457627118644068,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.23728813559322035,3.135593220338983,4.084745762711864,6.305084745762712,25.220338983050848,79.71186440677965,116.62711864406779,153.23728813559322,173.32203389830508,148.83050847457628,85.62711864406779,46.67796610169491,25.576271186440678,17.47457627118644,14.101694915254237,9.338983050847457,2.3389830508474576,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.3898305084745763,4.440677966101695,18.88135593220339,50.101694915254235,92.77966101694915,132.84745762711864,149.0,144.6949152542373,126.33898305084746,76.23728813559322,47.30508474576271,30.016949152542374,18.93220338983051,8.305084745762711,6.694915254237288,3.23728813559322,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.237288135593221,11.23728813559322,25.966101694915253,68.0,99.71186440677965,127.86440677966101,127.72881355932203,123.86440677966101,107.37288135593221,77.72881355932203,47.59322033898305,26.983050847457626,14.830508474576272,7.762711864406779,4.932203389830509,2.9322033898305087,0.0,0.0,0.0,0.0,2.3728813559322033,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.457627118644068,13.322033898305085,37.76271186440678,81.55932203389831,105.8135593220339,119.32203389830508,105.03389830508475,109.23728813559322,98.91525423728814,68.42372881355932,36.88135593220339,24.54237288135593,17.694915254237287,14.627118644067796,5.9491525423728815,2.9322033898305087,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.2033898305084746,5.898305084745763,26.423728813559322,75.2542372881356,91.11864406779661,84.45762711864407,60.94915254237288,63.3728813559322,63.559322033898304,40.389830508474574,20.440677966101696,18.372881355932204,14.59322033898305,7.203389830508475,4.593220338983051,2.9322033898305087,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.9491525423728814,20.491525423728813,26.406779661016948,19.508474576271187,9.491525423728813,4.983050847457627,14.694915254237289,10.576271186440678,0.5254237288135594,0.0,0.0,0.0]
*/
Within Set Sum of Squared Errors = 2.1480729824555594E8
Cluster Centers:
[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.3658536585365855,6.024390243902439,...]
[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0847457627118644,3.23728813559322,4.0,5.440677966101695,5.627118644067797,1.305084745762712,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.4406779661016949,4.288135593220339,2.23728813559322,0.15254237288135594,6.372881355932203,27.050847457627118,38.186440677966104,44.220338983050844,45.440677966101696,49.983050847457626,67.94915254237289,53.847457627118644,34.983050847457626,10.033898305084746,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.3898305084745763,4.084745762711864,4.254237288135593,4.254237288135593,...]
Last updated