Commit 652d1618 authored by Guolin Ke's avatar Guolin Ke
Browse files

add a small c_api test

parent 55cd6096
...@@ -675,7 +675,7 @@ RowFunctionFromCSR(const void* indptr, int indptr_type, const int32_t* indices, ...@@ -675,7 +675,7 @@ RowFunctionFromCSR(const void* indptr, int indptr_type, const int32_t* indices,
std::vector<std::pair<int, double>> ret; std::vector<std::pair<int, double>> ret;
int64_t start = ptr_indptr[idx]; int64_t start = ptr_indptr[idx];
int64_t end = ptr_indptr[idx + 1]; int64_t end = ptr_indptr[idx + 1];
CHECK(start >= 0 && end < nelem); CHECK(start >= 0 && end <= nelem);
for (int64_t i = start; i <= end; ++i) { for (int64_t i = start; i <= end; ++i) {
ret.emplace_back(indices[i], data_ptr[i]); ret.emplace_back(indices[i], data_ptr[i]);
} }
...@@ -688,7 +688,7 @@ RowFunctionFromCSR(const void* indptr, int indptr_type, const int32_t* indices, ...@@ -688,7 +688,7 @@ RowFunctionFromCSR(const void* indptr, int indptr_type, const int32_t* indices,
std::vector<std::pair<int, double>> ret; std::vector<std::pair<int, double>> ret;
int64_t start = ptr_indptr[idx]; int64_t start = ptr_indptr[idx];
int64_t end = ptr_indptr[idx + 1]; int64_t end = ptr_indptr[idx + 1];
CHECK(start >= 0 && end < nelem); CHECK(start >= 0 && end <= nelem);
for (int64_t i = start; i <= end; ++i) { for (int64_t i = start; i <= end; ++i) {
ret.emplace_back(indices[i], data_ptr[i]); ret.emplace_back(indices[i], data_ptr[i]);
} }
...@@ -706,7 +706,7 @@ RowFunctionFromCSR(const void* indptr, int indptr_type, const int32_t* indices, ...@@ -706,7 +706,7 @@ RowFunctionFromCSR(const void* indptr, int indptr_type, const int32_t* indices,
std::vector<std::pair<int, double>> ret; std::vector<std::pair<int, double>> ret;
int64_t start = ptr_indptr[idx]; int64_t start = ptr_indptr[idx];
int64_t end = ptr_indptr[idx + 1]; int64_t end = ptr_indptr[idx + 1];
CHECK(start >= 0 && end < nelem); CHECK(start >= 0 && end <= nelem);
for (int64_t i = start; i <= end; ++i) { for (int64_t i = start; i <= end; ++i) {
ret.emplace_back(indices[i], data_ptr[i]); ret.emplace_back(indices[i], data_ptr[i]);
} }
...@@ -719,7 +719,7 @@ RowFunctionFromCSR(const void* indptr, int indptr_type, const int32_t* indices, ...@@ -719,7 +719,7 @@ RowFunctionFromCSR(const void* indptr, int indptr_type, const int32_t* indices,
std::vector<std::pair<int, double>> ret; std::vector<std::pair<int, double>> ret;
int64_t start = ptr_indptr[idx]; int64_t start = ptr_indptr[idx];
int64_t end = ptr_indptr[idx + 1]; int64_t end = ptr_indptr[idx + 1];
CHECK(start >= 0 && end < nelem); CHECK(start >= 0 && end <= nelem);
for (int64_t i = start; i <= end; ++i) { for (int64_t i = start; i <= end; ++i) {
ret.emplace_back(indices[i], data_ptr[i]); ret.emplace_back(indices[i], data_ptr[i]);
} }
......
from __future__ import absolute_import
import sys import sys
import os import os
import ctypes import ctypes
import collections import collections
import re
import numpy as np import numpy as np
from scipy import sparse from scipy import sparse
def _load_lib(): def LoadDll():
"""Load xgboost Library.""" lib_path = '../../windows/x64/DLL/lib_lightgbm.dll'
lib_path = './windows/x64/DLL/lib_lightgbm.dll'
if len(lib_path) == 0: if len(lib_path) == 0:
return None return None
lib = ctypes.cdll.LoadLibrary(lib_path) lib = ctypes.cdll.LoadLibrary(lib_path)
return lib return lib
LIB = LoadDll()
LIB = _load_lib()
LIB.LGBM_GetLastError.restype = ctypes.c_char_p
def test_load_from_file():
handle = ctypes.c_void_p()
LIB.LGBM_CreateDatasetFromFile(ctypes.c_char_p('./examples/binary_classification/binary.train'),
ctypes.c_char_p('max_bin=15'), ctypes.c_void_p(None), ctypes.byref(handle))
num_data = ctypes.c_ulong()
LIB.LGBM_DatasetGetNumData(handle, ctypes.byref(num_data) )
print num_data
num_feature = ctypes.c_ulong()
LIB.LGBM_DatasetGetNumFeature(handle, ctypes.byref(num_feature) )
return handle
def c_array(ctype, values): def c_array(ctype, values):
"""Convert a python string to c array."""
return (ctype * len(values))(*values) return (ctype * len(values))(*values)
def c_str(string): def c_str(string):
"""Convert a python string to cstring."""
return ctypes.c_char_p(string.encode('utf-8')) return ctypes.c_char_p(string.encode('utf-8'))
def test_load_from_matric():
data = []
inp = open('./examples/binary_classification/binary.train', 'r')
for line in inp.readlines():
data.append( [float(x) for x in line.split('\t')[1:]] )
inp.close()
mat = np.array(data)
print mat.shape
data = np.array(mat.reshape(mat.size), copy=False)
handle = ctypes.c_void_p()
LIB.LGBM_CreateDatasetFromMat(data.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), 1,
mat.shape[0],
mat.shape[1], 1,
ctypes.c_char_p('max_bin=15 is_sparse=false'), None, ctypes.byref(handle) )
LIB.LGBM_DatasetFree(ctypes.byref(handle))
# num_data = ctypes.c_ulong()
# LIB.LGBM_DatasetGetNumData(handle, ctypes.byref(num_data) )
# print num_data
# num_feature = ctypes.c_ulong()
# LIB.LGBM_DatasetGetNumFeature(handle, ctypes.byref(num_feature) )
# print num_feature
return handle
def test_load_from_csr(filename, reference): def test_load_from_csr(filename, reference):
data = [] data = []
label = [] label = []
...@@ -73,41 +31,41 @@ def test_load_from_csr(filename, reference): ...@@ -73,41 +31,41 @@ def test_load_from_csr(filename, reference):
inp.close() inp.close()
mat = np.array(data) mat = np.array(data)
label = np.array(label, dtype=np.float32) label = np.array(label, dtype=np.float32)
print mat.shape
csr = sparse.csr_matrix(mat) csr = sparse.csr_matrix(mat)
handle = ctypes.c_void_p() handle = ctypes.c_void_p()
ref = None ref = None
if reference != None: if reference != None:
ref = ctypes.byref(reference) ref = ctypes.byref(reference)
LIB.LGBM_CreateDatasetFromCSR(c_array(ctypes.c_int, csr.indptr),
LIB.LGBM_CreateDatasetFromCSR(c_array(ctypes.c_int, csr.indptr), 2,
c_array(ctypes.c_int, csr.indices), c_array(ctypes.c_int, csr.indices),
csr.data.ctypes.data_as(ctypes.POINTER(ctypes.c_void_p)), csr.data.ctypes.data_as(ctypes.POINTER(ctypes.c_void_p)),
1, len(csr.indptr), len(csr.data), 1, len(csr.indptr), len(csr.data),
csr.shape[1], ctypes.c_char_p('max_bin=15'), ref, ctypes.byref(handle) ) csr.shape[1], ctypes.c_char_p('max_bin=15'), ref, ctypes.byref(handle) )
num_data = ctypes.c_ulong() num_data = ctypes.c_ulong()
LIB.LGBM_DatasetGetNumData(handle, ctypes.byref(num_data) ) LIB.LGBM_DatasetGetNumData(handle, ctypes.byref(num_data) )
print num_data
num_feature = ctypes.c_ulong() num_feature = ctypes.c_ulong()
LIB.LGBM_DatasetGetNumFeature(handle, ctypes.byref(num_feature) ) 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'), c_array(ctypes.c_float, label), len(label), 0)
return handle return handle
train = test_load_from_csr('./examples/binary_classification/binary.train', None) train = test_load_from_csr('../../examples/binary_classification/binary.train', None)
test = [test_load_from_csr('./examples/binary_classification/binary.test', train)] test = [test_load_from_csr('../../examples/binary_classification/binary.test', train)]
name = [c_str('test')] name = [c_str('test')]
booster = ctypes.c_void_p() booster = ctypes.c_void_p()
LIB.LGBM_BoosterCreate(train, c_array(ctypes.c_void_p, test), c_array(ctypes.c_char_p, name), 1, "app=binary metric=auc num_leaves=31", ctypes.byref(booster)) LIB.LGBM_BoosterCreate(train, c_array(ctypes.c_void_p, test), c_array(ctypes.c_char_p, name),
len(test), "app=binary metric=auc num_leaves=31 verbose=0", ctypes.byref(booster))
is_finished = ctypes.c_int(0) is_finished = ctypes.c_int(0)
for i in xrange(100): for i in xrange(100):
LIB.LGBM_BoosterUpdateOneIter(booster,ctypes.byref(is_finished)) LIB.LGBM_BoosterUpdateOneIter(booster,ctypes.byref(is_finished))
result = np.array([0.0], dtype=np.float32) result = np.array([0.0], dtype=np.float32)
out_len = ctypes.c_ulong(0) out_len = ctypes.c_ulong(0)
LIB.LGBM_BoosterEval(booster, 0, ctypes.byref(out_len), result.ctypes.data_as(ctypes.POINTER(ctypes.c_float))) LIB.LGBM_BoosterEval(booster, 1, ctypes.byref(out_len), result.ctypes.data_as(ctypes.POINTER(ctypes.c_float)))
print result print '%d Iteration test AUC %f' %(i, result[0])
LIB.LGBM_BoosterSaveModel(booster, -1, c_str('model.txt')) LIB.LGBM_BoosterSaveModel(booster, -1, c_str('model.txt'))
booster2 = ctypes.c_void_p()
booster2 = ctypes.c_void_p()
LIB.LGBM_BoosterLoadFromModelfile(c_str('model.txt'), ctypes.byref(booster2)) LIB.LGBM_BoosterLoadFromModelfile(c_str('model.txt'), ctypes.byref(booster2))
print type(len([0,0]))
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment