Transform X to ordinal codes. Parameters ---------- X : array-like of shape (n_samples, n_features) The data to encode. Returns ------- X_out : ndarray of shape (n_samples, n_features) Transformed input.
(self, X)
| 1582 | return self |
| 1583 | |
| 1584 | def transform(self, X): |
| 1585 | """ |
| 1586 | Transform X to ordinal codes. |
| 1587 | |
| 1588 | Parameters |
| 1589 | ---------- |
| 1590 | X : array-like of shape (n_samples, n_features) |
| 1591 | The data to encode. |
| 1592 | |
| 1593 | Returns |
| 1594 | ------- |
| 1595 | X_out : ndarray of shape (n_samples, n_features) |
| 1596 | Transformed input. |
| 1597 | """ |
| 1598 | check_is_fitted(self, "categories_") |
| 1599 | X_int, X_mask = self._transform( |
| 1600 | X, |
| 1601 | handle_unknown=self.handle_unknown, |
| 1602 | ensure_all_finite="allow-nan", |
| 1603 | ignore_category_indices=self._missing_indices, |
| 1604 | ) |
| 1605 | X_trans = X_int.astype(self.dtype, copy=False) |
| 1606 | |
| 1607 | for cat_idx, missing_idx in self._missing_indices.items(): |
| 1608 | X_missing_mask = X_int[:, cat_idx] == missing_idx |
| 1609 | X_trans[X_missing_mask, cat_idx] = self.encoded_missing_value |
| 1610 | |
| 1611 | # create separate category for unknown values |
| 1612 | if self.handle_unknown == "use_encoded_value": |
| 1613 | X_trans[~X_mask] = self.unknown_value |
| 1614 | return X_trans |
| 1615 | |
| 1616 | def inverse_transform(self, X): |
| 1617 | """ |