Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for SVC fit_proba not using class weights #5912

Merged
merged 7 commits into from
Jun 15, 2024
22 changes: 19 additions & 3 deletions python/cuml/svm/svc.pyx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019-2023, NVIDIA CORPORATION.
# Copyright (c) 2019-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -440,7 +440,7 @@ class SVC(SVMBase,
self._fit_status_ = 0
return self

def _fit_proba(self, X, y, samle_weight) -> "SVC":
def _fit_proba(self, X, y, sample_weight) -> "SVC":
params = self.get_params()
params["probability"] = False

Expand All @@ -460,8 +460,24 @@ class SVC(SVMBase,
cv=5,
method='sigmoid')

# Apply class weights to sample weights, necessary, so it doesn't crash when sample_weight is None
sample_weight = apply_class_weight(self.handle, sample_weight, self.class_weight, y, self.verbose,
self.output_type, self.dtype)

# If sample_weight is not None, it is a cupy array, and we need to convert it to a numpy array for sklearn
if sample_weight is not None:
# Currently, fitting a probabilistic SVC with class weights requires at least 3 classes, otherwise the following,
# ambiguous error is raised: ValueError: Buffer dtype mismatch, expected 'const float' but got 'double'
if len(set(y)) < 3:
raise ValueError("At least 3 classes are required to use probabilistic SVC with class weights.")

# Convert cupy array to numpy array
sample_weight = sample_weight.get()

with cuml.internals.exit_internal_api():
self.prob_svc.fit(X, y)
# Fit the model, sample_weight is either None or a numpy array
self.prob_svc.fit(X, y, sample_weight=sample_weight)

self._fit_status_ = 0
return self

Expand Down
Loading