MCPcopy Create free account
hub / github.com/scikit-learn/scikit-learn / transform

Method transform

sklearn/preprocessing/_target_encoder.py:424–467  ·  view source on GitHub ↗

Transform X with the target encoding. This method internally uses the `encodings_` attribute learnt during :meth:`TargetEncoder.fit_transform` to transform test data. .. note:: `fit(X, y).transform(X)` does not equal `fit_transform(X, y)` because a :

(self, X)

Source from the content-addressed store, hash-verified

422 return X_out
423
424 def transform(self, X):
425 """Transform X with the target encoding.
426
427 This method internally uses the `encodings_` attribute learnt during
428 :meth:`TargetEncoder.fit_transform` to transform test data.
429
430 .. note::
431 `fit(X, y).transform(X)` does not equal `fit_transform(X, y)` because a
432 :term:`cross fitting` scheme is used in `fit_transform` for encoding.
433 See the :ref:`User Guide <target_encoder>` for details.
434
435 Parameters
436 ----------
437 X : array-like of shape (n_samples, n_features)
438 The data to determine the categories of each feature.
439
440 Returns
441 -------
442 X_trans : ndarray of shape (n_samples, n_features) or \
443 (n_samples, (n_features * n_classes))
444 Transformed input.
445 """
446 X_ordinal, X_known_mask = self._transform(
447 X, handle_unknown="ignore", ensure_all_finite="allow-nan"
448 )
449
450 # If 'multiclass' multiply axis=1 by num of classes else keep shape the same
451 if self.target_type_ == "multiclass":
452 X_out = np.empty(
453 (X_ordinal.shape[0], X_ordinal.shape[1] * len(self.classes_)),
454 dtype=np.float64,
455 )
456 else:
457 X_out = np.empty_like(X_ordinal, dtype=np.float64)
458
459 self._transform_X_ordinal(
460 X_out,
461 X_ordinal,
462 ~X_known_mask,
463 slice(None),
464 self.encodings_,
465 self.target_mean_,
466 )
467 return X_out
468
469 def _fit_encodings_all(self, X, y):
470 """Fit a target encoding with all the data."""

Calls 2

_transform_X_ordinalMethod · 0.95
_transformMethod · 0.45