All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.tsingjyujing.geo.algorithm.cluster.GeoKMeansPlus.scala Maven / Gradle / Ivy

package com.github.tsingjyujing.geo.algorithm.cluster

import com.github.tsingjyujing.geo.basic.IGeoPoint
import com.github.tsingjyujing.geo.util.mathematical.Probability.{selectByProbability => sbp, selectUniformly => su}

import scala.collection.mutable

/**
  * Implementation of K-Means++ on geographical points.
  *
  * @see paper: k-means++: The Advantages of Careful Seeding
  * @tparam T type of point
  */
class GeoKMeansPlus[T <: IGeoPoint] extends BaseGeoKMeans[T] {
    /**
      * Get initialized k center points
      *
      * @param points sample point
      * @param k      k centers
      * @return
      */
    override def initializePoints(points: Iterable[T], k: Int): Iterable[IGeoPoint] = {
        val indexedPoints: IndexedSeq[T] = points.toIndexedSeq
        assert(k > 1, "k should an integer which greater than 1")
        val initPoints = mutable.ArrayBuffer[IGeoPoint]()
        initPoints += su(indexedPoints)
        (1 until k).foreach(_ => {
            val probWeight = indexedPoints.map(p => {
                val mind = initPoints.map(_.geoTo(p)).min
                mind * mind
            })
            initPoints += sbp(indexedPoints, probWeight)
        })
        initPoints
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy