-
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 6 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
158 changes: 158 additions & 0 deletions
158
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,158 @@ | ||
"""Data check that screens data for skewed or bimodal distrbutions prior to model training to ensure model performance is unaffected.""" | ||
|
||
from diptest import diptest | ||
from scipy.stats import skew | ||
|
||
from checkmates.data_checks import ( | ||
DataCheck, | ||
DataCheckActionCode, | ||
DataCheckActionOption, | ||
DataCheckMessageCode, | ||
DataCheckWarning, | ||
) | ||
|
||
|
||
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_FEATURES", | ||
... "data_check_name": "DistributionDataCheck", | ||
... "parameters": {}, | ||
... "metadata": { | ||
"is_skew": True, | ||
"transformation_strategy": "yeojohnson", | ||
... } | ||
... } | ||
... ] | ||
... } | ||
... ] | ||
""" | ||
messages = [] | ||
|
||
numeric_X = X.ww.select(["Integer", "Double"]) | ||
|
||
for col in numeric_X: | ||
( | ||
is_skew, | ||
distribution_type, | ||
skew_value, | ||
coef, | ||
) = _detect_skew_distribution_helper(col) | ||
|
||
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_FEATURES, | ||
data_check_name=self.name, | ||
metadata={ | ||
"is_skew": True, | ||
"transformation_strategy": "yeojohnson", | ||
"columns": col, | ||
}, | ||
), | ||
], | ||
).to_dict(), | ||
) | ||
return messages | ||
|
||
|
||
def _detect_skew_distribution_helper(X): | ||
"""Helper method to detect skewed or bimodal distribution. Returns boolean, distribution type, the skew value, and bimodal coefficient.""" | ||
skew_value = skew(X) | ||
coef = 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 | ||
|
||
|
||
# Testing Data to make sure skews are recognized-- successful | ||
NabilFayak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# import numpy as np | ||
# import pandas as pd | ||
# data = { | ||
# 'Column1': np.random.normal(0, 1, 1000), # Normally distributed data | ||
# 'Column2': np.random.exponential(1, 1000), # Right-skewed data | ||
# 'Column3': np.random.gamma(2, 2, 1000) # Right-skewed data | ||
# } | ||
|
||
# df = pd.DataFrame(data) | ||
# df.ww.init() | ||
# messages = [] | ||
|
||
# numeric_X = df.ww.select(["Integer", "Double"]) | ||
# print(numeric_X) | ||
# for col in numeric_X: | ||
# ( | ||
# is_skew, | ||
# distribution_type, | ||
# skew_value, | ||
# coef, | ||
# ) = _detect_skew_distribution_helper(numeric_X['Column2']) | ||
|
||
# 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="Distribution Data Check", | ||
# message_code=DataCheckMessageCode.SKEWED_DISTRIBUTION, | ||
# details=details, | ||
# action_options=[ | ||
# DataCheckActionOption( | ||
# DataCheckActionCode.TRANSFORM_FEATURES, | ||
# data_check_name="Distribution Data Check", | ||
# metadata={ | ||
# "is_skew": True, | ||
# "transformation_strategy": "yeojohnson", | ||
# "columns" : col | ||
# }, | ||
# ), | ||
# ], | ||
# ).to_dict(), | ||
# ) | ||
# print(messages) |
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
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.