January 2018
Beginner to intermediate
316 pages
7h 14m
English
First, we will utilize the scikit-learn TransformerMixin base class to create our own custom categorical imputer. This transformer (and all other custom transformers in this chapter) will work as an element in a pipeline with a fit and transform method.
The following code block will become very familiar throughout this chapter, so we will go over each line in detail:
from sklearn.base import TransformerMixin class CustomCategoryImputer(TransformerMixin): def __init__(self, cols=None): self.cols = cols def transform(self, df): X = df.copy() for col in self.cols: X[col].fillna(X[col].value_counts().index[0], inplace=True) return X def fit(self, *_): return self
There is a lot happening in this code block, so let's break ...
Read now
Unlock full access