gitlabds 2.0.0__tar.gz → 2.1.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {gitlabds-2.0.0 → gitlabds-2.1.1}/PKG-INFO +150 -57
- {gitlabds-2.0.0 → gitlabds-2.1.1}/README.md +149 -56
- {gitlabds-2.0.0 → gitlabds-2.1.1}/gitlabds/__init__.py +2 -1
- gitlabds-2.1.1/gitlabds/baselines.py +365 -0
- {gitlabds-2.0.0 → gitlabds-2.1.1}/gitlabds/model_evaluator.py +8 -8
- gitlabds-2.1.1/gitlabds/monitoring_metrics.py +470 -0
- {gitlabds-2.0.0 → gitlabds-2.1.1}/gitlabds/trends.py +1 -1
- {gitlabds-2.0.0 → gitlabds-2.1.1}/gitlabds.egg-info/PKG-INFO +150 -57
- {gitlabds-2.0.0 → gitlabds-2.1.1}/gitlabds.egg-info/SOURCES.txt +2 -1
- {gitlabds-2.0.0 → gitlabds-2.1.1}/setup.py +2 -0
- gitlabds-2.0.0/gitlabds/model_metrics.py +0 -578
- {gitlabds-2.0.0 → gitlabds-2.1.1}/LICENSE +0 -0
- {gitlabds-2.0.0 → gitlabds-2.1.1}/gitlabds/config_generator.py +0 -0
- {gitlabds-2.0.0 → gitlabds-2.1.1}/gitlabds/dummy.py +0 -0
- {gitlabds-2.0.0 → gitlabds-2.1.1}/gitlabds/feature_reduction.py +0 -0
- {gitlabds-2.0.0 → gitlabds-2.1.1}/gitlabds/insights.py +0 -0
- {gitlabds-2.0.0 → gitlabds-2.1.1}/gitlabds/memory_optimization.py +0 -0
- {gitlabds-2.0.0 → gitlabds-2.1.1}/gitlabds/missing.py +0 -0
- {gitlabds-2.0.0 → gitlabds-2.1.1}/gitlabds/missing_check.py +0 -0
- {gitlabds-2.0.0 → gitlabds-2.1.1}/gitlabds/missing_fill.py +0 -0
- {gitlabds-2.0.0 → gitlabds-2.1.1}/gitlabds/outliers.py +0 -0
- {gitlabds-2.0.0 → gitlabds-2.1.1}/gitlabds/split_data.py +0 -0
- {gitlabds-2.0.0 → gitlabds-2.1.1}/gitlabds.egg-info/dependency_links.txt +0 -0
- {gitlabds-2.0.0 → gitlabds-2.1.1}/gitlabds.egg-info/requires.txt +0 -0
- {gitlabds-2.0.0 → gitlabds-2.1.1}/gitlabds.egg-info/top_level.txt +0 -0
- {gitlabds-2.0.0 → gitlabds-2.1.1}/setup.cfg +0 -0
- {gitlabds-2.0.0 → gitlabds-2.1.1}/tests/__init__.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: gitlabds
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.1.1
|
|
4
4
|
Summary: Gitlab Data Science and Modeling Tools
|
|
5
5
|
Home-page: https://gitlab.com/gitlab-data/gitlabds
|
|
6
6
|
Author: Kevin Dietz
|
|
@@ -593,13 +593,12 @@ config.to_yaml("churn_model_config.yaml")
|
|
|
593
593
|
|
|
594
594
|
### Model Evaluation
|
|
595
595
|
|
|
596
|
-
#### Comprehensive Evaluation
|
|
597
596
|
<details><summary> ModelEvaluator </summary>
|
|
598
597
|
|
|
599
598
|
#### Description
|
|
600
599
|
A comprehensive framework for evaluating machine learning models, supporting both classification (binary and multi-class) and regression models. It provides extensive evaluation metrics, visualizations, and feature importance analysis.
|
|
601
600
|
|
|
602
|
-
`gitlabds.ModelEvaluator(model, x_train, y_train, x_test, y_test, x_oot=None, y_oot=None, classification=True, algo=None,
|
|
601
|
+
`gitlabds.ModelEvaluator(model, x_train, y_train, x_test, y_test, x_oot=None, y_oot=None, classification=True, algo=None, f1_threshold=0.50, decile_n=10, top_features_n=20, show_all_classes=True, show_plots=True, save_plots=True, plot_dir='plots', plot_save_format='png', plot_save_dpi=300)`
|
|
603
602
|
|
|
604
603
|
#### Parameters:
|
|
605
604
|
- _**model**_ : The trained model to evaluate. Must have predict for regression and predict_proba method for classification
|
|
@@ -611,7 +610,7 @@ A comprehensive framework for evaluating machine learning models, supporting bot
|
|
|
611
610
|
- _**y_oot**_ : Optional out-of-time validation labels.
|
|
612
611
|
- _**classification**_ : Whether this is a classification model. If False, regression metrics will be used.
|
|
613
612
|
- _**algo**_ : Algorithm type for feature importance calculation. Options: 'xgb', 'rf', 'mars'. For other algorithms, use `None`
|
|
614
|
-
- _**
|
|
613
|
+
- _**f1_threshold**_ : Threshold for binary classification.
|
|
615
614
|
- _**decile_n**_ : Number of n-tiles for lift calculation. Defaults to 10 for deciles
|
|
616
615
|
- _**top_features_n**_ : Number of top features to display in visualizations.
|
|
617
616
|
- _**show_all_classes**_ : Whether to show metrics for all classes in multi-class classification.
|
|
@@ -674,58 +673,8 @@ results.feature_importance.to_csv("feature_importance.csv")
|
|
|
674
673
|
```
|
|
675
674
|
</details>
|
|
676
675
|
|
|
677
|
-
#### Metric Functions
|
|
678
|
-
<details><summary> Model Metrics </summary>
|
|
679
676
|
|
|
680
|
-
|
|
681
|
-
Display a variety of model metrics for linear and logistic predictive models.
|
|
682
|
-
|
|
683
|
-
`gitlabds.model_metrics(model, x_train, y_train, x_test, y_test, show_graphs=True, f_score=0.50, classification=True, algo=None, decile_n=10, top_features_n=20):`
|
|
684
|
-
|
|
685
|
-
#### Parameters:
|
|
686
|
-
- _**model**_ : model file from training
|
|
687
|
-
- _**x_train**_ : Training features DataFrame.
|
|
688
|
-
- _**y_train**_ : Training labels (Series or DataFrame).
|
|
689
|
-
- _**x_test**_ : Test features DataFrame.
|
|
690
|
-
- _**y_test**_ : Test labels (Series or DataFrame).
|
|
691
|
-
- _**show_graphs**_ : Whether to display plots and graphs.
|
|
692
|
-
- _**f_score**_ : Threshold for binary classification.
|
|
693
|
-
- _**classification**_ : Whether this is a classification model. If False, regression metrics will be used.
|
|
694
|
-
- _**algo**_ : Algorithm type for feature importance calculation. Options: 'xgb', 'rf', 'mars'.
|
|
695
|
-
- _**decile_n**_ : Number of n-tiles for lift calculation.
|
|
696
|
-
- _**top_features_n**_ : Number of top features to display in feature importance.
|
|
697
|
-
|
|
698
|
-
#### Returns
|
|
699
|
-
- For classification models: tuple of (metricx, lift, classification_metricx, top_features, decile_breaks)
|
|
700
|
-
- For regression models: tuple of (metricx, top_features)
|
|
701
|
-
|
|
702
|
-
#### Examples:
|
|
703
|
-
```python
|
|
704
|
-
# For a classification model
|
|
705
|
-
import gitlabds
|
|
706
|
-
metricx, lift, classification_metricx, top_features, decile_breaks = gitlabds.model_metrics(
|
|
707
|
-
model=my_classifier,
|
|
708
|
-
x_train=x_train,
|
|
709
|
-
y_train=y_train,
|
|
710
|
-
x_test=x_test,
|
|
711
|
-
y_test=y_test,
|
|
712
|
-
classification=True,
|
|
713
|
-
algo='xgb'
|
|
714
|
-
)
|
|
715
|
-
|
|
716
|
-
# For a regression model
|
|
717
|
-
metricx, top_features = gitlabds.model_metrics(
|
|
718
|
-
model=my_regressor,
|
|
719
|
-
x_train=x_train,
|
|
720
|
-
y_train=y_train,
|
|
721
|
-
x_test=x_test,
|
|
722
|
-
y_test=y_test,
|
|
723
|
-
classification=False
|
|
724
|
-
)
|
|
725
|
-
```
|
|
726
|
-
</details>
|
|
727
|
-
|
|
728
|
-
#### Insight Generation
|
|
677
|
+
### Insight Generation
|
|
729
678
|
<details><summary> Marginal Effects </summary>
|
|
730
679
|
|
|
731
680
|
#### Description
|
|
@@ -808,9 +757,154 @@ results = gitlabds.prescriptions(
|
|
|
808
757
|
```
|
|
809
758
|
</details>
|
|
810
759
|
|
|
760
|
+
### Model Monitoring
|
|
761
|
+
|
|
762
|
+
<details><summary> Generate Baseline Features </summary>
|
|
763
|
+
|
|
764
|
+
#### Description
|
|
765
|
+
Generate baseline feature distributions, importance scores, and drift thresholds in a single comprehensive artifact for model monitoring.
|
|
766
|
+
|
|
767
|
+
`gitlabds.generate_baseline_features(training_data, feature_importance_df, importance_method="shapley_values", n_bins=10, psi_warning=0.1, psi_critical=0.2, ks_warning=0.2, ks_critical=0.3, js_warning=0.1, js_critical=0.2, output_path="baseline_features.json"):`
|
|
768
|
+
|
|
769
|
+
#### Parameters:
|
|
770
|
+
- **_training_data_** : Training feature data DataFrame
|
|
771
|
+
- **_feature_importance_df_** : DataFrame with columns: feature, importance
|
|
772
|
+
- **_importance_method_** : Method used to calculate importance (default: "shapley_values")
|
|
773
|
+
- **_n_bins_** : Number of bins for numerical features (default: 10)
|
|
774
|
+
- **_psi_warning, psi_critical_** : PSI thresholds for warning and critical drift detection
|
|
775
|
+
- **_ks_warning, ks_critical_** : KS statistic thresholds for warning and critical drift detection
|
|
776
|
+
- **_js_warning, js_critical_** : JS divergence thresholds for warning and critical drift detection
|
|
777
|
+
- **_output_path_** : Path to save the JSON file
|
|
778
|
+
|
|
779
|
+
#### Returns
|
|
780
|
+
- None (saves baseline artifact to JSON file)
|
|
781
|
+
|
|
782
|
+
#### Examples:
|
|
783
|
+
```python
|
|
784
|
+
# Generate baseline features with default thresholds
|
|
785
|
+
import gitlabds
|
|
786
|
+
gitlabds.generate_baseline_features(
|
|
787
|
+
training_data=train_df,
|
|
788
|
+
feature_importance_df=importance_df,
|
|
789
|
+
importance_method="shapley_values",
|
|
790
|
+
output_path="model_baseline_features.json"
|
|
791
|
+
)
|
|
792
|
+
```
|
|
793
|
+
|
|
794
|
+
```python
|
|
795
|
+
# Generate with custom drift thresholds
|
|
796
|
+
gitlabds.generate_baseline_features(
|
|
797
|
+
training_data=train_df,
|
|
798
|
+
feature_importance_df=importance_df,
|
|
799
|
+
n_bins=15,
|
|
800
|
+
psi_warning=0.15,
|
|
801
|
+
psi_critical=0.25,
|
|
802
|
+
output_path="custom_baseline_features.json"
|
|
803
|
+
)
|
|
804
|
+
```
|
|
805
|
+
</details>
|
|
806
|
+
|
|
807
|
+
<details><summary> Generate Baseline Calibration </summary>
|
|
808
|
+
|
|
809
|
+
#### Description
|
|
810
|
+
Generate baseline calibration data with train + test curves, prediction statistics, and model configuration for monitoring model calibration drift.
|
|
811
|
+
|
|
812
|
+
`gitlabds.generate_baseline_calibration(train_predictions, train_actuals, test_predictions, test_actuals, model_configuration, n_bins=10, output_path="baseline_calibration.json"):`
|
|
813
|
+
|
|
814
|
+
#### Parameters:
|
|
815
|
+
- **_train_predictions_** : Training set predicted probabilities
|
|
816
|
+
- **_train_actuals_** : Training set actual binary outcomes (0/1)
|
|
817
|
+
- **_test_predictions_** : Test set predicted probabilities
|
|
818
|
+
- **_test_actuals_** : Test set actual binary outcomes (0/1)
|
|
819
|
+
- **_model_configuration_** : Dictionary of model configuration parameters
|
|
820
|
+
- **_n_bins_** : Number of bins for calibration curve (default: 10)
|
|
821
|
+
- **_prediction_drift_warning**_ : Warning level for prediction score drift (default: 0.10),
|
|
822
|
+
- **_prediction_drift_critical**_ : Critical level for prediction score drift (default: 0.20),
|
|
823
|
+
- **_output_path_** : Path to save the JSON file
|
|
824
|
+
|
|
825
|
+
#### Returns
|
|
826
|
+
- None (saves baseline calibration to JSON file)
|
|
827
|
+
|
|
828
|
+
#### Examples:
|
|
829
|
+
```python
|
|
830
|
+
# Generate baseline calibration
|
|
831
|
+
import gitlabds
|
|
832
|
+
model_config = {'f1_threshold': 0.15, 'random_state': 42}
|
|
833
|
+
|
|
834
|
+
gitlabds.generate_baseline_calibration(
|
|
835
|
+
train_predictions=y_train_pred,
|
|
836
|
+
train_actuals=y_train,
|
|
837
|
+
test_predictions=y_test_pred,
|
|
838
|
+
test_actuals=y_test,
|
|
839
|
+
model_configuration=model_config,
|
|
840
|
+
prediction_drift_warning=0.15,
|
|
841
|
+
prediction_drift_critical=0.25
|
|
842
|
+
output_path="model_baseline_calibration.json"
|
|
843
|
+
)
|
|
844
|
+
```
|
|
845
|
+
</details>
|
|
846
|
+
|
|
847
|
+
<details><summary> Calculate Monitoring Metrics </summary>
|
|
848
|
+
|
|
849
|
+
#### Description
|
|
850
|
+
Calculate all monitoring metrics including feature drift, prediction drift, and health status for a model scoring run.
|
|
851
|
+
|
|
852
|
+
`gitlabds.calculate_monitoring_metrics(run_id, model_name, sub_model, model_version, scoring_date, feature_df, predictions, baseline_metrics, importance_threshold_pct=0.05):`
|
|
853
|
+
|
|
854
|
+
#### Parameters:
|
|
855
|
+
- **_run_id_** : Unique identifier for this scoring run
|
|
856
|
+
- **_model_name_** : Model name (e.g., "propensity_model")
|
|
857
|
+
- **_sub_model_** : Sub model identifier
|
|
858
|
+
- **_model_version_** : Model version (e.g., "2.1")
|
|
859
|
+
- **_scoring_date_** : Date of scoring
|
|
860
|
+
- **_feature_df_** : Feature data used for scoring
|
|
861
|
+
- **_predictions_** : Model predictions (probabilities 0-1)
|
|
862
|
+
- **_baseline_metrics_** : Dictionary containing baseline_features and baseline_calibration JSON data
|
|
863
|
+
- **_importance_threshold_pct_** : Percentage of total importance required for a feature to be considered "important"
|
|
864
|
+
|
|
865
|
+
#### Returns
|
|
866
|
+
- Dictionary containing DataFrames for each table:
|
|
867
|
+
- 'scoring_summary': model_scoring_summary table
|
|
868
|
+
- 'feature_drift': model_feature_drift table
|
|
869
|
+
|
|
870
|
+
#### Examples:
|
|
871
|
+
```python
|
|
872
|
+
# Calculate monitoring metrics for a scoring run
|
|
873
|
+
import gitlabds
|
|
874
|
+
from datetime import datetime
|
|
875
|
+
|
|
876
|
+
# Load baseline metrics
|
|
877
|
+
with open('baseline_features.json', 'r') as f:
|
|
878
|
+
baseline_features = json.load(f)
|
|
879
|
+
with open('baseline_calibration.json', 'r') as f:
|
|
880
|
+
baseline_calibration = json.load(f)
|
|
881
|
+
|
|
882
|
+
baseline_metrics = {
|
|
883
|
+
'baseline_features': baseline_features,
|
|
884
|
+
'baseline_calibration': baseline_calibration
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
# Calculate metrics
|
|
888
|
+
results = gitlabds.calculate_monitoring_metrics(
|
|
889
|
+
run_id="scoring_run_123",
|
|
890
|
+
model_name="churn_prediction",
|
|
891
|
+
sub_model="high_value_customers",
|
|
892
|
+
model_version="2.1",
|
|
893
|
+
scoring_date=datetime.now(),
|
|
894
|
+
feature_df=current_features,
|
|
895
|
+
predictions=model_predictions,
|
|
896
|
+
baseline_metrics=baseline_metrics,
|
|
897
|
+
importance_threshold_pct=0.05
|
|
898
|
+
)
|
|
899
|
+
|
|
900
|
+
# Access results
|
|
901
|
+
scoring_summary = results['scoring_summary']
|
|
902
|
+
feature_drift = results['feature_drift']
|
|
903
|
+
```
|
|
904
|
+
</details>
|
|
905
|
+
|
|
811
906
|
### SQL and Trend Analysis
|
|
812
907
|
|
|
813
|
-
#### SQL Generation
|
|
814
908
|
<details><summary> SQL Trend Query Generator </summary>
|
|
815
909
|
|
|
816
910
|
#### Description
|
|
@@ -884,7 +978,6 @@ sql = gitlabds.generate_sql_trend_query(
|
|
|
884
978
|
```
|
|
885
979
|
</details>
|
|
886
980
|
|
|
887
|
-
#### Trend Analysis
|
|
888
981
|
<details><summary> Trend Analysis </summary>
|
|
889
982
|
|
|
890
983
|
#### Description
|
|
@@ -573,13 +573,12 @@ config.to_yaml("churn_model_config.yaml")
|
|
|
573
573
|
|
|
574
574
|
### Model Evaluation
|
|
575
575
|
|
|
576
|
-
#### Comprehensive Evaluation
|
|
577
576
|
<details><summary> ModelEvaluator </summary>
|
|
578
577
|
|
|
579
578
|
#### Description
|
|
580
579
|
A comprehensive framework for evaluating machine learning models, supporting both classification (binary and multi-class) and regression models. It provides extensive evaluation metrics, visualizations, and feature importance analysis.
|
|
581
580
|
|
|
582
|
-
`gitlabds.ModelEvaluator(model, x_train, y_train, x_test, y_test, x_oot=None, y_oot=None, classification=True, algo=None,
|
|
581
|
+
`gitlabds.ModelEvaluator(model, x_train, y_train, x_test, y_test, x_oot=None, y_oot=None, classification=True, algo=None, f1_threshold=0.50, decile_n=10, top_features_n=20, show_all_classes=True, show_plots=True, save_plots=True, plot_dir='plots', plot_save_format='png', plot_save_dpi=300)`
|
|
583
582
|
|
|
584
583
|
#### Parameters:
|
|
585
584
|
- _**model**_ : The trained model to evaluate. Must have predict for regression and predict_proba method for classification
|
|
@@ -591,7 +590,7 @@ A comprehensive framework for evaluating machine learning models, supporting bot
|
|
|
591
590
|
- _**y_oot**_ : Optional out-of-time validation labels.
|
|
592
591
|
- _**classification**_ : Whether this is a classification model. If False, regression metrics will be used.
|
|
593
592
|
- _**algo**_ : Algorithm type for feature importance calculation. Options: 'xgb', 'rf', 'mars'. For other algorithms, use `None`
|
|
594
|
-
- _**
|
|
593
|
+
- _**f1_threshold**_ : Threshold for binary classification.
|
|
595
594
|
- _**decile_n**_ : Number of n-tiles for lift calculation. Defaults to 10 for deciles
|
|
596
595
|
- _**top_features_n**_ : Number of top features to display in visualizations.
|
|
597
596
|
- _**show_all_classes**_ : Whether to show metrics for all classes in multi-class classification.
|
|
@@ -654,58 +653,8 @@ results.feature_importance.to_csv("feature_importance.csv")
|
|
|
654
653
|
```
|
|
655
654
|
</details>
|
|
656
655
|
|
|
657
|
-
#### Metric Functions
|
|
658
|
-
<details><summary> Model Metrics </summary>
|
|
659
656
|
|
|
660
|
-
|
|
661
|
-
Display a variety of model metrics for linear and logistic predictive models.
|
|
662
|
-
|
|
663
|
-
`gitlabds.model_metrics(model, x_train, y_train, x_test, y_test, show_graphs=True, f_score=0.50, classification=True, algo=None, decile_n=10, top_features_n=20):`
|
|
664
|
-
|
|
665
|
-
#### Parameters:
|
|
666
|
-
- _**model**_ : model file from training
|
|
667
|
-
- _**x_train**_ : Training features DataFrame.
|
|
668
|
-
- _**y_train**_ : Training labels (Series or DataFrame).
|
|
669
|
-
- _**x_test**_ : Test features DataFrame.
|
|
670
|
-
- _**y_test**_ : Test labels (Series or DataFrame).
|
|
671
|
-
- _**show_graphs**_ : Whether to display plots and graphs.
|
|
672
|
-
- _**f_score**_ : Threshold for binary classification.
|
|
673
|
-
- _**classification**_ : Whether this is a classification model. If False, regression metrics will be used.
|
|
674
|
-
- _**algo**_ : Algorithm type for feature importance calculation. Options: 'xgb', 'rf', 'mars'.
|
|
675
|
-
- _**decile_n**_ : Number of n-tiles for lift calculation.
|
|
676
|
-
- _**top_features_n**_ : Number of top features to display in feature importance.
|
|
677
|
-
|
|
678
|
-
#### Returns
|
|
679
|
-
- For classification models: tuple of (metricx, lift, classification_metricx, top_features, decile_breaks)
|
|
680
|
-
- For regression models: tuple of (metricx, top_features)
|
|
681
|
-
|
|
682
|
-
#### Examples:
|
|
683
|
-
```python
|
|
684
|
-
# For a classification model
|
|
685
|
-
import gitlabds
|
|
686
|
-
metricx, lift, classification_metricx, top_features, decile_breaks = gitlabds.model_metrics(
|
|
687
|
-
model=my_classifier,
|
|
688
|
-
x_train=x_train,
|
|
689
|
-
y_train=y_train,
|
|
690
|
-
x_test=x_test,
|
|
691
|
-
y_test=y_test,
|
|
692
|
-
classification=True,
|
|
693
|
-
algo='xgb'
|
|
694
|
-
)
|
|
695
|
-
|
|
696
|
-
# For a regression model
|
|
697
|
-
metricx, top_features = gitlabds.model_metrics(
|
|
698
|
-
model=my_regressor,
|
|
699
|
-
x_train=x_train,
|
|
700
|
-
y_train=y_train,
|
|
701
|
-
x_test=x_test,
|
|
702
|
-
y_test=y_test,
|
|
703
|
-
classification=False
|
|
704
|
-
)
|
|
705
|
-
```
|
|
706
|
-
</details>
|
|
707
|
-
|
|
708
|
-
#### Insight Generation
|
|
657
|
+
### Insight Generation
|
|
709
658
|
<details><summary> Marginal Effects </summary>
|
|
710
659
|
|
|
711
660
|
#### Description
|
|
@@ -788,9 +737,154 @@ results = gitlabds.prescriptions(
|
|
|
788
737
|
```
|
|
789
738
|
</details>
|
|
790
739
|
|
|
740
|
+
### Model Monitoring
|
|
741
|
+
|
|
742
|
+
<details><summary> Generate Baseline Features </summary>
|
|
743
|
+
|
|
744
|
+
#### Description
|
|
745
|
+
Generate baseline feature distributions, importance scores, and drift thresholds in a single comprehensive artifact for model monitoring.
|
|
746
|
+
|
|
747
|
+
`gitlabds.generate_baseline_features(training_data, feature_importance_df, importance_method="shapley_values", n_bins=10, psi_warning=0.1, psi_critical=0.2, ks_warning=0.2, ks_critical=0.3, js_warning=0.1, js_critical=0.2, output_path="baseline_features.json"):`
|
|
748
|
+
|
|
749
|
+
#### Parameters:
|
|
750
|
+
- **_training_data_** : Training feature data DataFrame
|
|
751
|
+
- **_feature_importance_df_** : DataFrame with columns: feature, importance
|
|
752
|
+
- **_importance_method_** : Method used to calculate importance (default: "shapley_values")
|
|
753
|
+
- **_n_bins_** : Number of bins for numerical features (default: 10)
|
|
754
|
+
- **_psi_warning, psi_critical_** : PSI thresholds for warning and critical drift detection
|
|
755
|
+
- **_ks_warning, ks_critical_** : KS statistic thresholds for warning and critical drift detection
|
|
756
|
+
- **_js_warning, js_critical_** : JS divergence thresholds for warning and critical drift detection
|
|
757
|
+
- **_output_path_** : Path to save the JSON file
|
|
758
|
+
|
|
759
|
+
#### Returns
|
|
760
|
+
- None (saves baseline artifact to JSON file)
|
|
761
|
+
|
|
762
|
+
#### Examples:
|
|
763
|
+
```python
|
|
764
|
+
# Generate baseline features with default thresholds
|
|
765
|
+
import gitlabds
|
|
766
|
+
gitlabds.generate_baseline_features(
|
|
767
|
+
training_data=train_df,
|
|
768
|
+
feature_importance_df=importance_df,
|
|
769
|
+
importance_method="shapley_values",
|
|
770
|
+
output_path="model_baseline_features.json"
|
|
771
|
+
)
|
|
772
|
+
```
|
|
773
|
+
|
|
774
|
+
```python
|
|
775
|
+
# Generate with custom drift thresholds
|
|
776
|
+
gitlabds.generate_baseline_features(
|
|
777
|
+
training_data=train_df,
|
|
778
|
+
feature_importance_df=importance_df,
|
|
779
|
+
n_bins=15,
|
|
780
|
+
psi_warning=0.15,
|
|
781
|
+
psi_critical=0.25,
|
|
782
|
+
output_path="custom_baseline_features.json"
|
|
783
|
+
)
|
|
784
|
+
```
|
|
785
|
+
</details>
|
|
786
|
+
|
|
787
|
+
<details><summary> Generate Baseline Calibration </summary>
|
|
788
|
+
|
|
789
|
+
#### Description
|
|
790
|
+
Generate baseline calibration data with train + test curves, prediction statistics, and model configuration for monitoring model calibration drift.
|
|
791
|
+
|
|
792
|
+
`gitlabds.generate_baseline_calibration(train_predictions, train_actuals, test_predictions, test_actuals, model_configuration, n_bins=10, output_path="baseline_calibration.json"):`
|
|
793
|
+
|
|
794
|
+
#### Parameters:
|
|
795
|
+
- **_train_predictions_** : Training set predicted probabilities
|
|
796
|
+
- **_train_actuals_** : Training set actual binary outcomes (0/1)
|
|
797
|
+
- **_test_predictions_** : Test set predicted probabilities
|
|
798
|
+
- **_test_actuals_** : Test set actual binary outcomes (0/1)
|
|
799
|
+
- **_model_configuration_** : Dictionary of model configuration parameters
|
|
800
|
+
- **_n_bins_** : Number of bins for calibration curve (default: 10)
|
|
801
|
+
- **_prediction_drift_warning**_ : Warning level for prediction score drift (default: 0.10),
|
|
802
|
+
- **_prediction_drift_critical**_ : Critical level for prediction score drift (default: 0.20),
|
|
803
|
+
- **_output_path_** : Path to save the JSON file
|
|
804
|
+
|
|
805
|
+
#### Returns
|
|
806
|
+
- None (saves baseline calibration to JSON file)
|
|
807
|
+
|
|
808
|
+
#### Examples:
|
|
809
|
+
```python
|
|
810
|
+
# Generate baseline calibration
|
|
811
|
+
import gitlabds
|
|
812
|
+
model_config = {'f1_threshold': 0.15, 'random_state': 42}
|
|
813
|
+
|
|
814
|
+
gitlabds.generate_baseline_calibration(
|
|
815
|
+
train_predictions=y_train_pred,
|
|
816
|
+
train_actuals=y_train,
|
|
817
|
+
test_predictions=y_test_pred,
|
|
818
|
+
test_actuals=y_test,
|
|
819
|
+
model_configuration=model_config,
|
|
820
|
+
prediction_drift_warning=0.15,
|
|
821
|
+
prediction_drift_critical=0.25
|
|
822
|
+
output_path="model_baseline_calibration.json"
|
|
823
|
+
)
|
|
824
|
+
```
|
|
825
|
+
</details>
|
|
826
|
+
|
|
827
|
+
<details><summary> Calculate Monitoring Metrics </summary>
|
|
828
|
+
|
|
829
|
+
#### Description
|
|
830
|
+
Calculate all monitoring metrics including feature drift, prediction drift, and health status for a model scoring run.
|
|
831
|
+
|
|
832
|
+
`gitlabds.calculate_monitoring_metrics(run_id, model_name, sub_model, model_version, scoring_date, feature_df, predictions, baseline_metrics, importance_threshold_pct=0.05):`
|
|
833
|
+
|
|
834
|
+
#### Parameters:
|
|
835
|
+
- **_run_id_** : Unique identifier for this scoring run
|
|
836
|
+
- **_model_name_** : Model name (e.g., "propensity_model")
|
|
837
|
+
- **_sub_model_** : Sub model identifier
|
|
838
|
+
- **_model_version_** : Model version (e.g., "2.1")
|
|
839
|
+
- **_scoring_date_** : Date of scoring
|
|
840
|
+
- **_feature_df_** : Feature data used for scoring
|
|
841
|
+
- **_predictions_** : Model predictions (probabilities 0-1)
|
|
842
|
+
- **_baseline_metrics_** : Dictionary containing baseline_features and baseline_calibration JSON data
|
|
843
|
+
- **_importance_threshold_pct_** : Percentage of total importance required for a feature to be considered "important"
|
|
844
|
+
|
|
845
|
+
#### Returns
|
|
846
|
+
- Dictionary containing DataFrames for each table:
|
|
847
|
+
- 'scoring_summary': model_scoring_summary table
|
|
848
|
+
- 'feature_drift': model_feature_drift table
|
|
849
|
+
|
|
850
|
+
#### Examples:
|
|
851
|
+
```python
|
|
852
|
+
# Calculate monitoring metrics for a scoring run
|
|
853
|
+
import gitlabds
|
|
854
|
+
from datetime import datetime
|
|
855
|
+
|
|
856
|
+
# Load baseline metrics
|
|
857
|
+
with open('baseline_features.json', 'r') as f:
|
|
858
|
+
baseline_features = json.load(f)
|
|
859
|
+
with open('baseline_calibration.json', 'r') as f:
|
|
860
|
+
baseline_calibration = json.load(f)
|
|
861
|
+
|
|
862
|
+
baseline_metrics = {
|
|
863
|
+
'baseline_features': baseline_features,
|
|
864
|
+
'baseline_calibration': baseline_calibration
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
# Calculate metrics
|
|
868
|
+
results = gitlabds.calculate_monitoring_metrics(
|
|
869
|
+
run_id="scoring_run_123",
|
|
870
|
+
model_name="churn_prediction",
|
|
871
|
+
sub_model="high_value_customers",
|
|
872
|
+
model_version="2.1",
|
|
873
|
+
scoring_date=datetime.now(),
|
|
874
|
+
feature_df=current_features,
|
|
875
|
+
predictions=model_predictions,
|
|
876
|
+
baseline_metrics=baseline_metrics,
|
|
877
|
+
importance_threshold_pct=0.05
|
|
878
|
+
)
|
|
879
|
+
|
|
880
|
+
# Access results
|
|
881
|
+
scoring_summary = results['scoring_summary']
|
|
882
|
+
feature_drift = results['feature_drift']
|
|
883
|
+
```
|
|
884
|
+
</details>
|
|
885
|
+
|
|
791
886
|
### SQL and Trend Analysis
|
|
792
887
|
|
|
793
|
-
#### SQL Generation
|
|
794
888
|
<details><summary> SQL Trend Query Generator </summary>
|
|
795
889
|
|
|
796
890
|
#### Description
|
|
@@ -864,7 +958,6 @@ sql = gitlabds.generate_sql_trend_query(
|
|
|
864
958
|
```
|
|
865
959
|
</details>
|
|
866
960
|
|
|
867
|
-
#### Trend Analysis
|
|
868
961
|
<details><summary> Trend Analysis </summary>
|
|
869
962
|
|
|
870
963
|
#### Description
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
from .baselines import generate_baseline_features, generate_baseline_calibration
|
|
1
2
|
from .config_generator import ConfigGenerator
|
|
2
3
|
from .dummy import dummy_code, dummy_top, apply_dummy
|
|
3
4
|
from .feature_reduction import drop_categorical, remove_low_variation, remove_outcome_proxies, correlation_reduction
|
|
@@ -5,7 +6,7 @@ from .insights import marginal_effects, prescriptions
|
|
|
5
6
|
from .memory_optimization import reduce_memory_usage, sparse_encode_df, identify_categorical_candidates, compact_categorical, memory_profile_df, memory_optimization
|
|
6
7
|
from .missing import missing_values, apply_missing_values
|
|
7
8
|
from .model_evaluator import ModelEvaluator
|
|
8
|
-
from .
|
|
9
|
+
from .monitoring_metrics import calculate_monitoring_metrics, calculate_baseline_prediction_distribution, calculate_prediction_distribution, calculate_distribution_shift, calculate_psi_numerical, calculate_psi_categorical
|
|
9
10
|
from .outliers import mad_outliers, apply_outliers
|
|
10
11
|
from .split_data import split_data
|
|
11
12
|
from .trends import generate_sql_trend_query, trend_analysis
|