This is a follow up question to: What's the difference between fit and fit_transform in scikit-learn models?

I want to know why should we use fit at all when we have fit_transform which is much faster than using fit and transform separately? After all we will always transform the training data after fitting it. Do we have any use of fit all by itself?

  • $\begingroup$ I don't think this is a duplicate; it's a follow-up. Given that fit_transform=fit-then-transform, and you usually fit_transform training data then transform test data, why does fit exist as a separate method (or when/why should you use it)? $\endgroup$ – Ben Reiniger Apr 12 at 20:13

It probably is fairly rare to need to use fit and not instead fit_transform for a sklearn transformer. It nevertheless makes sense to keep the method separate: fitting a transformer is learning relevant information about the data, while transforming produces an altered dataset. Fitting still makes sense for sklearn predictors, and only some of those (particularly clusterers and outlier detectors) provide a combined fit_predict.

I can think of at least one instance where a transformer gets fitted but does not (immediately) transform data, but it is internal. In KBinsDiscretizer, if encode='onehot', then an internal instance of OneHotEncoder is created, and at fit time for the discretizer, the encoder is fitted (to dummy data) just to prepare it to transform future data. Transforming the data given to KBinsDiscretizer.fit would be wasteful at this point.

Finally, one comment on your post:

we have fit_transform which is much faster than using fit and transform separately

In most (but not all) cases, fit_transform is literally the same as fit(X, y).transform(X), so this should not be faster.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.