-
Notifications
You must be signed in to change notification settings - Fork 4
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
feat(spark): model quality and drift for current - multiclass classification #74
Merged
rivamarco
merged 6 commits into
main
from
feature/ROS-285-model-quality-current-multiclass
Jul 2, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8759843
feat: add multiclass model quality calc for current
rivamarco e1c4f85
feat: add model quality in job
rivamarco e601aa5
feat: merge branch 'main' into feature/ROS-285-model-quality-current-…
rivamarco 252806b
feat: add drift and refactoring
rivamarco 2bd80c4
feat: merge main
rivamarco e20e3ac
fix: remove strange timezone from files
rivamarco 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
This file contains 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
File renamed without changes.
This file contains 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,79 @@ | ||
from pyspark.sql import SparkSession | ||
|
||
from metrics.chi2 import Chi2Test | ||
from metrics.ks import KolmogorovSmirnovTest | ||
from models.current_dataset import CurrentDataset | ||
from models.reference_dataset import ReferenceDataset | ||
|
||
|
||
class DriftCalculator: | ||
@staticmethod | ||
def calculate_drift( | ||
spark_session: SparkSession, | ||
reference_dataset: ReferenceDataset, | ||
current_dataset: CurrentDataset, | ||
): | ||
drift_result = dict() | ||
drift_result["feature_metrics"] = [] | ||
|
||
categorical_features = [ | ||
categorical.name | ||
for categorical in reference_dataset.model.get_categorical_features() | ||
] | ||
chi2 = Chi2Test( | ||
spark_session=spark_session, | ||
reference_data=reference_dataset.reference, | ||
current_data=current_dataset.current, | ||
) | ||
|
||
for column in categorical_features: | ||
feature_dict_to_append = { | ||
"feature_name": column, | ||
"drift_calc": { | ||
"type": "CHI2", | ||
}, | ||
} | ||
if ( | ||
reference_dataset.reference_count > 5 | ||
and current_dataset.current_count > 5 | ||
): | ||
result_tmp = chi2.test(column, column) | ||
feature_dict_to_append["drift_calc"]["value"] = float( | ||
result_tmp["pValue"] | ||
) | ||
feature_dict_to_append["drift_calc"]["has_drift"] = bool( | ||
result_tmp["pValue"] <= 0.05 | ||
) | ||
else: | ||
feature_dict_to_append["drift_calc"]["value"] = None | ||
feature_dict_to_append["drift_calc"]["has_drift"] = False | ||
drift_result["feature_metrics"].append(feature_dict_to_append) | ||
|
||
numerical_features = [ | ||
numerical.name | ||
for numerical in reference_dataset.model.get_numerical_features() | ||
] | ||
ks = KolmogorovSmirnovTest( | ||
reference_data=reference_dataset.reference, | ||
current_data=current_dataset.current, | ||
alpha=0.05, | ||
phi=0.004, | ||
) | ||
|
||
for column in numerical_features: | ||
feature_dict_to_append = { | ||
"feature_name": column, | ||
"drift_calc": { | ||
"type": "KS", | ||
}, | ||
} | ||
result_tmp = ks.test(column, column) | ||
feature_dict_to_append["drift_calc"]["value"] = float( | ||
result_tmp["ks_statistic"] | ||
) | ||
feature_dict_to_append["drift_calc"]["has_drift"] = bool( | ||
result_tmp["ks_statistic"] > result_tmp["critical_value"] | ||
) | ||
drift_result["feature_metrics"].append(feature_dict_to_append) | ||
|
||
return drift_result |
File renamed without changes.
This file contains 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 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
index is calculated both with reference and current