|
| 1 | +""" |
| 2 | +Flat classifier approach, used for comparison purposes. |
| 3 | +
|
| 4 | +Implementation by @lpfgarcia |
| 5 | +""" |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +from sklearn.base import BaseEstimator |
| 9 | +from sklearn.linear_model import LogisticRegression |
| 10 | +from sklearn.utils.validation import check_is_fitted |
| 11 | + |
| 12 | + |
| 13 | +class FlatClassifier(BaseEstimator): |
| 14 | + """ |
| 15 | + A flat classifier utility that accepts as input a hierarchy and flattens it internally. |
| 16 | +
|
| 17 | + Examples |
| 18 | + -------- |
| 19 | + >>> from hiclass import FlatClassifier |
| 20 | + >>> y = [['1', '1.1'], ['2', '2.1']] |
| 21 | + >>> X = [[1, 2], [3, 4]] |
| 22 | + >>> flat = FlatClassifier() |
| 23 | + >>> flat.fit(X, y) |
| 24 | + >>> flat.predict(X) |
| 25 | + array([['1', '1.1'], |
| 26 | + ['2', '2.1']]) |
| 27 | + """ |
| 28 | + |
| 29 | + def __init__( |
| 30 | + self, |
| 31 | + local_classifier: BaseEstimator = LogisticRegression(), |
| 32 | + ): |
| 33 | + """ |
| 34 | + Initialize a flat classifier. |
| 35 | +
|
| 36 | + Parameters |
| 37 | + ---------- |
| 38 | + local_classifier : BaseEstimator, default=LogisticRegression |
| 39 | + The scikit-learn model used for the flat classification. Needs to have fit, predict and clone methods. |
| 40 | + """ |
| 41 | + self.local_classifier = local_classifier |
| 42 | + |
| 43 | + def fit(self, X, y, sample_weight=None): |
| 44 | + """ |
| 45 | + Fit a flat classifier. |
| 46 | +
|
| 47 | + Parameters |
| 48 | + ---------- |
| 49 | + X : {array-like, sparse matrix} of shape (n_samples, n_features) |
| 50 | + The training input samples. Internally, its dtype will be converted |
| 51 | + to ``dtype=np.float32``. If a sparse matrix is provided, it will be |
| 52 | + converted into a sparse ``csc_matrix``. |
| 53 | + y : array-like of shape (n_samples, n_levels) |
| 54 | + The target values, i.e., hierarchical class labels for classification. |
| 55 | + sample_weight : array-like of shape (n_samples,), default=None |
| 56 | + Array of weights that are assigned to individual samples. |
| 57 | + If not provided, then each sample is given unit weight. |
| 58 | +
|
| 59 | + Returns |
| 60 | + ------- |
| 61 | + self : object |
| 62 | + Fitted estimator. |
| 63 | + """ |
| 64 | + # Convert from hierarchical labels to flat labels |
| 65 | + self.separator_ = "::HiClass::Separator::" |
| 66 | + y = [self.separator_.join(i) for i in y] |
| 67 | + |
| 68 | + # Fit flat classifier |
| 69 | + self.local_classifier.fit(X, y, sample_weight=sample_weight) |
| 70 | + |
| 71 | + # Return the classifier |
| 72 | + return self |
| 73 | + |
| 74 | + def predict(self, X): |
| 75 | + """ |
| 76 | + Predict classes for the given data. |
| 77 | +
|
| 78 | + Hierarchical labels are returned. |
| 79 | +
|
| 80 | + Parameters |
| 81 | + ---------- |
| 82 | + X : {array-like, sparse matrix} of shape (n_samples, n_features) |
| 83 | + The input samples. Internally, its dtype will be converted |
| 84 | + to ``dtype=np.float32``. If a sparse matrix is provided, it will be |
| 85 | + converted into a sparse ``csr_matrix``. |
| 86 | + Returns |
| 87 | + ------- |
| 88 | + y : ndarray of shape (n_samples,) or (n_samples, n_outputs) |
| 89 | + The predicted classes. |
| 90 | + """ |
| 91 | + # Check if fit has been called |
| 92 | + check_is_fitted(self) |
| 93 | + |
| 94 | + # Predict and remove separator |
| 95 | + predictions = [ |
| 96 | + i.split(self.separator_) for i in self.local_classifier.predict(X) |
| 97 | + ] |
| 98 | + |
| 99 | + return np.array(predictions) |
0 commit comments