-
Notifications
You must be signed in to change notification settings - Fork 69
/
crosses.go
65 lines (56 loc) · 1.45 KB
/
crosses.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package regression
import (
"math"
"strconv"
)
type featureCross interface {
Calculate([]float64) []float64 //must return the same number of features each run
ExtendNames(map[int]string, int) int
}
type functionalCross struct {
functionName string
boundVars []int
crossFn func([]float64) []float64
}
func (c *functionalCross) Calculate(input []float64) []float64 {
return c.crossFn(input)
}
func (c *functionalCross) ExtendNames(input map[int]string, initialSize int) int {
for i, varIndex := range c.boundVars {
if input[varIndex] != "" {
input[initialSize+i] = "(" + input[varIndex] + ")" + c.functionName
}
}
return len(c.boundVars)
}
// Feature cross based on computing the power of an input.
func PowCross(i int, power float64) featureCross {
return &functionalCross{
functionName: "^" + strconv.FormatFloat(power, 'f', -1, 64),
boundVars: []int{i},
crossFn: func(vars []float64) []float64 {
return []float64{math.Pow(vars[i], power)}
},
}
}
// Feature cross based on the multiplication of multiple inputs.
func MultiplierCross(vars ...int) featureCross {
name := ""
for i, v := range vars {
name += strconv.Itoa(v)
if i < (len(vars) - 1) {
name += "*"
}
}
return &functionalCross{
functionName: name,
boundVars: vars,
crossFn: func(input []float64) []float64 {
var output float64 = 1
for _, variableIndex := range vars {
output *= input[variableIndex]
}
return []float64{output}
},
}
}