mirror of
https://github.com/sjwhitworth/golearn.git
synced 2025-04-28 13:48:56 +08:00
112 lines
3.1 KiB
Go
112 lines
3.1 KiB
Go
package linear_models
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/sjwhitworth/golearn/base"
|
|
)
|
|
|
|
type LogisticRegression struct {
|
|
param *Parameter
|
|
model *Model
|
|
}
|
|
|
|
func NewLogisticRegression(penalty string, C float64, eps float64) *LogisticRegression {
|
|
solver_type := 0
|
|
if penalty == "l2" {
|
|
solver_type = L2R_LR
|
|
} else if penalty == "l1" {
|
|
solver_type = L1R_LR
|
|
} else {
|
|
fmt.Println("Invalid penalty")
|
|
return nil
|
|
}
|
|
|
|
lr := LogisticRegression{}
|
|
lr.param = NewParameter(solver_type, C, eps)
|
|
lr.model = nil
|
|
return &lr
|
|
}
|
|
|
|
func convertInstancesToProblemVec(X base.FixedDataGrid) [][]float64 {
|
|
// Allocate problem array
|
|
_, rows := X.Size()
|
|
problemVec := make([][]float64, rows)
|
|
|
|
// Retrieve numeric non-class Attributes
|
|
numericAttrs := base.NonClassFloatAttributes(X)
|
|
numericAttrSpecs := base.ResolveAttributes(X, numericAttrs)
|
|
|
|
// Convert each row
|
|
X.MapOverRows(numericAttrSpecs, func(row [][]byte, rowNo int) (bool, error) {
|
|
// Allocate a new row
|
|
probRow := make([]float64, len(numericAttrSpecs))
|
|
// Read out the row
|
|
for i, _ := range numericAttrSpecs {
|
|
probRow[i] = base.UnpackBytesToFloat(row[i])
|
|
}
|
|
// Add the row
|
|
problemVec[rowNo] = probRow
|
|
return true, nil
|
|
})
|
|
return problemVec
|
|
}
|
|
|
|
func convertInstancesToLabelVec(X base.FixedDataGrid) []float64 {
|
|
// Get the class Attributes
|
|
classAttrs := X.AllClassAttributes()
|
|
// Only support 1 class Attribute
|
|
if len(classAttrs) != 1 {
|
|
panic(fmt.Sprintf("%d ClassAttributes (1 expected)", len(classAttrs)))
|
|
}
|
|
// ClassAttribute must be numeric
|
|
if _, ok := classAttrs[0].(*base.FloatAttribute); !ok {
|
|
panic(fmt.Sprintf("%s: ClassAttribute must be a FloatAttribute", classAttrs[0]))
|
|
}
|
|
// Allocate return structure
|
|
_, rows := X.Size()
|
|
labelVec := make([]float64, rows)
|
|
// Resolve class Attribute specification
|
|
classAttrSpecs := base.ResolveAttributes(X, classAttrs)
|
|
X.MapOverRows(classAttrSpecs, func(row [][]byte, rowNo int) (bool, error) {
|
|
labelVec[rowNo] = base.UnpackBytesToFloat(row[0])
|
|
return true, nil
|
|
})
|
|
return labelVec
|
|
}
|
|
|
|
func (lr *LogisticRegression) Fit(X base.FixedDataGrid) {
|
|
problemVec := convertInstancesToProblemVec(X)
|
|
labelVec := convertInstancesToLabelVec(X)
|
|
prob := NewProblem(problemVec, labelVec, 0)
|
|
lr.model = Train(prob, lr.param)
|
|
}
|
|
|
|
func (lr *LogisticRegression) Predict(X base.FixedDataGrid) base.FixedDataGrid {
|
|
|
|
// Only support 1 class Attribute
|
|
classAttrs := X.AllClassAttributes()
|
|
if len(classAttrs) != 1 {
|
|
panic(fmt.Sprintf("%d Wrong number of classes", len(classAttrs)))
|
|
}
|
|
// Generate return structure
|
|
ret := base.GeneratePredictionVector(X)
|
|
classAttrSpecs := base.ResolveAttributes(ret, classAttrs)
|
|
// Retrieve numeric non-class Attributes
|
|
numericAttrs := base.NonClassFloatAttributes(X)
|
|
numericAttrSpecs := base.ResolveAttributes(X, numericAttrs)
|
|
|
|
// Allocate row storage
|
|
row := make([]float64, len(numericAttrSpecs))
|
|
X.MapOverRows(numericAttrSpecs, func(rowBytes [][]byte, rowNo int) (bool, error) {
|
|
for i, r := range rowBytes {
|
|
row[i] = base.UnpackBytesToFloat(r)
|
|
}
|
|
val := Predict(lr.model, row)
|
|
vals := base.PackFloatToBytes(val)
|
|
ret.Set(classAttrSpecs[0], rowNo, vals)
|
|
return true, nil
|
|
})
|
|
|
|
return ret
|
|
}
|