Fit underlying estimators. Parameters ---------- X : {array-like, sparse matrix} of shape (n_samples, n_features) Data. y : array-like of shape (n_samples,) Multi-class targets. **fit_params : dict Parameters passed to th
(self, X, y, **fit_params)
| 1161 | prefer_skip_nested_validation=False |
| 1162 | ) |
| 1163 | def fit(self, X, y, **fit_params): |
| 1164 | """Fit underlying estimators. |
| 1165 | |
| 1166 | Parameters |
| 1167 | ---------- |
| 1168 | X : {array-like, sparse matrix} of shape (n_samples, n_features) |
| 1169 | Data. |
| 1170 | |
| 1171 | y : array-like of shape (n_samples,) |
| 1172 | Multi-class targets. |
| 1173 | |
| 1174 | **fit_params : dict |
| 1175 | Parameters passed to the ``estimator.fit`` method of each |
| 1176 | sub-estimator. |
| 1177 | |
| 1178 | .. versionadded:: 1.4 |
| 1179 | Only available if `enable_metadata_routing=True`. See |
| 1180 | :ref:`Metadata Routing User Guide <metadata_routing>` for more |
| 1181 | details. |
| 1182 | |
| 1183 | Returns |
| 1184 | ------- |
| 1185 | self : object |
| 1186 | Returns a fitted instance of self. |
| 1187 | """ |
| 1188 | _raise_for_params(fit_params, self, "fit") |
| 1189 | |
| 1190 | routed_params = process_routing( |
| 1191 | self, |
| 1192 | "fit", |
| 1193 | **fit_params, |
| 1194 | ) |
| 1195 | |
| 1196 | y = validate_data(self, X="no_validation", y=y) |
| 1197 | |
| 1198 | random_state = check_random_state(self.random_state) |
| 1199 | check_classification_targets(y) |
| 1200 | |
| 1201 | self.classes_ = np.unique(y) |
| 1202 | n_classes = self.classes_.shape[0] |
| 1203 | if n_classes == 0: |
| 1204 | raise ValueError( |
| 1205 | "OutputCodeClassifier can not be fit when no class is present." |
| 1206 | ) |
| 1207 | n_estimators = int(n_classes * self.code_size) |
| 1208 | |
| 1209 | # FIXME: there are more elaborate methods than generating the codebook |
| 1210 | # randomly. |
| 1211 | self.code_book_ = random_state.uniform(size=(n_classes, n_estimators)) |
| 1212 | self.code_book_[self.code_book_ > 0.5] = 1.0 |
| 1213 | |
| 1214 | if hasattr(self.estimator, "decision_function"): |
| 1215 | self.code_book_[self.code_book_ != 1] = -1.0 |
| 1216 | else: |
| 1217 | self.code_book_[self.code_book_ != 1] = 0.0 |
| 1218 | |
| 1219 | classes_index = {c: i for i, c in enumerate(self.classes_)} |
| 1220 |