-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathtest_.py
266 lines (236 loc) · 8.96 KB
/
test_.py
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# coding: utf-8
import ctypes
from pathlib import Path
from platform import system
import numpy as np
from scipy import sparse
try:
from lightgbm.basic import _LIB as LIB
except ModuleNotFoundError:
print("Could not import lightgbm Python-package, looking for lib_lightgbm at the repo root")
if system() in ("Windows", "Microsoft"):
lib_file = Path(__file__).absolute().parents[2] / "Release" / "lib_lightgbm.dll"
else:
lib_file = Path(__file__).absolute().parents[2] / "lib_lightgbm.so"
LIB = ctypes.cdll.LoadLibrary(lib_file)
LIB.LGBM_GetLastError.restype = ctypes.c_char_p
dtype_float32 = 0
dtype_float64 = 1
dtype_int32 = 2
dtype_int64 = 3
def c_str(string):
return ctypes.c_char_p(str(string).encode("utf-8"))
def load_from_file(filename, reference):
ref = None
if reference is not None:
ref = reference
handle = ctypes.c_void_p()
LIB.LGBM_DatasetCreateFromFile(c_str(str(filename)), c_str("max_bin=15"), ref, ctypes.byref(handle))
print(LIB.LGBM_GetLastError())
num_data = ctypes.c_int(0)
LIB.LGBM_DatasetGetNumData(handle, ctypes.byref(num_data))
num_feature = ctypes.c_int(0)
LIB.LGBM_DatasetGetNumFeature(handle, ctypes.byref(num_feature))
print(f"#data: {num_data.value} #feature: {num_feature.value}")
return handle
def save_to_binary(handle, filename):
LIB.LGBM_DatasetSaveBinary(handle, c_str(filename))
def load_from_csr(filename, reference):
data = np.loadtxt(str(filename), dtype=np.float64)
csr = sparse.csr_matrix(data[:, 1:])
label = data[:, 0].astype(np.float32)
handle = ctypes.c_void_p()
ref = None
if reference is not None:
ref = reference
LIB.LGBM_DatasetCreateFromCSR(
csr.indptr.ctypes.data_as(ctypes.POINTER(ctypes.c_int32)),
ctypes.c_int(dtype_int32),
csr.indices.ctypes.data_as(ctypes.POINTER(ctypes.c_int32)),
csr.data.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
ctypes.c_int(dtype_float64),
ctypes.c_int64(len(csr.indptr)),
ctypes.c_int64(len(csr.data)),
ctypes.c_int64(csr.shape[1]),
c_str("max_bin=15"),
ref,
ctypes.byref(handle),
)
num_data = ctypes.c_int(0)
LIB.LGBM_DatasetGetNumData(handle, ctypes.byref(num_data))
num_feature = ctypes.c_int(0)
LIB.LGBM_DatasetGetNumFeature(handle, ctypes.byref(num_feature))
LIB.LGBM_DatasetSetField(
handle,
c_str("label"),
label.ctypes.data_as(ctypes.POINTER(ctypes.c_float)),
ctypes.c_int(len(label)),
ctypes.c_int(dtype_float32),
)
print(f"#data: {num_data.value} #feature: {num_feature.value}")
return handle
def load_from_csc(filename, reference):
data = np.loadtxt(str(filename), dtype=np.float64)
csc = sparse.csc_matrix(data[:, 1:])
label = data[:, 0].astype(np.float32)
handle = ctypes.c_void_p()
ref = None
if reference is not None:
ref = reference
LIB.LGBM_DatasetCreateFromCSC(
csc.indptr.ctypes.data_as(ctypes.POINTER(ctypes.c_int32)),
ctypes.c_int(dtype_int32),
csc.indices.ctypes.data_as(ctypes.POINTER(ctypes.c_int32)),
csc.data.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
ctypes.c_int(dtype_float64),
ctypes.c_int64(len(csc.indptr)),
ctypes.c_int64(len(csc.data)),
ctypes.c_int64(csc.shape[0]),
c_str("max_bin=15"),
ref,
ctypes.byref(handle),
)
num_data = ctypes.c_int(0)
LIB.LGBM_DatasetGetNumData(handle, ctypes.byref(num_data))
num_feature = ctypes.c_int(0)
LIB.LGBM_DatasetGetNumFeature(handle, ctypes.byref(num_feature))
LIB.LGBM_DatasetSetField(
handle,
c_str("label"),
label.ctypes.data_as(ctypes.POINTER(ctypes.c_float)),
ctypes.c_int(len(label)),
ctypes.c_int(dtype_float32),
)
print(f"#data: {num_data.value} #feature: {num_feature.value}")
return handle
def load_from_mat(filename, reference):
mat = np.loadtxt(str(filename), dtype=np.float64)
label = mat[:, 0].astype(np.float32)
mat = mat[:, 1:]
data = np.asarray(mat.reshape(mat.size), dtype=np.float64)
handle = ctypes.c_void_p()
ref = None
if reference is not None:
ref = reference
LIB.LGBM_DatasetCreateFromMat(
data.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
ctypes.c_int(dtype_float64),
ctypes.c_int32(mat.shape[0]),
ctypes.c_int32(mat.shape[1]),
ctypes.c_int(1),
c_str("max_bin=15"),
ref,
ctypes.byref(handle),
)
num_data = ctypes.c_int(0)
LIB.LGBM_DatasetGetNumData(handle, ctypes.byref(num_data))
num_feature = ctypes.c_int(0)
LIB.LGBM_DatasetGetNumFeature(handle, ctypes.byref(num_feature))
LIB.LGBM_DatasetSetField(
handle,
c_str("label"),
label.ctypes.data_as(ctypes.POINTER(ctypes.c_float)),
ctypes.c_int(len(label)),
ctypes.c_int(dtype_float32),
)
print(f"#data: {num_data.value} #feature: {num_feature.value}")
return handle
def free_dataset(handle):
LIB.LGBM_DatasetFree(handle)
def test_dataset(tmp_path):
binary_example_dir = Path(__file__).absolute().parents[2] / "examples" / "binary_classification"
train = load_from_file(binary_example_dir / "binary.train", None)
test = load_from_mat(binary_example_dir / "binary.test", train)
free_dataset(test)
test = load_from_csr(binary_example_dir / "binary.test", train)
free_dataset(test)
test = load_from_csc(binary_example_dir / "binary.test", train)
free_dataset(test)
train_binary = str(tmp_path / "train.binary.bin")
save_to_binary(train, train_binary)
free_dataset(train)
train = load_from_file(train_binary, None)
free_dataset(train)
def test_booster(tmp_path):
binary_example_dir = Path(__file__).absolute().parents[2] / "examples" / "binary_classification"
train = load_from_mat(binary_example_dir / "binary.train", None)
test = load_from_mat(binary_example_dir / "binary.test", train)
booster = ctypes.c_void_p()
model_path = tmp_path / "model.txt"
LIB.LGBM_BoosterCreate(train, c_str("app=binary metric=auc num_leaves=31 verbose=0"), ctypes.byref(booster))
LIB.LGBM_BoosterAddValidData(booster, test)
is_finished = ctypes.c_int(0)
for i in range(1, 51):
LIB.LGBM_BoosterUpdateOneIter(booster, ctypes.byref(is_finished))
result = np.array([0.0], dtype=np.float64)
out_len = ctypes.c_int(0)
LIB.LGBM_BoosterGetEval(
booster, ctypes.c_int(0), ctypes.byref(out_len), result.ctypes.data_as(ctypes.POINTER(ctypes.c_double))
)
if i % 10 == 0:
print(f"{i} iteration test AUC {result[0]:.6f}")
LIB.LGBM_BoosterSaveModel(booster, ctypes.c_int(0), ctypes.c_int(-1), ctypes.c_int(0), c_str(str(model_path)))
LIB.LGBM_BoosterFree(booster)
free_dataset(train)
free_dataset(test)
booster2 = ctypes.c_void_p()
num_total_model = ctypes.c_int(0)
LIB.LGBM_BoosterCreateFromModelfile(c_str(str(model_path)), ctypes.byref(num_total_model), ctypes.byref(booster2))
data = np.loadtxt(str(binary_example_dir / "binary.test"), dtype=np.float64)
mat = data[:, 1:]
preds = np.empty(mat.shape[0], dtype=np.float64)
num_preds = ctypes.c_int64(0)
data = np.asarray(mat.reshape(mat.size), dtype=np.float64)
LIB.LGBM_BoosterPredictForMat(
booster2,
data.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
ctypes.c_int(dtype_float64),
ctypes.c_int32(mat.shape[0]),
ctypes.c_int32(mat.shape[1]),
ctypes.c_int(1),
ctypes.c_int(1),
ctypes.c_int(0),
ctypes.c_int(25),
c_str(""),
ctypes.byref(num_preds),
preds.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
)
LIB.LGBM_BoosterPredictForFile(
booster2,
c_str(str(binary_example_dir / "binary.test")),
ctypes.c_int(0),
ctypes.c_int(0),
ctypes.c_int(0),
ctypes.c_int(25),
c_str(""),
c_str(tmp_path / "preds.txt"),
)
LIB.LGBM_BoosterPredictForFile(
booster2,
c_str(str(binary_example_dir / "binary.test")),
ctypes.c_int(0),
ctypes.c_int(0),
ctypes.c_int(10),
ctypes.c_int(25),
c_str(""),
c_str(tmp_path / "preds.txt"),
)
LIB.LGBM_BoosterFree(booster2)
def test_max_thread_control():
# at initialization, should be -1
num_threads = ctypes.c_int(0)
ret = LIB.LGBM_GetMaxThreads(ctypes.byref(num_threads))
assert ret == 0
assert num_threads.value == -1
# updating that value through the C API should work
ret = LIB.LGBM_SetMaxThreads(ctypes.c_int(6))
assert ret == 0
ret = LIB.LGBM_GetMaxThreads(ctypes.byref(num_threads))
assert ret == 0
assert num_threads.value == 6
# resetting to any negative number should set it to -1
ret = LIB.LGBM_SetMaxThreads(ctypes.c_int(-123))
assert ret == 0
ret = LIB.LGBM_GetMaxThreads(ctypes.byref(num_threads))
assert ret == 0
assert num_threads.value == -1