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

commonMain.jetbrains.datalore.plot.base.util.SamplingUtil.kt Maven / Gradle / Ivy

There is a newer version: 4.5.3-alpha1
Show newest version
/*
 * Copyright (c) 2020. JetBrains s.r.o.
 * Use of this source code is governed by the MIT license that can be found in the LICENSE file.
 */

package jetbrains.datalore.plot.base.util

import jetbrains.datalore.plot.base.DataFrame
import kotlin.random.Random

object SamplingUtil {

    fun  sampleWithoutReplacement(
        popSize: Int, sampleSize: Int, rand: Random, onPick: (Set) -> T, onDrop: (Set) -> T
    ): T {
        val pick = sampleSize <= popSize / 2
        val indexCount = if (pick) sampleSize else popSize - sampleSize

        val indexSet = HashSet()

        while (indexSet.size < indexCount) {
            indexSet.add(rand.nextInt(popSize))
        }

        return if (pick) onPick(indexSet) else onDrop(indexSet)
    }

    fun sampleWithoutReplacement(sampleSize: Int, rand: Random, data: DataFrame): DataFrame {
        return sampleWithoutReplacement(
            data.rowCount(),
            sampleSize,
            rand,
            { data.selectIndices(it) },
            { data.dropIndices(it) }
        )
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy