Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
tianlh
LightGBM-DCU
Commits
272fedb9
Unverified
Commit
272fedb9
authored
May 20, 2021
by
Nikita Titov
Committed by
GitHub
May 20, 2021
Browse files
[tests][python] Handle data types more accurate in C API test (#4297)
parent
e35ed5f6
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
77 additions
and
61 deletions
+77
-61
tests/c_api_test/test_.py
tests/c_api_test/test_.py
+77
-61
No files found.
tests/c_api_test/test_.py
View file @
272fedb9
...
...
@@ -52,10 +52,6 @@ dtype_int32 = 2
dtype_int64
=
3
def
c_array
(
ctype
,
values
):
return
(
ctype
*
len
(
values
))(
*
values
)
def
c_str
(
string
):
return
ctypes
.
c_char_p
(
string
.
encode
(
'utf-8'
))
...
...
@@ -71,9 +67,9 @@ def load_from_file(filename, reference):
ref
,
ctypes
.
byref
(
handle
))
print
(
LIB
.
LGBM_GetLastError
())
num_data
=
ctypes
.
c_
long
(
)
num_data
=
ctypes
.
c_
int
(
0
)
LIB
.
LGBM_DatasetGetNumData
(
handle
,
ctypes
.
byref
(
num_data
))
num_feature
=
ctypes
.
c_
long
(
)
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
...
...
@@ -91,7 +87,7 @@ def load_from_csr(filename, reference):
values
=
line
.
split
(
'
\t
'
)
data
.
append
([
float
(
x
)
for
x
in
values
[
1
:]])
label
.
append
(
float
(
values
[
0
]))
mat
=
np
.
array
(
data
)
mat
=
np
.
array
(
data
,
dtype
=
np
.
float64
)
label
=
np
.
array
(
label
,
dtype
=
np
.
float32
)
csr
=
sparse
.
csr_matrix
(
mat
)
handle
=
ctypes
.
c_void_p
()
...
...
@@ -100,22 +96,27 @@ def load_from_csr(filename, reference):
ref
=
reference
LIB
.
LGBM_DatasetCreateFromCSR
(
c
_array
(
ctypes
.
c_int
,
csr
.
indptr
),
dtype_int32
,
c
_array
(
ctypes
.
c_int
,
csr
.
indices
),
csr
.
data
.
ctypes
.
data_as
(
ctypes
.
POINTER
(
ctypes
.
c_
void_p
)),
dtype_float64
,
c
sr
.
indptr
.
ctypes
.
data_as
(
ctypes
.
POINTER
(
ctypes
.
c_int32
)
),
ctypes
.
c_int
(
dtype_int32
)
,
c
sr
.
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_
long
(
)
num_data
=
ctypes
.
c_
int
(
0
)
LIB
.
LGBM_DatasetGetNumData
(
handle
,
ctypes
.
byref
(
num_data
))
num_feature
=
ctypes
.
c_
long
(
)
num_feature
=
ctypes
.
c_
int
(
0
)
LIB
.
LGBM_DatasetGetNumFeature
(
handle
,
ctypes
.
byref
(
num_feature
))
LIB
.
LGBM_DatasetSetField
(
handle
,
c_str
(
'label'
),
c_array
(
ctypes
.
c_float
,
label
),
len
(
label
),
0
)
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
...
...
@@ -128,31 +129,36 @@ def load_from_csc(filename, reference):
values
=
line
.
split
(
'
\t
'
)
data
.
append
([
float
(
x
)
for
x
in
values
[
1
:]])
label
.
append
(
float
(
values
[
0
]))
mat
=
np
.
array
(
data
)
mat
=
np
.
array
(
data
,
dtype
=
np
.
float64
)
label
=
np
.
array
(
label
,
dtype
=
np
.
float32
)
cs
r
=
sparse
.
csc_matrix
(
mat
)
cs
c
=
sparse
.
csc_matrix
(
mat
)
handle
=
ctypes
.
c_void_p
()
ref
=
None
if
reference
is
not
None
:
ref
=
reference
LIB
.
LGBM_DatasetCreateFromCSC
(
c
_array
(
ctypes
.
c_int
,
csr
.
indptr
),
dtype_int32
,
c
_array
(
ctypes
.
c_int
,
csr
.
indices
),
cs
r
.
data
.
ctypes
.
data_as
(
ctypes
.
POINTER
(
ctypes
.
c_
void_p
)),
dtype_float64
,
ctypes
.
c_int64
(
len
(
cs
r
.
indptr
)),
ctypes
.
c_int64
(
len
(
cs
r
.
data
)),
ctypes
.
c_int64
(
cs
r
.
shape
[
0
]),
c
sc
.
indptr
.
ctypes
.
data_as
(
ctypes
.
POINTER
(
ctypes
.
c_int32
)
),
ctypes
.
c_int
(
dtype_int32
)
,
c
sc
.
indices
.
ctypes
.
data_as
(
ctypes
.
POINTER
(
ctypes
.
c_int32
)
),
cs
c
.
data
.
ctypes
.
data_as
(
ctypes
.
POINTER
(
ctypes
.
c_
double
)),
ctypes
.
c_int
(
dtype_float64
)
,
ctypes
.
c_int64
(
len
(
cs
c
.
indptr
)),
ctypes
.
c_int64
(
len
(
cs
c
.
data
)),
ctypes
.
c_int64
(
cs
c
.
shape
[
0
]),
c_str
(
'max_bin=15'
),
ref
,
ctypes
.
byref
(
handle
))
num_data
=
ctypes
.
c_
long
(
)
num_data
=
ctypes
.
c_
int
(
0
)
LIB
.
LGBM_DatasetGetNumData
(
handle
,
ctypes
.
byref
(
num_data
))
num_feature
=
ctypes
.
c_
long
(
)
num_feature
=
ctypes
.
c_
int
(
0
)
LIB
.
LGBM_DatasetGetNumFeature
(
handle
,
ctypes
.
byref
(
num_feature
))
LIB
.
LGBM_DatasetSetField
(
handle
,
c_str
(
'label'
),
c_array
(
ctypes
.
c_float
,
label
),
len
(
label
),
0
)
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
...
...
@@ -165,8 +171,8 @@ def load_from_mat(filename, reference):
values
=
line
.
split
(
'
\t
'
)
data
.
append
([
float
(
x
)
for
x
in
values
[
1
:]])
label
.
append
(
float
(
values
[
0
]))
mat
=
np
.
array
(
data
)
data
=
np
.
array
(
mat
.
reshape
(
mat
.
size
),
copy
=
False
)
mat
=
np
.
array
(
data
,
dtype
=
np
.
float64
)
data
=
np
.
array
(
mat
.
reshape
(
mat
.
size
),
dtype
=
np
.
float64
,
copy
=
False
)
label
=
np
.
array
(
label
,
dtype
=
np
.
float32
)
handle
=
ctypes
.
c_void_p
()
ref
=
None
...
...
@@ -174,19 +180,24 @@ def load_from_mat(filename, reference):
ref
=
reference
LIB
.
LGBM_DatasetCreateFromMat
(
data
.
ctypes
.
data_as
(
ctypes
.
POINTER
(
ctypes
.
c_
void_p
)),
dtype_float64
,
mat
.
shape
[
0
],
mat
.
shape
[
1
],
1
,
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_
long
(
)
num_data
=
ctypes
.
c_
int
(
0
)
LIB
.
LGBM_DatasetGetNumData
(
handle
,
ctypes
.
byref
(
num_data
))
num_feature
=
ctypes
.
c_
long
(
)
num_feature
=
ctypes
.
c_
int
(
0
)
LIB
.
LGBM_DatasetGetNumFeature
(
handle
,
ctypes
.
byref
(
num_feature
))
LIB
.
LGBM_DatasetSetField
(
handle
,
c_str
(
'label'
),
c_array
(
ctypes
.
c_float
,
label
),
len
(
label
),
0
)
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
...
...
@@ -228,20 +239,25 @@ def test_booster():
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_
ulong
(
0
)
out_len
=
ctypes
.
c_
int
(
0
)
LIB
.
LGBM_BoosterGetEval
(
booster
,
0
,
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
]:.
6
f
}
'
)
LIB
.
LGBM_BoosterSaveModel
(
booster
,
0
,
-
1
,
0
,
c_str
(
'model.txt'
))
LIB
.
LGBM_BoosterSaveModel
(
booster
,
ctypes
.
c_int
(
0
),
ctypes
.
c_int
(
-
1
),
ctypes
.
c_int
(
0
),
c_str
(
'model.txt'
))
LIB
.
LGBM_BoosterFree
(
booster
)
free_dataset
(
train
)
free_dataset
(
test
)
booster2
=
ctypes
.
c_void_p
()
num_total_model
=
ctypes
.
c_
long
(
)
num_total_model
=
ctypes
.
c_
int
(
0
)
LIB
.
LGBM_BoosterCreateFromModelfile
(
c_str
(
'model.txt'
),
ctypes
.
byref
(
num_total_model
),
...
...
@@ -251,20 +267,20 @@ def test_booster():
'../../examples/binary_classification/binary.test'
),
'r'
)
as
inp
:
for
line
in
inp
.
readlines
():
data
.
append
([
float
(
x
)
for
x
in
line
.
split
(
'
\t
'
)[
1
:]])
mat
=
np
.
array
(
data
)
mat
=
np
.
array
(
data
,
dtype
=
np
.
float64
)
preb
=
np
.
zeros
(
mat
.
shape
[
0
],
dtype
=
np
.
float64
)
num_preb
=
ctypes
.
c_
long
(
)
data
=
np
.
array
(
mat
.
reshape
(
mat
.
size
),
copy
=
False
)
num_preb
=
ctypes
.
c_
int64
(
0
)
data
=
np
.
array
(
mat
.
reshape
(
mat
.
size
),
dtype
=
np
.
float64
,
copy
=
False
)
LIB
.
LGBM_BoosterPredictForMat
(
booster2
,
data
.
ctypes
.
data_as
(
ctypes
.
POINTER
(
ctypes
.
c_
void_p
)),
dtype_float64
,
mat
.
shape
[
0
],
mat
.
shape
[
1
],
1
,
1
,
0
,
25
,
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_preb
),
preb
.
ctypes
.
data_as
(
ctypes
.
POINTER
(
ctypes
.
c_double
)))
...
...
@@ -272,20 +288,20 @@ def test_booster():
booster2
,
c_str
(
os
.
path
.
join
(
os
.
path
.
dirname
(
os
.
path
.
realpath
(
__file__
)),
'../../examples/binary_classification/binary.test'
)),
0
,
0
,
0
,
25
,
ctypes
.
c_int
(
0
)
,
ctypes
.
c_int
(
0
)
,
ctypes
.
c_int
(
0
)
,
ctypes
.
c_int
(
25
)
,
c_str
(
''
),
c_str
(
'preb.txt'
))
LIB
.
LGBM_BoosterPredictForFile
(
booster2
,
c_str
(
os
.
path
.
join
(
os
.
path
.
dirname
(
os
.
path
.
realpath
(
__file__
)),
'../../examples/binary_classification/binary.test'
)),
0
,
0
,
10
,
25
,
ctypes
.
c_int
(
0
)
,
ctypes
.
c_int
(
0
)
,
ctypes
.
c_int
(
10
)
,
ctypes
.
c_int
(
25
)
,
c_str
(
''
),
c_str
(
'preb.txt'
))
LIB
.
LGBM_BoosterFree
(
booster2
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment