Encode target labels with value between 0 and n_classes-1. This transformer should be used to encode target values, *i.e.* `y`, and not the input `X`. Read more in the :ref:`User Guide `. .. versionadded:: 0.12 Attributes ---------- classes_ : n
| 38 | |
| 39 | |
| 40 | class LabelEncoder(TransformerMixin, BaseEstimator, auto_wrap_output_keys=None): |
| 41 | """Encode target labels with value between 0 and n_classes-1. |
| 42 | |
| 43 | This transformer should be used to encode target values, *i.e.* `y`, and |
| 44 | not the input `X`. |
| 45 | |
| 46 | Read more in the :ref:`User Guide <preprocessing_targets>`. |
| 47 | |
| 48 | .. versionadded:: 0.12 |
| 49 | |
| 50 | Attributes |
| 51 | ---------- |
| 52 | classes_ : ndarray of shape (n_classes,) |
| 53 | Holds the label for each class. |
| 54 | |
| 55 | See Also |
| 56 | -------- |
| 57 | OrdinalEncoder : Encode categorical features using an ordinal encoding |
| 58 | scheme. |
| 59 | OneHotEncoder : Encode categorical features as a one-hot numeric array. |
| 60 | |
| 61 | Examples |
| 62 | -------- |
| 63 | `LabelEncoder` can be used to normalize labels. |
| 64 | |
| 65 | >>> from sklearn.preprocessing import LabelEncoder |
| 66 | >>> le = LabelEncoder() |
| 67 | >>> le.fit([1, 2, 2, 6]) |
| 68 | LabelEncoder() |
| 69 | >>> le.classes_ |
| 70 | array([1, 2, 6]) |
| 71 | >>> le.transform([1, 1, 2, 6]) |
| 72 | array([0, 0, 1, 2]...) |
| 73 | >>> le.inverse_transform([0, 0, 1, 2]) |
| 74 | array([1, 1, 2, 6]) |
| 75 | |
| 76 | It can also be used to transform non-numerical labels (as long as they are |
| 77 | hashable and comparable) to numerical labels. |
| 78 | |
| 79 | >>> le = LabelEncoder() |
| 80 | >>> le.fit(["paris", "paris", "tokyo", "amsterdam"]) |
| 81 | LabelEncoder() |
| 82 | >>> list(le.classes_) |
| 83 | [np.str_('amsterdam'), np.str_('paris'), np.str_('tokyo')] |
| 84 | >>> le.transform(["tokyo", "tokyo", "paris"]) |
| 85 | array([2, 2, 1]...) |
| 86 | >>> list(le.inverse_transform([2, 2, 1])) |
| 87 | [np.str_('tokyo'), np.str_('tokyo'), np.str_('paris')] |
| 88 | """ |
| 89 | |
| 90 | def fit(self, y): |
| 91 | """Fit label encoder. |
| 92 | |
| 93 | Parameters |
| 94 | ---------- |
| 95 | y : array-like of shape (n_samples,) |
| 96 | Target values. |
| 97 |
no outgoing calls
searching dependent graphs…