-
Notifications
You must be signed in to change notification settings - Fork 5
/
preprocess_binarize.go
115 lines (91 loc) · 3.4 KB
/
preprocess_binarize.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package mlpack
/*
#cgo CFLAGS: -I./capi -Wall
#cgo LDFLAGS: -L. -lmlpack_go_preprocess_binarize
#include <capi/preprocess_binarize.h>
#include <stdlib.h>
*/
import "C"
import "gonum.org/v1/gonum/mat"
type PreprocessBinarizeOptionalParam struct {
Dimension int
Threshold float64
Verbose bool
}
func PreprocessBinarizeOptions() *PreprocessBinarizeOptionalParam {
return &PreprocessBinarizeOptionalParam{
Dimension: 0,
Threshold: 0,
Verbose: false,
}
}
/*
This utility takes a dataset and binarizes the variables into either 0 or 1
given threshold. User can apply binarization on a dimension or the whole
dataset. The dimension to apply binarization to can be specified using the
"Dimension" parameter; if left unspecified, every dimension will be binarized.
The threshold for binarization can also be specified with the "Threshold"
parameter; the default threshold is 0.0.
The binarized matrix may be saved with the "Output" output parameter.
For example, if we want to set all variables greater than 5 in the dataset X
to 1 and variables less than or equal to 5.0 to 0, and save the result to Y,
we could run
// Initialize optional parameters for PreprocessBinarize().
param := mlpack.PreprocessBinarizeOptions()
param.Threshold = 5
Y := mlpack.PreprocessBinarize(X, param)
But if we want to apply this to only the first (0th) dimension of X, we could
instead run
// Initialize optional parameters for PreprocessBinarize().
param := mlpack.PreprocessBinarizeOptions()
param.Threshold = 5
param.Dimension = 0
Y := mlpack.PreprocessBinarize(X, param)
Input parameters:
- input (mat.Dense): Input data matrix.
- Dimension (int): Dimension to apply the binarization. If not set, the
program will binarize every dimension by default. Default value 0.
- Threshold (float64): Threshold to be applied for binarization. If not
set, the threshold defaults to 0.0. Default value 0.
- Verbose (bool): Display informational messages and the full list of
parameters and timers at the end of execution.
Output parameters:
- output (mat.Dense): Matrix in which to save the output.
*/
func PreprocessBinarize(input *mat.Dense, param *PreprocessBinarizeOptionalParam) (*mat.Dense) {
params := getParams("preprocess_binarize")
timers := getTimers()
disableBacktrace()
disableVerbose()
// Detect if the parameter was passed; set if so.
gonumToArmaMat(params, "input", input, false)
setPassed(params, "input")
// Detect if the parameter was passed; set if so.
if param.Dimension != 0 {
setParamInt(params, "dimension", param.Dimension)
setPassed(params, "dimension")
}
// Detect if the parameter was passed; set if so.
if param.Threshold != 0 {
setParamDouble(params, "threshold", param.Threshold)
setPassed(params, "threshold")
}
// Detect if the parameter was passed; set if so.
if param.Verbose != false {
setParamBool(params, "verbose", param.Verbose)
setPassed(params, "verbose")
enableVerbose()
}
// Mark all output options as passed.
setPassed(params, "output")
// Call the mlpack program.
C.mlpackPreprocessBinarize(params.mem, timers.mem)
// Initialize result variable and get output.
var outputPtr mlpackArma
output := outputPtr.armaToGonumMat(params, "output")
// Clean memory.
cleanParams(params)
cleanTimers(timers)
// Return output(s).
return output
}