commonMain.jetbrains.datalore.plot.base.util.SamplingUtil.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lets-plot-common Show documentation
Show all versions of lets-plot-common Show documentation
Lets-Plot JVM package without rendering part
/*
* 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