2014-05-05 21:47:56 +08:00
|
|
|
package linear_models
|
|
|
|
|
2014-07-02 15:48:35 +01:00
|
|
|
import (
|
2014-08-22 09:18:01 +00:00
|
|
|
"errors"
|
2014-07-02 15:48:35 +01:00
|
|
|
"fmt"
|
2020-09-06 10:01:07 +01:00
|
|
|
|
2014-08-22 07:21:24 +00:00
|
|
|
"github.com/sjwhitworth/golearn/base"
|
2014-07-02 15:48:35 +01:00
|
|
|
)
|
2014-05-05 21:47:56 +08:00
|
|
|
|
|
|
|
type LogisticRegression struct {
|
|
|
|
param *Parameter
|
|
|
|
model *Model
|
|
|
|
}
|
|
|
|
|
2014-08-22 09:18:01 +00:00
|
|
|
func NewLogisticRegression(penalty string, C float64, eps float64) (*LogisticRegression, error) {
|
2014-05-05 21:47:56 +08:00
|
|
|
solver_type := 0
|
|
|
|
if penalty == "l2" {
|
|
|
|
solver_type = L2R_LR
|
|
|
|
} else if penalty == "l1" {
|
|
|
|
solver_type = L1R_LR
|
|
|
|
} else {
|
2014-08-22 09:18:01 +00:00
|
|
|
return nil, errors.New(fmt.Sprintf("Invalid penalty '%s'", penalty))
|
2014-05-05 21:47:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
lr := LogisticRegression{}
|
|
|
|
lr.param = NewParameter(solver_type, C, eps)
|
|
|
|
lr.model = nil
|
2014-08-22 09:18:01 +00:00
|
|
|
return &lr, nil
|
2014-05-05 21:47:56 +08:00
|
|
|
}
|
|
|
|
|
2014-10-04 17:57:39 +01:00
|
|
|
func (lr *LogisticRegression) Fit(X base.FixedDataGrid) error {
|
2014-07-02 15:48:35 +01:00
|
|
|
problemVec := convertInstancesToProblemVec(X)
|
|
|
|
labelVec := convertInstancesToLabelVec(X)
|
|
|
|
prob := NewProblem(problemVec, labelVec, 0)
|
2014-05-05 21:47:56 +08:00
|
|
|
lr.model = Train(prob, lr.param)
|
2014-10-04 17:57:39 +01:00
|
|
|
return nil
|
2014-05-05 21:47:56 +08:00
|
|
|
}
|
|
|
|
|
2014-10-04 17:57:39 +01:00
|
|
|
func (lr *LogisticRegression) Predict(X base.FixedDataGrid) (base.FixedDataGrid, error) {
|
2014-08-02 16:22:15 +01:00
|
|
|
|
|
|
|
// Only support 1 class Attribute
|
|
|
|
classAttrs := X.AllClassAttributes()
|
|
|
|
if len(classAttrs) != 1 {
|
|
|
|
panic(fmt.Sprintf("%d Wrong number of classes", len(classAttrs)))
|
2014-05-05 21:47:56 +08:00
|
|
|
}
|
2014-08-02 16:22:15 +01:00
|
|
|
// Generate return structure
|
|
|
|
ret := base.GeneratePredictionVector(X)
|
2014-08-03 12:31:26 +01:00
|
|
|
classAttrSpecs := base.ResolveAttributes(ret, classAttrs)
|
2014-08-02 16:22:15 +01:00
|
|
|
// Retrieve numeric non-class Attributes
|
|
|
|
numericAttrs := base.NonClassFloatAttributes(X)
|
2014-08-03 12:31:26 +01:00
|
|
|
numericAttrSpecs := base.ResolveAttributes(X, numericAttrs)
|
2014-08-02 16:22:15 +01:00
|
|
|
|
|
|
|
// 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
|
|
|
|
})
|
|
|
|
|
2014-10-04 17:57:39 +01:00
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lr *LogisticRegression) String() string {
|
|
|
|
return "LogisticRegression"
|
2014-05-05 21:47:56 +08:00
|
|
|
}
|