se.alipsa.groovy.stats.Sampler.groovy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of matrix-stats Show documentation
Show all versions of matrix-stats Show documentation
Statistical hypothesis tests based on Matrix data.
The newest version!
package se.alipsa.groovy.stats
import se.alipsa.groovy.matrix.Matrix
class Sampler {
static List split(Matrix data, BigDecimal ratio) {
if (ratio > 1.0 || ratio < 0) {
throw new IllegalArgumentException("Ratio must be a number between 0 and 1")
}
int size = (int) (data.rowCount() * ratio)
def samples = (0..data.rowCount()-1).collect()
samples.shuffle()
def train = samples.take(size)
def test = samples.takeRight(data.rowCount()-size)
Matrix trainMatrix = Matrix.builder()
.matrixName(data.matrixName + '-train')
.columnNames(data.columnNames())
.rows(data.rows(train))
.types(data.types())
.build()
Matrix testMatrix =
Matrix.builder()
.matrixName(data.matrixName + '-test')
.columnNames(data.columnNames())
.rows(data.rows(test))
.types(data.types())
.build()
return [trainMatrix, testMatrix]
}
}