utils

package
v0.0.0-...-bc599d9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 19, 2024 License: LGPL-2.1 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddToCol

func AddToCol(D *mat.Dense, s []float64, col int)

adds the i-th number in s to the ith row to the col column of D. s must have as many elements as D has rows.

func ArgSort

func ArgSort(a []float64) ([]int, []float64)

Returns the indexes that would sort the given slice and also the sorted slice. It doesn't touch the original slice!

func CrossValidationSamples

func CrossValidationSamples(data *DataBunch, nfold int, usecopies ...bool) (int, func() (*DataBunch, *DataBunch), error)

Returns an int "n", and a function that the first n times is called, will return a "folded" training set and test set. After the function has been called n times, it will return nil, nil. If the fold requested is too large for the dataset, an error will be returned and a smaller fold will be used. If not nfold is requested, 5-fold will be used.

func DenseCol

func DenseCol(D *mat.Dense, col int) *mat.Dense

Returns the col-th column of the D matrix as a row Dense matrix

func JSONTree

func JSONTree(t JTree, ids ...*idGiver) ([][]byte, uint, error)

takes a JSON-able tree and returns it as a slice of json-serialized []byte, where each element in the slice is a node.

func MemArgSort

func MemArgSort(target []float64, tmpint []int, tmpval []float64) ([]int, []float64)

Returns the indexes that would sort the given slice and also the sorted slice. It doesn't touch the original slice!

func Normalization

func Normalization(p, probs []float64) []float64

puts in probs the normalization output for the inputs in p. allocates a new slice if probs is nil.

func NormalizationDense

func NormalizationDense(O, D *mat.Dense) *mat.Dense

puts in D the normalization output for the inputs in O. allocates a new matrix if D is nil.

func PrintDenseMatrix

func PrintDenseMatrix(m *mat.Dense) string

Produces a printable version of m

func SampleMatrix

func SampleMatrix(m [][]float64, rowIndexes, colIndexes []int) [][]float64

takes a sub-matrix of m, consisting in its rows with indexes present in rowIndexes and the columns present in colIndexes, inthe order in which they appear in the 2 int slices.

func SampleSlice

func SampleSlice(s []float64, indexes []int) []float64

Produces a slice that contains each ith element (zero-based) in the slice s, where i is an element in indexes in the order in which they appear in indexes.

func SoftMax

func SoftMax(p, probs []float64) []float64

puts in probs the softmax output for the inputs in p. allocates a new matrix if probs is nil.

func SoftMaxDense

func SoftMaxDense(O, D *mat.Dense) *mat.Dense

puts in D the softmax output for the inputs in O. allocates a new matrix if D is nil.

func ToOnes

func ToOnes(m *mat.Dense)

Sets all entries of m to 1.0

func TransposeFloats

func TransposeFloats(m [][]float64) [][]float64

Transposes the matrix represented by the [][]m slice of slices.

Types

type DataBunch

type DataBunch struct {
	Data        [][]float64
	Keys        []string
	Labels      []int
	FloatLabels []float64 //for now we keep both
}

A simple structure for data Keys are the feature names, Lables are the classification of each Data vector, if available

func DataBunchFromCSVFile

func DataBunchFromCSVFile(filename string, hasHeader, hasLabels bool, separator ...rune) (*DataBunch, error)

reads a libSVM-formatted file and returns a DataBunch. It's a pretty poor reader right now as it doesn't support sparse-libSVM files.

func DataBunchFromLibSVMFile

func DataBunchFromLibSVMFile(filename string, hasHeader ...bool) (*DataBunch, error)

reads a libSVM-formatted file and returns a DataBunch. It's a pretty poor reader right now as it doesn't support sparse-libSVM files. The file has to have missing data points explitly (say, set to 0) and it needs to have labels.

func ParseCSVFromReader

func ParseCSVFromReader(r io.Reader, hasHeader, hasLabels bool, sep rune) (*DataBunch, error)

func ParseLibSVMFromReader

func ParseLibSVMFromReader(r io.Reader, hasHeader bool) (*DataBunch, error)

func (*DataBunch) CSV

func (D *DataBunch) CSV(separator ...string) string

returns the data in CSV format, using whatever string is given as a separator (comma if no string is given)

func (*DataBunch) LibSVM

func (D *DataBunch) LibSVM() string

returns the data in libSVM format not very good as it doesn't omit the zero-valued data, but I'll get there.

func (*DataBunch) OHEKeys

func (D *DataBunch) OHEKeys() (*mat.Dense, []string)

returns a one-hot-encoded representation of the keys of the data bunch

func (*DataBunch) OHELabels

func (D *DataBunch) OHELabels() (*mat.Dense, []int)

Returns a one-hot-encoded representation of the labels of the data bunch.

func (*DataBunch) String

func (D *DataBunch) String() string

Returns a string representation of the data bunch

type Encodeable

type Encodeable interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 |
		~string
}

type JSONNode

type JSONNode struct {
	Id                uint
	Nsamples          int
	Samples           []int
	SplitFeatureIndex int
	BestScoreSoFar    float64
	Leaf              bool
	Threshold         float64
	Leftid            uint
	Rightid           uint
	Branches          int
	Value             float64
	XGB               bool
}

Structure for serializing a tree node.

func (*JSONNode) String

func (j *JSONNode) String() string

type JTree

type JTree interface {
	JNode(uint, ...bool) *JSONNode
	//sets the left or right node to the given JTree and return it
	//if given nil, just returns the current value for the node
	Leftf(JTree) JTree
	Rightf(JTree) JTree
	Leaf() bool
}

A tree that can "pack" itself as a JSONNode

func UnJSONTree

func UnJSONTree(str string, r *bufio.Reader, creator func(*JSONNode) JTree) (JTree, error)

takes a bufio reader containing a json-serialized representation of a Tree, plus a tree creator function, and returns the created JTree

type LossFunc

type LossFunc interface {
	Loss(*mat.Dense, *mat.Dense, *mat.Dense) float64
	Name() string
	NegGradients(*mat.Dense, *mat.Dense, *mat.Dense) *mat.Dense
	Gradients(*mat.Dense, *mat.Dense, *mat.Dense) *mat.Dense
	Hessian(*mat.Dense, *mat.Dense) *mat.Dense
}

Interface for loss functions used in the package.

type MSELoss

type MSELoss struct {
}

the error used in regular gradient boosting

func (*MSELoss) Gradients

func (m *MSELoss) Gradients(yohelabels, probabilities, results *mat.Dense) *mat.Dense

Returns the results matrix filled with the gradients. if a nil results is given, it will allocate a new matrix and return it.

func (*MSELoss) Hessian

func (m *MSELoss) Hessian(probabilities, hessian *mat.Dense) *mat.Dense

Returns the results matrix filled with the Hessian. if a nil results is given, it will allocate a new matrix and return it.

func (*MSELoss) Loss

func (mse *MSELoss) Loss(y, pred, loss *mat.Dense) float64

func (*MSELoss) Name

func (m *MSELoss) Name() string

func (*MSELoss) NegGradients

func (m *MSELoss) NegGradients(yohelabels, probabilities, results *mat.Dense) *mat.Dense

Returns the results matrix filled witht he negative gradients. if a nil results is given, it will allocate a new matrix and return it.

type SQErrLoss

type SQErrLoss struct {
}

Square error

func (*SQErrLoss) Gradients

func (m *SQErrLoss) Gradients(yohelabels, pred, results *mat.Dense) *mat.Dense

Returns the results matrix filled with the gradients. if a nil results is given, it will allocate a new matrix and return it.

func (*SQErrLoss) Hessian

func (m *SQErrLoss) Hessian(probabilities, hessian *mat.Dense) *mat.Dense

Returns the results matrix filled with the Hessian. if a nil results is given, it will allocate a new matrix and return it.

func (*SQErrLoss) Loss

func (sq *SQErrLoss) Loss(y, pred, loss *mat.Dense) float64

func (*SQErrLoss) Name

func (sq *SQErrLoss) Name() string

func (*SQErrLoss) NegGradients

func (m *SQErrLoss) NegGradients(yohelabels, pred, results *mat.Dense) *mat.Dense

Returns the results matrix filled witht he negative gradients. if a nil results is given, it will allocate a new matrix and return it.

type Slice

type Slice struct {
	sort.Float64Slice
	Idx []int
}

func NewSlice

func NewSlice(n []float64) *Slice

func (Slice) Swap

func (s Slice) Swap(i, j int)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL