| 85 | |
| 86 | |
| 87 | def load_from_csr(filename, reference): |
| 88 | data = [] |
| 89 | label = [] |
| 90 | with open(filename, 'r') as inp: |
| 91 | for line in inp.readlines(): |
| 92 | values = line.split('\t') |
| 93 | data.append([float(x) for x in values[1:]]) |
| 94 | label.append(float(values[0])) |
| 95 | mat = np.array(data) |
| 96 | label = np.array(label, dtype=np.float32) |
| 97 | csr = sparse.csr_matrix(mat) |
| 98 | handle = ctypes.c_void_p() |
| 99 | ref = None |
| 100 | if reference is not None: |
| 101 | ref = reference |
| 102 | |
| 103 | LIB.LGBM_DatasetCreateFromCSR( |
| 104 | c_array(ctypes.c_int, csr.indptr), |
| 105 | dtype_int32, |
| 106 | c_array(ctypes.c_int, csr.indices), |
| 107 | csr.data.ctypes.data_as(ctypes.POINTER(ctypes.c_void_p)), |
| 108 | dtype_float64, |
| 109 | ctypes.c_int64(len(csr.indptr)), |
| 110 | ctypes.c_int64(len(csr.data)), |
| 111 | ctypes.c_int64(csr.shape[1]), |
| 112 | c_str('max_bin=15'), |
| 113 | ref, |
| 114 | ctypes.byref(handle)) |
| 115 | num_data = ctypes.c_long() |
| 116 | LIB.LGBM_DatasetGetNumData(handle, ctypes.byref(num_data)) |
| 117 | num_feature = ctypes.c_long() |
| 118 | LIB.LGBM_DatasetGetNumFeature(handle, ctypes.byref(num_feature)) |
| 119 | LIB.LGBM_DatasetSetField(handle, c_str('label'), c_array(ctypes.c_float, label), len(label), 0) |
| 120 | print('#data: %d #feature: %d' % (num_data.value, num_feature.value)) |
| 121 | return handle |
| 122 | |
| 123 | |
| 124 | def load_from_csc(filename, reference): |