-
Notifications
You must be signed in to change notification settings - Fork 73
/
doc.go
208 lines (168 loc) · 6.21 KB
/
doc.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
Package leaves is pure Go implemetation of prediction part for GBRT (Gradient
Boosting Regression Trees) models from popular frameworks.
General
All loaded models exibit the same interface from `Ensemble struct`. One can
use method `Name` to get string representation of model origin. Possible name
values are "lightgbm.gbdt", "lightgbm.rf", "xgboost.gbtree", "xgboost.gblinear", etc.
LightGBM model
Example: binary classification
build_breast_cancer_model.py:
import lightgbm as lgb
import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
X, y = datasets.load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
n_estimators = 30
d_train = lgb.Dataset(X_train, label=y_train)
params = {
'boosting_type': 'gbdt',
'objective': 'binary',
}
clf = lgb.train(params, d_train, n_estimators)
y_pred = clf.predict(X_test)
y_pred_raw = clf.predict(X_test, raw_score=True)
clf.save_model('lg_breast_cancer.model') # save the model in txt format
np.savetxt('lg_breast_cancer_true_predictions.txt', y_pred)
np.savetxt('lg_breast_cancer_true_predictions_raw.txt', y_pred_raw)
np.savetxt('breast_cancer_test.tsv', X_test, delimiter='\t')
predict_breast_cancer_model.go:
package main
import (
"fmt"
"github.com/dmitryikh/leaves"
"github.com/dmitryikh/leaves/mat"
"github.com/dmitryikh/leaves/util"
)
func main() {
// loading test data
test, err := mat.DenseMatFromCsvFile("breast_cancer_test.tsv", 0, false, "\t", 0.0)
if err != nil {
panic(err)
}
// loading model
model, err := leaves.LGEnsembleFromFile("lg_breast_cancer.model", true)
if err != nil {
panic(err)
}
fmt.Printf("Name: %s\n", model.Name())
fmt.Printf("NFeatures: %d\n", model.NFeatures())
fmt.Printf("NOutputGroups: %d\n", model.NOutputGroups())
fmt.Printf("NEstimators: %d\n", model.NEstimators())
fmt.Printf("Transformation: %s\n", model.Transformation().Name())
// loading true predictions as DenseMat
truePredictions, err := mat.DenseMatFromCsvFile("lg_breast_cancer_true_predictions.txt", 0, false, "\t", 0.0)
if err != nil {
panic(err)
}
truePredictionsRaw, err := mat.DenseMatFromCsvFile("lg_breast_cancer_true_predictions_raw.txt", 0, false, "\t", 0.0)
if err != nil {
panic(err)
}
// preallocate slice to store model predictions
predictions := make([]float64, test.Rows*model.NOutputGroups())
// do predictions
model.PredictDense(test.Values, test.Rows, test.Cols, predictions, 0, 1)
// compare results
const tolerance = 1e-6
if err := util.AlmostEqualFloat64Slices(truePredictions.Values, predictions, tolerance); err != nil {
panic(fmt.Errorf("different predictions: %s", err.Error()))
}
// compare raw predictions (before transformation function)
rawModel := model.EnsembleWithRawPredictions()
rawModel.PredictDense(test.Values, test.Rows, test.Cols, predictions, 0, 1)
if err := util.AlmostEqualFloat64Slices(truePredictionsRaw.Values, predictions, tolerance); err != nil {
panic(fmt.Errorf("different raw predictions: %s", err.Error()))
}
fmt.Println("Predictions the same!")
}
Output:
Name: lightgbm.gbdt
NFeatures: 30
NOutputGroups: 1
NEstimators: 30
Transformation: logistic
Predictions the same!
XGBoost Model
example: Multiclass Classification
build_iris_model.py
import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
import xgboost as xgb
X, y = datasets.load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
xg_train = xgb.DMatrix(X_train, label=y_train)
xg_test = xgb.DMatrix(X_test, label=y_test)
params = {
'objective': 'multi:softmax',
'num_class': 3,
}
n_estimators = 5
clf = xgb.train(params, xg_train, n_estimators)
# use output_margin=True because of `leaves` predictions are raw scores (before
# transformation function)
y_pred = clf.predict(xg_test, output_margin=True)
# save the model in binary format
clf.save_model('xg_iris.model')
np.savetxt('xg_iris_true_predictions.txt', y_pred, delimiter='\t')
datasets.dump_svmlight_file(X_test, y_test, 'iris_test.libsvm')
predict_iris_model.go:
package main
import (
"fmt"
"github.com/dmitryikh/leaves"
"github.com/dmitryikh/leaves/mat"
"github.com/dmitryikh/leaves/util"
)
func main() {
// loading test data
csr, err := mat.CSRMatFromLibsvmFile("iris_test.libsvm", 0, true)
if err != nil {
panic(err)
}
// loading model
model, err := leaves.XGEnsembleFromFile("xg_iris.model", false)
if err != nil {
panic(err)
}
fmt.Printf("Name: %s\n", model.Name())
fmt.Printf("NFeatures: %d\n", model.NFeatures())
fmt.Printf("NOutputGroups: %d\n", model.NOutputGroups())
fmt.Printf("NEstimators: %d\n", model.NEstimators())
// loading true predictions as DenseMat
truePredictions, err := mat.DenseMatFromCsvFile("xg_iris_true_predictions.txt", 0, false, "\t", 0.0)
if err != nil {
panic(err)
}
// preallocate slice to store model predictions
predictions := make([]float64, csr.Rows()*model.NOutputGroups())
// do predictions
model.PredictCSR(csr.RowHeaders, csr.ColIndexes, csr.Values, predictions, 0, 1)
// compare results
const tolerance = 1e-6
// compare results. Count number of mismatched values beacase of floating point
// tolerances in decision rule
mismatch, err := util.NumMismatchedFloat64Slices(truePredictions.Values, predictions, tolerance)
if err != nil {
panic(err)
}
if mismatch > 2 {
panic(fmt.Errorf("mismatched more than %d predictions", mismatch))
}
fmt.Printf("Predictions the same! (mismatch = %d)\n", mismatch)
}
Output:
Name: xgboost.gbtree
NFeatures: 4
NOutputGroups: 3
NEstimators: 5
Predictions the same! (mismatch = 0)
Notes on XGBoost DART support
Please note that one must not provide nEstimators = 0 when predict with DART models from xgboost. For more details see xgboost's documentation.
Notes on LightGBM DART support
Models trained with 'boosting_type': 'dart' options can be loaded with func `leaves.LGEnsembleFromFile`.
But the name of the model (given by `Name()` method) will be 'lightgbm.gbdt', because LightGBM model format doesn't distinguish 'gbdt' and 'dart' models.
*/
package leaves