Skip to content

Commit

Permalink
add predict_proba and predict_log_proba
Browse files Browse the repository at this point in the history
  • Loading branch information
balins committed Sep 10, 2024
1 parent a39a4e2 commit 1d89635
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions fsvm/_fuzzy_svc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
)
from sklearn.svm import SVC as _SVC
from sklearn.utils._param_validation import Interval, StrOptions
from sklearn.utils.metaestimators import available_if
from sklearn.utils.multiclass import check_classification_targets
from sklearn.utils.validation import column_or_1d

Expand Down Expand Up @@ -417,6 +418,72 @@ def predict(self, X):

return self.svc_.predict(X)

def _check_proba(self):
if not self.probability:
raise AttributeError(
"predict_proba is not available when probability=False"
)
return True

@available_if(_check_proba)
def predict_proba(self, X):
"""Compute probabilities of possible outcomes for samples in X.
The model needs to have probability information computed at training
time: fit with attribute `probability` set to True.
Parameters
----------
X : array-like of shape (n_samples, n_features)
For kernel="precomputed", the expected shape of X is
(n_samples_test, n_samples_train).
Returns
-------
T : ndarray of shape (n_samples, n_classes)
Returns the probability of the sample for each class in
the model. The columns correspond to the classes in sorted
order, as they appear in the attribute :term:`classes_`.
Notes
-----
The probability model is created using cross validation, so
the results can be slightly different than those obtained by
predict. Also, it will produce meaningless results on very small
datasets.
"""
return self.svc_.predict_proba(X)

@available_if(_check_proba)
def predict_log_proba(self, X):
"""Compute log probabilities of possible outcomes for samples in X.
The model need to have probability information computed at training
time: fit with attribute `probability` set to True.
Parameters
----------
X : array-like of shape (n_samples, n_features) or \
(n_samples_test, n_samples_train)
For kernel="precomputed", the expected shape of X is
(n_samples_test, n_samples_train).
Returns
-------
T : ndarray of shape (n_samples, n_classes)
Returns the log-probabilities of the sample for each class in
the model. The columns correspond to the classes in sorted
order, as they appear in the attribute :term:`classes_`.
Notes
-----
The probability model is created using cross validation, so
the results can be slightly different than those obtained by
predict. Also, it will produce meaningless results on very small
datasets.
"""
return self.svc_.predict_log_proba(X)

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 1d89635

Please sign in to comment.