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

org.nield.kotlinstatistics.Clustering.kt Maven / Gradle / Ivy

The newest version!
package org.nield.kotlinstatistics

import org.apache.commons.math3.ml.clustering.*

fun Collection>.kMeansCluster(k: Int, maxIterations: Int) = kMeansCluster(k, maxIterations, {it.first}, {it.second})
fun Sequence>.kMeansCluster(k: Int, maxIterations: Int) = toList().kMeansCluster(k, maxIterations, {it.first}, {it.second})

inline fun  Collection.kMeansCluster(k: Int, maxIterations: Int, crossinline xSelector: (T) -> Double, crossinline ySelector: (T) -> Double) =
        asSequence().map { ClusterInput(it, doubleArrayOf(xSelector(it), ySelector(it))) }
                .toList()
                .let {
                    KMeansPlusPlusClusterer>(k,maxIterations)
                        .cluster(it)
                        .map {
                            Centroid((it.center).point.let { DoublePoint(it[0],it[1])}, it.points.map { it.item })
                        }
                }


inline fun  Sequence.kMeansCluster(k: Int, maxIterations: Int, crossinline xSelector: (T) -> Double, crossinline ySelector: (T) -> Double) =
        toList().kMeansCluster(k,maxIterations,xSelector,ySelector)



inline fun  Collection.fuzzyKMeansCluster(k: Int, fuzziness: Double, crossinline xSelector: (T) -> Double, crossinline ySelector: (T) -> Double) =
        asSequence().map { ClusterInput(it, doubleArrayOf(xSelector(it), ySelector(it))) }
                .toList()
                .let {
                    FuzzyKMeansClusterer>(k,fuzziness)
                            .cluster(it)
                            .map {
                                Centroid((it.center).point.let { DoublePoint(it[0],it[1])}, it.points.map { it.item })
                            }
                }
inline fun  Sequence.fuzzyKMeansCluster(k: Int, fuzziness: Double, crossinline xSelector: (T) -> Double, crossinline ySelector: (T) -> Double) =
        toList().fuzzyKMeansCluster(k,fuzziness,xSelector,ySelector)


fun Collection>.multiKMeansCluster(k: Int, maxIterations: Int, trialCount: Int) = multiKMeansCluster(k, maxIterations,  trialCount, {it.first}, {it.second})
fun Sequence>.multiKMeansCluster(k: Int, maxIterations: Int, trialCount: Int) = toList().multiKMeansCluster(k, maxIterations, trialCount, {it.first}, {it.second})

inline fun  Sequence.multiKMeansCluster(k: Int, maxIterations: Int, trialCount: Int, crossinline xSelector: (T) -> Double, crossinline ySelector: (T) -> Double) =
        toList().multiKMeansCluster(k, maxIterations, trialCount, xSelector, ySelector)


inline fun  Collection.multiKMeansCluster(k: Int, maxIterations: Int, trialCount: Int, crossinline xSelector: (T) -> Double, crossinline ySelector: (T) -> Double) =
        asSequence().map { ClusterInput(it, doubleArrayOf(xSelector(it), ySelector(it))) }
                .toList()
                .let { list ->
                    KMeansPlusPlusClusterer>(k, maxIterations)
                            .let {
                                MultiKMeansPlusPlusClusterer>(it, trialCount)
                                        .cluster(list)
                                        .map {
                                            Centroid(DoublePoint(-1.0,-1.0), it.points.map { it.item })
                                        }
                            }
                }


inline fun  Collection.dbScanCluster(maximumRadius: Double, minPoints: Int, crossinline xSelector: (T) -> Double, crossinline ySelector: (T) -> Double) =
        asSequence().map { ClusterInput(it, doubleArrayOf(xSelector(it), ySelector(it))) }
                .toList()
                .let {
                    DBSCANClusterer>(maximumRadius,minPoints)
                            .cluster(it)
                            .map {
                                Centroid(DoublePoint(-1.0,-1.0), it.points.map { it.item })
                            }
                }


inline fun  Sequence.dbScanCluster(maximumRadius: Double, minPoints: Int, crossinline xSelector: (T) -> Double, crossinline ySelector: (T) -> Double) =
        toList().dbScanCluster(maximumRadius,minPoints,xSelector,ySelector)

class ClusterInput(val item: T, val location: DoubleArray): Clusterable {
    override fun getPoint() = location
}

data class DoublePoint(val x: Double, val y: Double)
data class Centroid(val center: DoublePoint, val points: List)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy