2014-05-02 20:50:14 -04:00
|
|
|
|
package utilities
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2014-05-02 22:38:15 -04:00
|
|
|
|
mat "github.com/gonum/matrix/mat64"
|
2014-05-02 20:50:14 -04:00
|
|
|
|
"math/rand"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2014-05-02 22:38:15 -04:00
|
|
|
|
func shuffleMatrix(dataset *mat.Dense, numGen *rand.Rand) *mat.Dense {
|
|
|
|
|
shuffledSet := mat.DenseCopyOf(dataset)
|
|
|
|
|
rowCount, colCount := shuffledSet.Dims()
|
|
|
|
|
temp := make([]float64, colCount)
|
2014-05-02 20:50:14 -04:00
|
|
|
|
|
2014-05-02 22:38:15 -04:00
|
|
|
|
// Fisher–Yates shuffle
|
|
|
|
|
for i := 0; i < rowCount; i++ {
|
2014-05-02 20:50:14 -04:00
|
|
|
|
j := numGen.Intn(i + 1)
|
2014-05-02 22:38:15 -04:00
|
|
|
|
if j != i {
|
|
|
|
|
// Make a "hard" copy to avoid pointer craziness.
|
|
|
|
|
copy(temp, shuffledSet.RowView(i))
|
|
|
|
|
shuffledSet.SetRow(i, shuffledSet.RowView(j))
|
|
|
|
|
shuffledSet.SetRow(j, temp)
|
|
|
|
|
}
|
2014-05-02 20:50:14 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return shuffledSet
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TrainTestSplit splits input DenseMatrix into subsets for testing.
|
|
|
|
|
// The function expects a test size number (int) or percentage (float64), and a random state or nil to get "random" shuffle.
|
|
|
|
|
// It returns a list containing the train-test split and an error status.
|
2014-05-02 22:38:15 -04:00
|
|
|
|
func TrainTestSplit(size interface{}, randomState interface{}, datasets ...*mat.Dense) ([]*mat.Dense, error) {
|
2014-05-02 20:50:14 -04:00
|
|
|
|
// Get number of instances (rows).
|
2014-05-02 22:38:15 -04:00
|
|
|
|
instanceCount, _ := datasets[0].Dims()
|
2014-05-02 20:50:14 -04:00
|
|
|
|
|
|
|
|
|
// Input should be one or two matrices.
|
|
|
|
|
dataCount := len(datasets)
|
|
|
|
|
if dataCount > 2 {
|
|
|
|
|
return nil, fmt.Errorf("Expected 1 or 2 datasets, got %d\n", dataCount)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if dataCount == 2 {
|
|
|
|
|
// Test for consistency.
|
2014-05-02 22:38:15 -04:00
|
|
|
|
labelCount, labelFeatures := datasets[1].Dims()
|
|
|
|
|
if labelCount != instanceCount {
|
2014-05-02 20:50:14 -04:00
|
|
|
|
return nil, fmt.Errorf("Data and labels must have the same number of instances")
|
2014-05-02 22:38:15 -04:00
|
|
|
|
} else if labelFeatures != 1 {
|
2014-05-02 20:50:14 -04:00
|
|
|
|
return nil, fmt.Errorf("Label matrix must have single feature")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var trainSize, testSize int
|
|
|
|
|
switch size := size.(type) {
|
|
|
|
|
// If size is an integer, treat it as the test data instance count.
|
|
|
|
|
case int:
|
|
|
|
|
trainSize = instanceCount - size
|
|
|
|
|
testSize = size
|
|
|
|
|
case float64:
|
|
|
|
|
// If size is a float, treat it as a percentage of the instances to be allocated to the test set.
|
|
|
|
|
trainSize = int(float64(instanceCount)*(1-size) + 0.5)
|
|
|
|
|
testSize = int(float64(instanceCount)*size + 0.5)
|
|
|
|
|
default:
|
|
|
|
|
return nil, fmt.Errorf("Expected a test instance count (int) or percentage (float64)")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create a deterministic shuffle, or a "random" one based on current time.
|
|
|
|
|
var randSource rand.Source
|
|
|
|
|
if seed, ok := randomState.(int); ok {
|
|
|
|
|
randSource = rand.NewSource(int64(seed))
|
|
|
|
|
} else {
|
|
|
|
|
randSource = rand.NewSource(time.Now().Unix())
|
|
|
|
|
}
|
|
|
|
|
numGen := rand.New(randSource)
|
|
|
|
|
|
|
|
|
|
// Return slice will hold training and test data and optional labels matrix.
|
2014-05-02 22:38:15 -04:00
|
|
|
|
var returnDatasets []*mat.Dense
|
2014-05-02 20:50:14 -04:00
|
|
|
|
|
|
|
|
|
for _, dataset := range datasets {
|
2014-05-02 22:38:15 -04:00
|
|
|
|
_, featureCount := dataset.Dims()
|
|
|
|
|
|
2014-05-02 20:50:14 -04:00
|
|
|
|
tempMatrix := shuffleMatrix(dataset, numGen)
|
2014-05-02 22:38:15 -04:00
|
|
|
|
|
2014-05-02 20:50:14 -04:00
|
|
|
|
// Features count is different on data and labels.
|
2014-05-02 22:38:15 -04:00
|
|
|
|
returnDatasets = append(returnDatasets, mat.NewDense(trainSize, featureCount, tempMatrix.RawMatrix().Data[:trainSize*featureCount]))
|
|
|
|
|
returnDatasets = append(returnDatasets, mat.NewDense(testSize, featureCount, tempMatrix.RawMatrix().Data[trainSize*featureCount:]))
|
2014-05-02 20:50:14 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return returnDatasets, nil
|
|
|
|
|
}
|