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

Detect highly null columns #121

Merged
merged 22 commits into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
11 changes: 9 additions & 2 deletions evalml/models/auto_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
class AutoBase:
def __init__(self, problem_type, tuner, cv, objective, max_pipelines, max_time,
model_types, detect_label_leakage, start_iteration_callback,
add_result_callback, additional_objectives, random_state, verbose):
add_result_callback, additional_objectives, random_state, verbose,
angela97lin marked this conversation as resolved.
Show resolved Hide resolved
detect_highly_null, null_threshold):
if tuner is None:
tuner = SKOptTuner
self.objective = get_objective(objective)
Expand All @@ -30,8 +31,10 @@ def __init__(self, problem_type, tuner, cv, objective, max_pipelines, max_time,
self.add_result_callback = add_result_callback
self.cv = cv
self.verbose = verbose
self.logger = Logger(self.verbose)
self.detect_highly_null = detect_highly_null
angela97lin marked this conversation as resolved.
Show resolved Hide resolved
self.null_threshold = null_threshold

self.logger = Logger(self.verbose)
self.possible_pipelines = get_pipelines(problem_type=self.problem_type, model_types=model_types)
self.objective = get_objective(objective)

Expand Down Expand Up @@ -119,6 +122,10 @@ def fit(self, X, y, feature_types=None, raise_errors=False):
leaked = [str(k) for k in leaked.keys()]
self.logger.log("WARNING: Possible label leakage: %s" % ", ".join(leaked))

if self.detect_highly_null:
highly_null_columns = preprocessing.detect_highly_null(X, percent_threshold=self.null_threshold)
self.logger.log("WARNING: {} columns are at least {}% null.".format(', '.join(highly_null_columns), self.null_threshold * 100))

pbar = tqdm(range(self.max_pipelines), disable=not self.verbose, file=stdout, bar_format='{desc} {percentage:3.0f}%|{bar}| Elapsed:{elapsed}')
start = time.time()
for n in pbar:
Expand Down
8 changes: 6 additions & 2 deletions evalml/models/auto_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ def __init__(self,
add_result_callback=None,
additional_objectives=None,
random_state=0,
verbose=True):
verbose=True,
angela97lin marked this conversation as resolved.
Show resolved Hide resolved
detect_highly_null=True,
null_threshold=0.95):
"""Automated classifier pipeline search

Arguments:
Expand Down Expand Up @@ -86,7 +88,9 @@ def __init__(self,
add_result_callback=add_result_callback,
random_state=random_state,
verbose=verbose,
additional_objectives=additional_objectives
additional_objectives=additional_objectives,
detect_highly_null=detect_highly_null,
null_threshold=null_threshold
)

def set_problem_type(self, objective, multiclass):
Expand Down
8 changes: 6 additions & 2 deletions evalml/models/auto_regressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ def __init__(self,
add_result_callback=None,
additional_objectives=None,
random_state=0,
verbose=True):
verbose=True,
detect_highly_null=True,
null_threshold=0.95):
"""Automated regressors pipeline search

Arguments:
Expand Down Expand Up @@ -76,5 +78,7 @@ def __init__(self,
add_result_callback=add_result_callback,
random_state=random_state,
verbose=verbose,
additional_objectives=additional_objectives
additional_objectives=additional_objectives,
detect_highly_null=detect_highly_null,
null_threshold=null_threshold
)
16 changes: 16 additions & 0 deletions evalml/preprocessing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,19 @@ def detect_label_leakage(X, y, threshold=.95):
corrs = X.corrwith(y).abs()
out = corrs[corrs >= threshold]
return out.to_dict()


def detect_highly_null(X, percent_threshold=.95):
angela97lin marked this conversation as resolved.
Show resolved Hide resolved
""" Checks if there are any highly-null columns in a dataframe.

Args:
X (DataFrame) : features
percent_threshold(float): Require that percentage of non-null values to not be considered "highly-null", defaults to .95

Returns:
a set of features that are highly-null
"""
threshold = len(X) * percent_threshold
num_nonnan = X.count()
angela97lin marked this conversation as resolved.
Show resolved Hide resolved
filtered = num_nonnan[num_nonnan < threshold]
return (set(filtered.index))
17 changes: 17 additions & 0 deletions evalml/tests/preprocessing_tests/test_drop_null.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import numpy as np
import pandas as pd

from evalml.preprocessing import detect_highly_null


def test_detect_highly_null():
df = pd.DataFrame(np.random.random((100, 5)), columns=list("ABCDE"))
angela97lin marked this conversation as resolved.
Show resolved Hide resolved
df.loc[:11, 'A'] = np.nan
df.loc[:10, 'B'] = np.nan
df.loc[:30, 'C'] = np.nan
df.loc[:, 'D'] = np.nan
df.loc[:9, 'E'] = np.nan

expected = {'A', 'B', 'C', 'D'}
nan_dropped_df = detect_highly_null(df, percent_threshold=.9)
assert expected == nan_dropped_df