Skip to content

Instantly share code, notes, and snippets.

@elena-roff
Created March 29, 2019 10:58
Show Gist options
  • Save elena-roff/b0e73b0aa760818e5e6259ed8ab88d42 to your computer and use it in GitHub Desktop.
Save elena-roff/b0e73b0aa760818e5e6259ed8ab88d42 to your computer and use it in GitHub Desktop.
Mask for filters on train feature set
filters = {
'field_1': <threshold value for the field1>,
'field_2': <threshold value for the field2>,
}
# creates a matrix of True's
mask = np.ones(features.shape[0], dtype=bool)
for field, threshold_value in filters.items():
# updates the matrix with
# applied and accumulated conditions
mask &= data[field] < threshold_value
# ---- USAGE ----
features = pd.DataFrame({'x': [1, 2, 3, 4, 5, 6], 'y': [3, 4, 5, 6, 7, 8]})
label = pd.DataFrame({'z': [1, 0, 1, 1, 0, 0]})
filters = {'x': 3, 'y': 6}
mask = np.ones(features.shape[0], dtype=bool)
for field, threshold_value in filters.items():
# updates the matrix with
# applied and accumulated conditions
mask &= features[field] < threshold_value
# filtered train set
features = features.loc[mask]
label = label.loc[mask]
features
# x y
# 0 1 3
# 1 2 4
label
# z
# 0 1
# 1 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment