MCPcopy Create free account
hub / github.com/Xtra-Computing/thundersvm / _sparse_fit

Method _sparse_fit

python/thundersvm/thundersvm.py:217–267  ·  view source on GitHub ↗
(self, X, y, solver_type, kernel)

Source from the content-addressed store, hash-verified

215 self.n_classes = n_classes[0]
216
217 def _sparse_fit(self, X, y, solver_type, kernel):
218 X.data = np.asarray(X.data, dtype=np.float32, order='C')
219 X.sort_indices()
220 kernel_type = kernel
221 data = X.data.ctypes.data_as(POINTER(c_float))
222 indices = X.indices.ctypes.data_as(POINTER(c_int32))
223 indptr = X.indptr.ctypes.data_as(POINTER(c_int32))
224 y = np.asarray(y, dtype=np.float32, order='C')
225 label = y.ctypes.data_as(POINTER(c_float))
226
227 if self.class_weight is None:
228 weight_size = 0
229 self.class_weight = dict()
230 weight_label = (c_int * weight_size)()
231 weight_label[:] = list(self.class_weight.keys())
232 weight = (c_float * weight_size)()
233 weight[:] = list(self.class_weight.values())
234 elif self.class_weight == 'balanced':
235 y_unique = np.unique(y)
236 y_count = np.bincount(y.astype(int))
237 weight_label_list = []
238 weight_list = []
239 for n in range(0, len(y_count)):
240 if y_count[n] != 0:
241 weight_label_list.append(n)
242 weight_list.append(X.shape[0] / (len(y_unique) * y_count[n]))
243 weight_size = len(weight_list)
244 weight_label = (c_int * weight_size)()
245 weight_label[:] = weight_label_list
246 weight = (c_float * weight_size)()
247 weight[:] = weight_list
248 else:
249 weight_size = len(self.class_weight)
250 weight_label = (c_int * weight_size)()
251 weight_label[:] = list(self.class_weight.keys())
252 weight = (c_float * weight_size)()
253 weight[:] = list(self.class_weight.values())
254
255 n_features = (c_int * 1)()
256 n_classes = (c_int * 1)()
257 self._train_succeed = (c_int * 1)()
258 thundersvm.sparse_model_scikit(
259 X.shape[0], data, indptr, indices, label, solver_type,
260 kernel_type, self.degree, c_float(self._gamma), c_float(self.coef0),
261 c_float(self.C), c_float(self.nu), c_float(self.epsilon), c_float(self.tol),
262 self.probability, weight_size, weight_label, weight,
263 self.verbose, self.max_iter, self.n_jobs, self.max_mem_size,
264 self.gpu_id,
265 n_features, n_classes, self._train_succeed, c_void_p(self.model))
266 self.n_features = n_features[0]
267 self.n_classes = n_classes[0]
268
269 def _validate_for_predict(self, X):
270 # check_is_fitted(self, 'support_')

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected