text_plain_output_1.png image_output_1.png
from sklearn.base import BaseEstimator, TransformerMixin

from typing import Tuple

import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

df_drug = pd.read_csv('../input/drugsets/drug200.csv')
df_drug

def find_boxplot_boundaries(
    col: pd.Series, Na_to_K_coeff: float = 1.5
) -> Tuple[float, float]:
    """Findx minimum and maximum in boxplot.

    Args:
        col: a pandas serires of input.
        Na_to_K_coeff: Na_to_K coefficient in box plot
    """
    Q1 = col.quantile(0.25)
    Q3 = col.quantile(0.75)
    IQR = Q3 - Q1
    lower = Q1 - Na_to_K_coeff * IQR
    upper = Q3 + Na_to_K_coeff * IQR
    return lower, upper

class BoxplotOutlierClipper(BaseEstimator, TransformerMixin):
    def __init__(self, Na_to_K_coeff: float = 1.5):
        self.Na_to_K = Na_to_K_coeff
        self.lower = None
        self.upper = None

    def fit(self, X: pd.Series):
        self.lower, self.upper = find_boxplot_boundaries(X, self.Na_to_K)
        return self

    def transform(self, X):
        return X.clip(self.lower, self.upper)

X_train = pd.get_dummies(X_train)
X_test = pd.get_dummies(X_test)

X_train