End-to-End ML Project- Beginner friendly

73 / 94

Custom Transformers

Scikit-learn is one of the most widely used libraries in machine learning. Just think of a basic task regarding machine learning and search for it on the internet. Most probably you will find that there's a class or function already implemented in sklearn to perform that task. But what if it's not? What, if we want to perform that task in some other way?

In such cases, we'll have to create a transformer of our own. As we have seen, we have to perform several data transformation steps in the right order. So sklearn provides a Pipeline class to create a pipeline to execute all the steps in a sequential order. We use it a lot in machine learning. So, we have to make our custom transformer in a way such that it works seamlessly with sklearn functionalities such as Pipeline.

For that, all we need to do is to create a class and implement three methods- fit(), transform(), and fit_transform(). For the last one, we only have to add TransformerMixin as a base class. Now, let's implement the other two.

We'll also add BaseEstimator as a base class as it gives us two extra methods get_params() and set_params() which help much in automatic hyperparameter tuning.

We'll create a transformer class that adds the combined attributes which we discussed before.


No hints are availble for this assesment

Answer is not availble for this assesment

Loading comments...