Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add min_impute and nan warning #423

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions healthcareai/common/transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

This module contains transformers for preprocessing data. Most operate on DataFrames and are named appropriately.
"""
import warnings
import numpy as np
import pandas as pd

Expand All @@ -15,16 +16,26 @@ class DataFrameImputer(TransformerMixin):
"""
Impute missing values in a dataframe.

Columns of dtype object or category (assumed categorical) are imputed with the mode (most frequent value in column).
Columns of dtype object or category (assumed categorical) are imputed
with the mode (most frequent value in column).

Columns of other types (assumed continuous) are imputed with mean of column.
"""

def __init__(self, impute=True, verbose=True):
def __init__(self, impute=True, max_impute=.5, verbose=True):
"""
Initiates the DataFrameImputer with imputation flag and threshold.

Args:
impute: Flag on whether to impute
max_impute: Maximum proportion of values to impute, will warn
the user if the threshold is passed
"""
self.impute = impute
self.object_columns = None
self.fill = None
self.verbose = verbose
self.max_impute = max_impute

def fit(self, X, y=None):
# Return if not imputing
Expand Down Expand Up @@ -54,7 +65,15 @@ def transform(self, X, y=None):
# Return if not imputing
if self.impute is False:
return X


#Warn users if %nan is too high
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be thrilled if this were factored out to a separate function with tests (and I'm happy to guide you through that if you'd like).

for c in X:
pct_impute = X[c].isnull().sum() / len(X)
if pct_impute > self.max_impute:
warnings.warn("'{0}' column is missing {1:.2f}% of data. Imputed "
"values may be invalid.".format(c, pct_impute*100),
RuntimeWarning)

result = X.fillna(self.fill)

for i in self.object_columns:
Expand Down