Skip to content

Commit

Permalink
fix calculating class imbalance
Browse files Browse the repository at this point in the history
  • Loading branch information
balins committed Aug 30, 2024
1 parent 1db7f82 commit c53b8c5
Showing 1 changed file with 5 additions and 20 deletions.
25 changes: 5 additions & 20 deletions fsvm/_fuzzy_svc.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ class FuzzySVC(ClassifierMixin, BaseEstimator):
This parameter is only used when `membership_decay='exponential'`.
balanced : bool, default=True
Set the parameter C of class i to r_i*C for FuzzySVC. If `True`, the
membership of each sample will be scaled by the number r_i expressing
its ratio to the number of samples of the majority class as in [1]_.
Whether to use the values of y to automatically adjust weights
inversely proportional to class frequencies in the input data as
``n_samples / (n_classes * np.bincount(y))``.
C : float, default=1.0
Regularization parameter. The strength of the regularization is
Expand Down Expand Up @@ -327,8 +327,7 @@ def fit(self, X, y):
self.y_ = y

y_ = column_or_1d(y, warn=True)
classes, y_, counts = np.unique(y, return_inverse=True, return_counts=True)
class_imbalance = self.__calculate_class_imbalance(y, classes, counts)
classes, y_ = np.unique(y_, return_inverse=True)

svc_args = {
"C": self.C,
Expand All @@ -340,7 +339,7 @@ def fit(self, X, y):
"probability": self.probability,
"tol": self.tol,
"cache_size": self.cache_size,
"class_weight": class_imbalance,
"class_weight": "balanced" if self.balanced else None,
"verbose": self.verbose,
"max_iter": self.max_iter,
"decision_function_shape": self.decision_function_shape,
Expand Down Expand Up @@ -415,20 +414,6 @@ def predict(self, X):

return self.svc_.predict(X)

def __calculate_class_imbalance(self, y, classes, counts):
class_imbalance = None

if self.balanced:
class_imbalance = {}
majority_class_count = np.amax(counts)

for class_label in set(y):
idx = np.where(classes == class_label)[0][0]

class_imbalance[class_label] = counts[idx] / majority_class_count

return class_imbalance

def __calculate_membership_degree(self):
if self.membership_decay == "exponential":
membership = 2 / (1 + np.exp(self.beta * self.distance_))
Expand Down

0 comments on commit c53b8c5

Please sign in to comment.