| 122 | |
| 123 | |
| 124 | def load_from_csc(filename, reference): |
| 125 | data = [] |
| 126 | label = [] |
| 127 | with open(filename, 'r') as inp: |
| 128 | for line in inp.readlines(): |
| 129 | values = line.split('\t') |
| 130 | data.append([float(x) for x in values[1:]]) |
| 131 | label.append(float(values[0])) |
| 132 | mat = np.array(data) |
| 133 | label = np.array(label, dtype=np.float32) |
| 134 | csr = sparse.csc_matrix(mat) |
| 135 | handle = ctypes.c_void_p() |
| 136 | ref = None |
| 137 | if reference is not None: |
| 138 | ref = reference |
| 139 | |
| 140 | LIB.LGBM_DatasetCreateFromCSC( |
| 141 | c_array(ctypes.c_int, csr.indptr), |
| 142 | dtype_int32, |
| 143 | c_array(ctypes.c_int, csr.indices), |
| 144 | csr.data.ctypes.data_as(ctypes.POINTER(ctypes.c_void_p)), |
| 145 | dtype_float64, |
| 146 | ctypes.c_int64(len(csr.indptr)), |
| 147 | ctypes.c_int64(len(csr.data)), |
| 148 | ctypes.c_int64(csr.shape[0]), |
| 149 | c_str('max_bin=15'), |
| 150 | ref, |
| 151 | ctypes.byref(handle)) |
| 152 | num_data = ctypes.c_long() |
| 153 | LIB.LGBM_DatasetGetNumData(handle, ctypes.byref(num_data)) |
| 154 | num_feature = ctypes.c_long() |
| 155 | LIB.LGBM_DatasetGetNumFeature(handle, ctypes.byref(num_feature)) |
| 156 | LIB.LGBM_DatasetSetField(handle, c_str('label'), c_array(ctypes.c_float, label), len(label), 0) |
| 157 | print('#data: %d #feature: %d' % (num_data.value, num_feature.value)) |
| 158 | return handle |
| 159 | |
| 160 | |
| 161 | def load_from_mat(filename, reference): |