Last active
April 17, 2021 14:36
-
-
Save DFoly/13b71998ef8112e67dcb0a3e5af803f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import pandas as pd | |
from typing import Dict, List | |
from sklearn.datasets import make_blobs | |
from sklearn.base import BaseEstimator, TransformerMixin | |
from sklearn.preprocessing import StandardScaler | |
from sklearn.pipeline import Pipeline | |
from sklearn.linear_model import LogisticRegression | |
class CustomImputer(BaseEstimator, TransformerMixin): | |
"""Impute missing data for numerical features.""" | |
def __init__(self, variables: List[str] = None) -> None: | |
if not isinstance(variables, list): | |
self.variables = [variables] | |
else: | |
self.variables = variables | |
def fit(self, X: pd.DataFrame, y: pd.Series = None) -> "CustomImputer": | |
self.imputer_dict_: Dict[str, float] = {} | |
for feature in self.variables: | |
self.imputer_dict_[feature] = X[feature].mean() | |
return self | |
def transform(self, X: pd.DataFrame) -> pd.DataFrame: | |
X = X.copy() | |
for feature in self.variables: | |
X[feature].fillna(self.imputer_dict_[feature], inplace=True) | |
return X | |
# generate some data | |
X, y = make_blobs(n_samples=10, centers=3, n_features=4, | |
random_state=0) | |
df = pd.DataFrame(X, columns = ['X1', 'X2', 'X3', 'X4']) | |
df['X1'].iloc[2:8] = np.nan # add missing values | |
missing_columns = df.columns[df.isnull().any()].values[0] | |
preprocessor = Pipeline(steps=[ | |
('imputer', CustomImputer(missing_columns)), | |
('scaler', StandardScaler())]) | |
lr = Pipeline(steps=[('preprocessor', preprocessor), | |
('classifier', LogisticRegression())]) | |
lr.fit(df, y) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment