-
Notifications
You must be signed in to change notification settings - Fork 1
Distribution data check #21
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
Open
NabilFayak
wants to merge
8
commits into
main
Choose a base branch
from
distribution_data_check
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d63a71f
distibution_data_check init
15e07f7
release notes updated
6ff5fbf
adjusted to check for overall data not just target
7c3e224
lint fix
9abccac
added data checking logic
d40d5f5
linter
2fe1d7e
added some tests for normalizer and data check
670df80
lint fix
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
155 changes: 155 additions & 0 deletions
155
checkmates/data_checks/checks/distribution_data_check.py
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
"""Data check that checks if the target data contains certain distributions that may need to be transformed prior training to improve model performance.""" | ||
NabilFayak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import diptest | ||
import numpy as np | ||
import woodwork as ww | ||
|
||
from checkmates.data_checks import ( | ||
DataCheck, | ||
DataCheckActionCode, | ||
DataCheckActionOption, | ||
DataCheckError, | ||
DataCheckMessageCode, | ||
DataCheckWarning, | ||
) | ||
from checkmates.utils import infer_feature_types | ||
|
||
|
||
class DistributionDataCheck(DataCheck): | ||
"""Check if the overall data contains certain distributions that may need to be transformed prior training to improve model performance. Uses the skew test and yeojohnson transformation.""" | ||
|
||
def validate(self, X, y): | ||
"""Check if the overall data has a skewed or bimodal distribution. | ||
|
||
Args: | ||
X (pd.DataFrame, np.ndarray): Overall data to check for skewed or bimodal distributions. | ||
y (pd.Series, np.ndarray): Target data to check for underlying distributions. | ||
|
||
Returns: | ||
dict (DataCheckError): List with DataCheckErrors if certain distributions are found in the overall data. | ||
|
||
Examples: | ||
>>> import pandas as pd | ||
|
||
Features and target data that exhibit a skewed distribution will raise a warning for the user to transform the data. | ||
|
||
>>> X = [5, 7, 8, 9, 10, 11, 12, 15, 20] | ||
>>> data_check = DistributionDataCheck() | ||
>>> assert data_check.validate(X, y) == [ | ||
... { | ||
... "message": "Data may have a skewed distribution.", | ||
... "data_check_name": "DistributionDataCheck", | ||
... "level": "warning", | ||
... "code": "SKEWED_DISTRIBUTION", | ||
... "details": {"distribution type": "positive skew", "Skew Value": 0.7939, "Bimodal Coefficient": 1.0,}, | ||
... "action_options": [ | ||
... { | ||
... "code": "TRANSFORM_TARGET", | ||
NabilFayak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
... "data_check_name": "DistributionDataCheck", | ||
... "parameters": {}, | ||
... "metadata": { | ||
"is_skew": True, | ||
"transformation_strategy": "yeojohnson", | ||
... } | ||
... } | ||
... ] | ||
... } | ||
... ] | ||
... | ||
>>> X = pd.Series([1, 1, 1, 2, 2, 3, 4, 4, 5, 5, 5]) | ||
>>> assert target_check.validate(X, y) == [] | ||
... | ||
... | ||
>>> X = pd.Series(pd.date_range("1/1/21", periods=10)) | ||
>>> assert target_check.validate(X, y) == [ | ||
... { | ||
... "message": "Target is unsupported datetime type. Valid Woodwork logical types include: integer, double, age, age_fractional", | ||
NabilFayak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
... "data_check_name": "DistributionDataCheck", | ||
... "level": "error", | ||
... "details": {"columns": None, "rows": None, "unsupported_type": "datetime"}, | ||
... "code": "TARGET_UNSUPPORTED_TYPE", | ||
... "action_options": [] | ||
... } | ||
... ] | ||
""" | ||
messages = [] | ||
|
||
if y is None: | ||
messages.append( | ||
DataCheckError( | ||
message="Data is None", | ||
data_check_name=self.name, | ||
message_code=DataCheckMessageCode.TARGET_IS_NONE, | ||
details={}, | ||
).to_dict(), | ||
) | ||
return messages | ||
|
||
y = infer_feature_types(y) | ||
allowed_types = [ | ||
ww.logical_types.Integer.type_string, | ||
ww.logical_types.Double.type_string, | ||
ww.logical_types.Age.type_string, | ||
ww.logical_types.AgeFractional.type_string, | ||
] | ||
is_supported_type = y.ww.logical_type.type_string in allowed_types | ||
|
||
if not is_supported_type: | ||
messages.append( | ||
DataCheckError( | ||
message="Target is unsupported {} type. Valid Woodwork logical types include: {}".format( | ||
NabilFayak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
y.ww.logical_type.type_string, | ||
", ".join([ltype for ltype in allowed_types]), | ||
), | ||
data_check_name=self.name, | ||
message_code=DataCheckMessageCode.TARGET_UNSUPPORTED_TYPE, | ||
details={"unsupported_type": X.ww.logical_type.type_string}, | ||
).to_dict(), | ||
) | ||
return messages | ||
|
||
( | ||
is_skew, | ||
distribution_type, | ||
skew_value, | ||
coef, | ||
) = _detect_skew_distribution_helper(X) | ||
|
||
if is_skew: | ||
details = { | ||
"distribution type": distribution_type, | ||
"Skew Value": skew_value, | ||
"Bimodal Coefficient": coef, | ||
} | ||
messages.append( | ||
DataCheckWarning( | ||
message="Data may have a skewed distribution.", | ||
data_check_name=self.name, | ||
message_code=DataCheckMessageCode.SKEWED_DISTRIBUTION, | ||
details=details, | ||
action_options=[ | ||
DataCheckActionOption( | ||
DataCheckActionCode.TRANSFORM_TARGET, | ||
NabilFayak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
data_check_name=self.name, | ||
metadata={ | ||
"is_skew": True, | ||
"transformation_strategy": "yeojohnson", | ||
}, | ||
), | ||
], | ||
).to_dict(), | ||
) | ||
return messages | ||
|
||
|
||
def _detect_skew_distribution_helper(X): | ||
NabilFayak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Helper method to detect skewed or bimodal distribution. Returns boolean, distribution type, the skew value, and bimodal coefficient.""" | ||
skew_value = np.stats.skew(X) | ||
coef = diptest.diptest(X)[1] | ||
|
||
if coef < 0.05: | ||
NabilFayak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return True, "bimodal distribution", skew_value, coef | ||
if skew_value < -0.5: | ||
return True, "negative skew", skew_value, coef | ||
if skew_value > 0.5: | ||
return True, "positive skew", skew_value, coef | ||
return False, "no skew", skew_value, coef |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.