churnkit 0.75.0a1__py3-none-any.whl
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.
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/00_start_here.ipynb +647 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/01_data_discovery.ipynb +1165 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/01a_a_temporal_text_deep_dive.ipynb +961 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/01a_temporal_deep_dive.ipynb +1690 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/01b_temporal_quality.ipynb +679 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/01c_temporal_patterns.ipynb +3305 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/01d_event_aggregation.ipynb +1463 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/02_column_deep_dive.ipynb +1430 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/02a_text_columns_deep_dive.ipynb +854 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/03_quality_assessment.ipynb +1639 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/04_relationship_analysis.ipynb +1890 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/05_multi_dataset.ipynb +1457 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/06_feature_opportunities.ipynb +1624 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/07_modeling_readiness.ipynb +780 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/08_baseline_experiments.ipynb +979 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/09_business_alignment.ipynb +572 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/10_spec_generation.ipynb +1179 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/11_scoring_validation.ipynb +1418 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/12_view_documentation.ipynb +151 -0
- churnkit-0.75.0a1.dist-info/METADATA +229 -0
- churnkit-0.75.0a1.dist-info/RECORD +302 -0
- churnkit-0.75.0a1.dist-info/WHEEL +4 -0
- churnkit-0.75.0a1.dist-info/entry_points.txt +2 -0
- churnkit-0.75.0a1.dist-info/licenses/LICENSE +202 -0
- customer_retention/__init__.py +37 -0
- customer_retention/analysis/__init__.py +0 -0
- customer_retention/analysis/auto_explorer/__init__.py +62 -0
- customer_retention/analysis/auto_explorer/exploration_manager.py +470 -0
- customer_retention/analysis/auto_explorer/explorer.py +258 -0
- customer_retention/analysis/auto_explorer/findings.py +291 -0
- customer_retention/analysis/auto_explorer/layered_recommendations.py +485 -0
- customer_retention/analysis/auto_explorer/recommendation_builder.py +148 -0
- customer_retention/analysis/auto_explorer/recommendations.py +418 -0
- customer_retention/analysis/business/__init__.py +26 -0
- customer_retention/analysis/business/ab_test_designer.py +144 -0
- customer_retention/analysis/business/fairness_analyzer.py +166 -0
- customer_retention/analysis/business/intervention_matcher.py +121 -0
- customer_retention/analysis/business/report_generator.py +222 -0
- customer_retention/analysis/business/risk_profile.py +199 -0
- customer_retention/analysis/business/roi_analyzer.py +139 -0
- customer_retention/analysis/diagnostics/__init__.py +20 -0
- customer_retention/analysis/diagnostics/calibration_analyzer.py +133 -0
- customer_retention/analysis/diagnostics/cv_analyzer.py +144 -0
- customer_retention/analysis/diagnostics/error_analyzer.py +107 -0
- customer_retention/analysis/diagnostics/leakage_detector.py +394 -0
- customer_retention/analysis/diagnostics/noise_tester.py +140 -0
- customer_retention/analysis/diagnostics/overfitting_analyzer.py +190 -0
- customer_retention/analysis/diagnostics/segment_analyzer.py +122 -0
- customer_retention/analysis/discovery/__init__.py +8 -0
- customer_retention/analysis/discovery/config_generator.py +49 -0
- customer_retention/analysis/discovery/discovery_flow.py +19 -0
- customer_retention/analysis/discovery/type_inferencer.py +147 -0
- customer_retention/analysis/interpretability/__init__.py +13 -0
- customer_retention/analysis/interpretability/cohort_analyzer.py +185 -0
- customer_retention/analysis/interpretability/counterfactual.py +175 -0
- customer_retention/analysis/interpretability/individual_explainer.py +141 -0
- customer_retention/analysis/interpretability/pdp_generator.py +103 -0
- customer_retention/analysis/interpretability/shap_explainer.py +106 -0
- customer_retention/analysis/jupyter_save_hook.py +28 -0
- customer_retention/analysis/notebook_html_exporter.py +136 -0
- customer_retention/analysis/notebook_progress.py +60 -0
- customer_retention/analysis/plotly_preprocessor.py +154 -0
- customer_retention/analysis/recommendations/__init__.py +54 -0
- customer_retention/analysis/recommendations/base.py +158 -0
- customer_retention/analysis/recommendations/cleaning/__init__.py +11 -0
- customer_retention/analysis/recommendations/cleaning/consistency.py +107 -0
- customer_retention/analysis/recommendations/cleaning/deduplicate.py +94 -0
- customer_retention/analysis/recommendations/cleaning/impute.py +67 -0
- customer_retention/analysis/recommendations/cleaning/outlier.py +71 -0
- customer_retention/analysis/recommendations/datetime/__init__.py +3 -0
- customer_retention/analysis/recommendations/datetime/extract.py +149 -0
- customer_retention/analysis/recommendations/encoding/__init__.py +3 -0
- customer_retention/analysis/recommendations/encoding/categorical.py +114 -0
- customer_retention/analysis/recommendations/pipeline.py +74 -0
- customer_retention/analysis/recommendations/registry.py +76 -0
- customer_retention/analysis/recommendations/selection/__init__.py +3 -0
- customer_retention/analysis/recommendations/selection/drop_column.py +56 -0
- customer_retention/analysis/recommendations/transform/__init__.py +4 -0
- customer_retention/analysis/recommendations/transform/power.py +94 -0
- customer_retention/analysis/recommendations/transform/scale.py +112 -0
- customer_retention/analysis/visualization/__init__.py +15 -0
- customer_retention/analysis/visualization/chart_builder.py +2619 -0
- customer_retention/analysis/visualization/console.py +122 -0
- customer_retention/analysis/visualization/display.py +171 -0
- customer_retention/analysis/visualization/number_formatter.py +36 -0
- customer_retention/artifacts/__init__.py +3 -0
- customer_retention/artifacts/fit_artifact_registry.py +146 -0
- customer_retention/cli.py +93 -0
- customer_retention/core/__init__.py +0 -0
- customer_retention/core/compat/__init__.py +193 -0
- customer_retention/core/compat/detection.py +99 -0
- customer_retention/core/compat/ops.py +48 -0
- customer_retention/core/compat/pandas_backend.py +57 -0
- customer_retention/core/compat/spark_backend.py +75 -0
- customer_retention/core/components/__init__.py +11 -0
- customer_retention/core/components/base.py +79 -0
- customer_retention/core/components/components/__init__.py +13 -0
- customer_retention/core/components/components/deployer.py +26 -0
- customer_retention/core/components/components/explainer.py +26 -0
- customer_retention/core/components/components/feature_eng.py +33 -0
- customer_retention/core/components/components/ingester.py +34 -0
- customer_retention/core/components/components/profiler.py +34 -0
- customer_retention/core/components/components/trainer.py +38 -0
- customer_retention/core/components/components/transformer.py +36 -0
- customer_retention/core/components/components/validator.py +37 -0
- customer_retention/core/components/enums.py +33 -0
- customer_retention/core/components/orchestrator.py +94 -0
- customer_retention/core/components/registry.py +59 -0
- customer_retention/core/config/__init__.py +39 -0
- customer_retention/core/config/column_config.py +95 -0
- customer_retention/core/config/experiments.py +71 -0
- customer_retention/core/config/pipeline_config.py +117 -0
- customer_retention/core/config/source_config.py +83 -0
- customer_retention/core/utils/__init__.py +28 -0
- customer_retention/core/utils/leakage.py +85 -0
- customer_retention/core/utils/severity.py +53 -0
- customer_retention/core/utils/statistics.py +90 -0
- customer_retention/generators/__init__.py +0 -0
- customer_retention/generators/notebook_generator/__init__.py +167 -0
- customer_retention/generators/notebook_generator/base.py +55 -0
- customer_retention/generators/notebook_generator/cell_builder.py +49 -0
- customer_retention/generators/notebook_generator/config.py +47 -0
- customer_retention/generators/notebook_generator/databricks_generator.py +48 -0
- customer_retention/generators/notebook_generator/local_generator.py +48 -0
- customer_retention/generators/notebook_generator/project_init.py +174 -0
- customer_retention/generators/notebook_generator/runner.py +150 -0
- customer_retention/generators/notebook_generator/script_generator.py +110 -0
- customer_retention/generators/notebook_generator/stages/__init__.py +19 -0
- customer_retention/generators/notebook_generator/stages/base_stage.py +86 -0
- customer_retention/generators/notebook_generator/stages/s01_ingestion.py +100 -0
- customer_retention/generators/notebook_generator/stages/s02_profiling.py +95 -0
- customer_retention/generators/notebook_generator/stages/s03_cleaning.py +180 -0
- customer_retention/generators/notebook_generator/stages/s04_transformation.py +165 -0
- customer_retention/generators/notebook_generator/stages/s05_feature_engineering.py +115 -0
- customer_retention/generators/notebook_generator/stages/s06_feature_selection.py +97 -0
- customer_retention/generators/notebook_generator/stages/s07_model_training.py +176 -0
- customer_retention/generators/notebook_generator/stages/s08_deployment.py +81 -0
- customer_retention/generators/notebook_generator/stages/s09_monitoring.py +112 -0
- customer_retention/generators/notebook_generator/stages/s10_batch_inference.py +642 -0
- customer_retention/generators/notebook_generator/stages/s11_feature_store.py +348 -0
- customer_retention/generators/orchestration/__init__.py +23 -0
- customer_retention/generators/orchestration/code_generator.py +196 -0
- customer_retention/generators/orchestration/context.py +147 -0
- customer_retention/generators/orchestration/data_materializer.py +188 -0
- customer_retention/generators/orchestration/databricks_exporter.py +411 -0
- customer_retention/generators/orchestration/doc_generator.py +311 -0
- customer_retention/generators/pipeline_generator/__init__.py +26 -0
- customer_retention/generators/pipeline_generator/findings_parser.py +727 -0
- customer_retention/generators/pipeline_generator/generator.py +142 -0
- customer_retention/generators/pipeline_generator/models.py +166 -0
- customer_retention/generators/pipeline_generator/renderer.py +2125 -0
- customer_retention/generators/spec_generator/__init__.py +37 -0
- customer_retention/generators/spec_generator/databricks_generator.py +433 -0
- customer_retention/generators/spec_generator/generic_generator.py +373 -0
- customer_retention/generators/spec_generator/mlflow_pipeline_generator.py +685 -0
- customer_retention/generators/spec_generator/pipeline_spec.py +298 -0
- customer_retention/integrations/__init__.py +0 -0
- customer_retention/integrations/adapters/__init__.py +13 -0
- customer_retention/integrations/adapters/base.py +10 -0
- customer_retention/integrations/adapters/factory.py +25 -0
- customer_retention/integrations/adapters/feature_store/__init__.py +6 -0
- customer_retention/integrations/adapters/feature_store/base.py +57 -0
- customer_retention/integrations/adapters/feature_store/databricks.py +94 -0
- customer_retention/integrations/adapters/feature_store/feast_adapter.py +97 -0
- customer_retention/integrations/adapters/feature_store/local.py +75 -0
- customer_retention/integrations/adapters/mlflow/__init__.py +6 -0
- customer_retention/integrations/adapters/mlflow/base.py +32 -0
- customer_retention/integrations/adapters/mlflow/databricks.py +54 -0
- customer_retention/integrations/adapters/mlflow/experiment_tracker.py +161 -0
- customer_retention/integrations/adapters/mlflow/local.py +50 -0
- customer_retention/integrations/adapters/storage/__init__.py +5 -0
- customer_retention/integrations/adapters/storage/base.py +33 -0
- customer_retention/integrations/adapters/storage/databricks.py +76 -0
- customer_retention/integrations/adapters/storage/local.py +59 -0
- customer_retention/integrations/feature_store/__init__.py +47 -0
- customer_retention/integrations/feature_store/definitions.py +215 -0
- customer_retention/integrations/feature_store/manager.py +744 -0
- customer_retention/integrations/feature_store/registry.py +412 -0
- customer_retention/integrations/iteration/__init__.py +28 -0
- customer_retention/integrations/iteration/context.py +212 -0
- customer_retention/integrations/iteration/feedback_collector.py +184 -0
- customer_retention/integrations/iteration/orchestrator.py +168 -0
- customer_retention/integrations/iteration/recommendation_tracker.py +341 -0
- customer_retention/integrations/iteration/signals.py +212 -0
- customer_retention/integrations/llm_context/__init__.py +4 -0
- customer_retention/integrations/llm_context/context_builder.py +201 -0
- customer_retention/integrations/llm_context/prompts.py +100 -0
- customer_retention/integrations/streaming/__init__.py +103 -0
- customer_retention/integrations/streaming/batch_integration.py +149 -0
- customer_retention/integrations/streaming/early_warning_model.py +227 -0
- customer_retention/integrations/streaming/event_schema.py +214 -0
- customer_retention/integrations/streaming/online_store_writer.py +249 -0
- customer_retention/integrations/streaming/realtime_scorer.py +261 -0
- customer_retention/integrations/streaming/trigger_engine.py +293 -0
- customer_retention/integrations/streaming/window_aggregator.py +393 -0
- customer_retention/stages/__init__.py +0 -0
- customer_retention/stages/cleaning/__init__.py +9 -0
- customer_retention/stages/cleaning/base.py +28 -0
- customer_retention/stages/cleaning/missing_handler.py +160 -0
- customer_retention/stages/cleaning/outlier_handler.py +204 -0
- customer_retention/stages/deployment/__init__.py +28 -0
- customer_retention/stages/deployment/batch_scorer.py +106 -0
- customer_retention/stages/deployment/champion_challenger.py +299 -0
- customer_retention/stages/deployment/model_registry.py +182 -0
- customer_retention/stages/deployment/retraining_trigger.py +245 -0
- customer_retention/stages/features/__init__.py +73 -0
- customer_retention/stages/features/behavioral_features.py +266 -0
- customer_retention/stages/features/customer_segmentation.py +505 -0
- customer_retention/stages/features/feature_definitions.py +265 -0
- customer_retention/stages/features/feature_engineer.py +551 -0
- customer_retention/stages/features/feature_manifest.py +340 -0
- customer_retention/stages/features/feature_selector.py +239 -0
- customer_retention/stages/features/interaction_features.py +160 -0
- customer_retention/stages/features/temporal_features.py +243 -0
- customer_retention/stages/ingestion/__init__.py +9 -0
- customer_retention/stages/ingestion/load_result.py +32 -0
- customer_retention/stages/ingestion/loaders.py +195 -0
- customer_retention/stages/ingestion/source_registry.py +130 -0
- customer_retention/stages/modeling/__init__.py +31 -0
- customer_retention/stages/modeling/baseline_trainer.py +139 -0
- customer_retention/stages/modeling/cross_validator.py +125 -0
- customer_retention/stages/modeling/data_splitter.py +205 -0
- customer_retention/stages/modeling/feature_scaler.py +99 -0
- customer_retention/stages/modeling/hyperparameter_tuner.py +107 -0
- customer_retention/stages/modeling/imbalance_handler.py +282 -0
- customer_retention/stages/modeling/mlflow_logger.py +95 -0
- customer_retention/stages/modeling/model_comparator.py +149 -0
- customer_retention/stages/modeling/model_evaluator.py +138 -0
- customer_retention/stages/modeling/threshold_optimizer.py +131 -0
- customer_retention/stages/monitoring/__init__.py +37 -0
- customer_retention/stages/monitoring/alert_manager.py +328 -0
- customer_retention/stages/monitoring/drift_detector.py +201 -0
- customer_retention/stages/monitoring/performance_monitor.py +242 -0
- customer_retention/stages/preprocessing/__init__.py +5 -0
- customer_retention/stages/preprocessing/transformer_manager.py +284 -0
- customer_retention/stages/profiling/__init__.py +256 -0
- customer_retention/stages/profiling/categorical_distribution.py +269 -0
- customer_retention/stages/profiling/categorical_target_analyzer.py +274 -0
- customer_retention/stages/profiling/column_profiler.py +527 -0
- customer_retention/stages/profiling/distribution_analysis.py +483 -0
- customer_retention/stages/profiling/drift_detector.py +310 -0
- customer_retention/stages/profiling/feature_capacity.py +507 -0
- customer_retention/stages/profiling/pattern_analysis_config.py +513 -0
- customer_retention/stages/profiling/profile_result.py +212 -0
- customer_retention/stages/profiling/quality_checks.py +1632 -0
- customer_retention/stages/profiling/relationship_detector.py +256 -0
- customer_retention/stages/profiling/relationship_recommender.py +454 -0
- customer_retention/stages/profiling/report_generator.py +520 -0
- customer_retention/stages/profiling/scd_analyzer.py +151 -0
- customer_retention/stages/profiling/segment_analyzer.py +632 -0
- customer_retention/stages/profiling/segment_aware_outlier.py +265 -0
- customer_retention/stages/profiling/target_level_analyzer.py +217 -0
- customer_retention/stages/profiling/temporal_analyzer.py +388 -0
- customer_retention/stages/profiling/temporal_coverage.py +488 -0
- customer_retention/stages/profiling/temporal_feature_analyzer.py +692 -0
- customer_retention/stages/profiling/temporal_feature_engineer.py +703 -0
- customer_retention/stages/profiling/temporal_pattern_analyzer.py +636 -0
- customer_retention/stages/profiling/temporal_quality_checks.py +278 -0
- customer_retention/stages/profiling/temporal_target_analyzer.py +241 -0
- customer_retention/stages/profiling/text_embedder.py +87 -0
- customer_retention/stages/profiling/text_processor.py +115 -0
- customer_retention/stages/profiling/text_reducer.py +60 -0
- customer_retention/stages/profiling/time_series_profiler.py +303 -0
- customer_retention/stages/profiling/time_window_aggregator.py +376 -0
- customer_retention/stages/profiling/type_detector.py +382 -0
- customer_retention/stages/profiling/window_recommendation.py +288 -0
- customer_retention/stages/temporal/__init__.py +166 -0
- customer_retention/stages/temporal/access_guard.py +180 -0
- customer_retention/stages/temporal/cutoff_analyzer.py +235 -0
- customer_retention/stages/temporal/data_preparer.py +178 -0
- customer_retention/stages/temporal/point_in_time_join.py +134 -0
- customer_retention/stages/temporal/point_in_time_registry.py +148 -0
- customer_retention/stages/temporal/scenario_detector.py +163 -0
- customer_retention/stages/temporal/snapshot_manager.py +259 -0
- customer_retention/stages/temporal/synthetic_coordinator.py +66 -0
- customer_retention/stages/temporal/timestamp_discovery.py +531 -0
- customer_retention/stages/temporal/timestamp_manager.py +255 -0
- customer_retention/stages/transformation/__init__.py +13 -0
- customer_retention/stages/transformation/binary_handler.py +85 -0
- customer_retention/stages/transformation/categorical_encoder.py +245 -0
- customer_retention/stages/transformation/datetime_transformer.py +97 -0
- customer_retention/stages/transformation/numeric_transformer.py +181 -0
- customer_retention/stages/transformation/pipeline.py +257 -0
- customer_retention/stages/validation/__init__.py +60 -0
- customer_retention/stages/validation/adversarial_scoring_validator.py +205 -0
- customer_retention/stages/validation/business_sense_gate.py +173 -0
- customer_retention/stages/validation/data_quality_gate.py +235 -0
- customer_retention/stages/validation/data_validators.py +511 -0
- customer_retention/stages/validation/feature_quality_gate.py +183 -0
- customer_retention/stages/validation/gates.py +117 -0
- customer_retention/stages/validation/leakage_gate.py +352 -0
- customer_retention/stages/validation/model_validity_gate.py +213 -0
- customer_retention/stages/validation/pipeline_validation_runner.py +264 -0
- customer_retention/stages/validation/quality_scorer.py +544 -0
- customer_retention/stages/validation/rule_generator.py +57 -0
- customer_retention/stages/validation/scoring_pipeline_validator.py +446 -0
- customer_retention/stages/validation/timeseries_detector.py +769 -0
- customer_retention/transforms/__init__.py +47 -0
- customer_retention/transforms/artifact_store.py +50 -0
- customer_retention/transforms/executor.py +157 -0
- customer_retention/transforms/fitted.py +92 -0
- customer_retention/transforms/ops.py +148 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
from dataclasses import dataclass, field
|
|
2
|
+
from datetime import datetime
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import Any, Dict, List, Optional, Tuple
|
|
5
|
+
|
|
6
|
+
import yaml
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@dataclass
|
|
10
|
+
class ModelFeedback:
|
|
11
|
+
iteration_id: str
|
|
12
|
+
model_type: str
|
|
13
|
+
metrics: Dict[str, float]
|
|
14
|
+
feature_importances: Dict[str, float]
|
|
15
|
+
confusion_matrix: Optional[List[List[int]]] = None
|
|
16
|
+
error_analysis: Optional[Dict[str, Any]] = None
|
|
17
|
+
collected_at: datetime = field(default_factory=datetime.now)
|
|
18
|
+
|
|
19
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
20
|
+
return {
|
|
21
|
+
"iteration_id": self.iteration_id,
|
|
22
|
+
"model_type": self.model_type,
|
|
23
|
+
"metrics": self.metrics,
|
|
24
|
+
"feature_importances": self.feature_importances,
|
|
25
|
+
"confusion_matrix": self.confusion_matrix,
|
|
26
|
+
"error_analysis": self.error_analysis,
|
|
27
|
+
"collected_at": self.collected_at.isoformat()
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
@classmethod
|
|
31
|
+
def from_dict(cls, data: Dict[str, Any]) -> "ModelFeedback":
|
|
32
|
+
return cls(
|
|
33
|
+
iteration_id=data["iteration_id"],
|
|
34
|
+
model_type=data["model_type"],
|
|
35
|
+
metrics=data["metrics"],
|
|
36
|
+
feature_importances=data["feature_importances"],
|
|
37
|
+
confusion_matrix=data.get("confusion_matrix"),
|
|
38
|
+
error_analysis=data.get("error_analysis"),
|
|
39
|
+
collected_at=datetime.fromisoformat(data["collected_at"]) if "collected_at" in data else datetime.now()
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@dataclass
|
|
44
|
+
class FeatureInsight:
|
|
45
|
+
feature_name: str
|
|
46
|
+
importance_rank: int
|
|
47
|
+
importance_score: float
|
|
48
|
+
recommendation_to_drop: bool
|
|
49
|
+
recommendation_to_engineer: Optional[str] = None
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class ModelFeedbackCollector:
|
|
53
|
+
def __init__(self, drop_threshold: float = 0.01):
|
|
54
|
+
self.drop_threshold = drop_threshold
|
|
55
|
+
|
|
56
|
+
def create_from_sklearn(self, model, iteration_id: str,
|
|
57
|
+
feature_names: List[str],
|
|
58
|
+
metrics: Dict[str, float]) -> ModelFeedback:
|
|
59
|
+
model_type = type(model).__name__
|
|
60
|
+
|
|
61
|
+
feature_importances = {}
|
|
62
|
+
if hasattr(model, 'feature_importances_'):
|
|
63
|
+
for name, imp in zip(feature_names, model.feature_importances_):
|
|
64
|
+
feature_importances[name] = float(imp)
|
|
65
|
+
elif hasattr(model, 'coef_'):
|
|
66
|
+
import numpy as np
|
|
67
|
+
coefs = np.abs(model.coef_).flatten()
|
|
68
|
+
for name, coef in zip(feature_names, coefs):
|
|
69
|
+
feature_importances[name] = float(coef)
|
|
70
|
+
|
|
71
|
+
return ModelFeedback(
|
|
72
|
+
iteration_id=iteration_id,
|
|
73
|
+
model_type=model_type,
|
|
74
|
+
metrics=metrics,
|
|
75
|
+
feature_importances=feature_importances
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
def analyze_feature_importance(self, feedback: ModelFeedback) -> List[FeatureInsight]:
|
|
79
|
+
sorted_features = sorted(
|
|
80
|
+
feedback.feature_importances.items(),
|
|
81
|
+
key=lambda x: x[1],
|
|
82
|
+
reverse=True
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
insights = []
|
|
86
|
+
for rank, (feature, score) in enumerate(sorted_features, 1):
|
|
87
|
+
should_drop = score < self.drop_threshold
|
|
88
|
+
recommendation = None
|
|
89
|
+
if should_drop:
|
|
90
|
+
recommendation = f"Consider removing {feature} (importance: {score:.4f})"
|
|
91
|
+
|
|
92
|
+
insights.append(FeatureInsight(
|
|
93
|
+
feature_name=feature,
|
|
94
|
+
importance_rank=rank,
|
|
95
|
+
importance_score=score,
|
|
96
|
+
recommendation_to_drop=should_drop,
|
|
97
|
+
recommendation_to_engineer=recommendation
|
|
98
|
+
))
|
|
99
|
+
|
|
100
|
+
return insights
|
|
101
|
+
|
|
102
|
+
def suggest_next_actions(self, feedback: ModelFeedback) -> List[str]:
|
|
103
|
+
actions = []
|
|
104
|
+
|
|
105
|
+
roc_auc = feedback.metrics.get("roc_auc", 0)
|
|
106
|
+
pr_auc = feedback.metrics.get("pr_auc", 0)
|
|
107
|
+
|
|
108
|
+
if roc_auc >= 0.90 or pr_auc >= 0.85:
|
|
109
|
+
actions.append(f"Model performance is excellent (ROC-AUC: {roc_auc:.2f}). Consider deploying.")
|
|
110
|
+
elif roc_auc >= 0.80:
|
|
111
|
+
actions.append(f"Model performance is good (ROC-AUC: {roc_auc:.2f}). Consider feature engineering for improvement.")
|
|
112
|
+
else:
|
|
113
|
+
actions.append(f"Model performance needs improvement (ROC-AUC: {roc_auc:.2f}). Review feature engineering and data quality.")
|
|
114
|
+
|
|
115
|
+
low_importance = [
|
|
116
|
+
name for name, score in feedback.feature_importances.items()
|
|
117
|
+
if score < self.drop_threshold
|
|
118
|
+
]
|
|
119
|
+
if low_importance:
|
|
120
|
+
actions.append(f"Consider dropping {len(low_importance)} low-importance features: {', '.join(low_importance[:5])}")
|
|
121
|
+
|
|
122
|
+
if len(feedback.feature_importances) < 5:
|
|
123
|
+
actions.append("Feature set is small. Consider engineering additional features.")
|
|
124
|
+
|
|
125
|
+
top_features = self.get_top_features(feedback, n=3)
|
|
126
|
+
if top_features:
|
|
127
|
+
top_names = [f[0] for f in top_features]
|
|
128
|
+
actions.append(f"Top performing features: {', '.join(top_names)}. Consider creating derived features from these.")
|
|
129
|
+
|
|
130
|
+
return actions
|
|
131
|
+
|
|
132
|
+
def compare_feedback(self, previous: ModelFeedback,
|
|
133
|
+
current: ModelFeedback) -> Dict[str, Any]:
|
|
134
|
+
metric_improvements = {}
|
|
135
|
+
for metric in current.metrics:
|
|
136
|
+
if metric in previous.metrics:
|
|
137
|
+
metric_improvements[metric] = current.metrics[metric] - previous.metrics[metric]
|
|
138
|
+
|
|
139
|
+
avg_improvement = sum(metric_improvements.values()) / len(metric_improvements) if metric_improvements else 0
|
|
140
|
+
|
|
141
|
+
if avg_improvement > 0.02:
|
|
142
|
+
trend = "improved"
|
|
143
|
+
elif avg_improvement < -0.02:
|
|
144
|
+
trend = "degraded"
|
|
145
|
+
else:
|
|
146
|
+
trend = "stable"
|
|
147
|
+
|
|
148
|
+
feature_changes = {}
|
|
149
|
+
for feature, score in current.feature_importances.items():
|
|
150
|
+
if feature in previous.feature_importances:
|
|
151
|
+
feature_changes[feature] = score - previous.feature_importances[feature]
|
|
152
|
+
|
|
153
|
+
return {
|
|
154
|
+
"metric_improvements": metric_improvements,
|
|
155
|
+
"overall_trend": trend,
|
|
156
|
+
"feature_importance_changes": feature_changes,
|
|
157
|
+
"previous_iteration": previous.iteration_id,
|
|
158
|
+
"current_iteration": current.iteration_id
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
def get_top_features(self, feedback: ModelFeedback,
|
|
162
|
+
n: int = 5) -> List[Tuple[str, float]]:
|
|
163
|
+
sorted_features = sorted(
|
|
164
|
+
feedback.feature_importances.items(),
|
|
165
|
+
key=lambda x: x[1],
|
|
166
|
+
reverse=True
|
|
167
|
+
)
|
|
168
|
+
return sorted_features[:n]
|
|
169
|
+
|
|
170
|
+
def get_low_importance_features(self, feedback: ModelFeedback) -> List[str]:
|
|
171
|
+
return [
|
|
172
|
+
name for name, score in feedback.feature_importances.items()
|
|
173
|
+
if score < self.drop_threshold
|
|
174
|
+
]
|
|
175
|
+
|
|
176
|
+
def save_feedback(self, feedback: ModelFeedback, path: str) -> None:
|
|
177
|
+
Path(path).parent.mkdir(parents=True, exist_ok=True)
|
|
178
|
+
with open(path, "w") as f:
|
|
179
|
+
yaml.dump(feedback.to_dict(), f, default_flow_style=False, sort_keys=False)
|
|
180
|
+
|
|
181
|
+
def load_feedback(self, path: str) -> ModelFeedback:
|
|
182
|
+
with open(path, "r") as f:
|
|
183
|
+
data = yaml.safe_load(f)
|
|
184
|
+
return ModelFeedback.from_dict(data)
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
from typing import Any, Dict, List, Optional, Tuple
|
|
3
|
+
|
|
4
|
+
from .context import IterationContext, IterationContextManager, IterationStatus, IterationTrigger
|
|
5
|
+
from .feedback_collector import ModelFeedback, ModelFeedbackCollector
|
|
6
|
+
from .recommendation_tracker import RecommendationTracker, TrackedRecommendation
|
|
7
|
+
from .signals import SignalAggregator
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class IterationOrchestrator:
|
|
11
|
+
def __init__(self, findings_dir: str,
|
|
12
|
+
signal_aggregator: Optional[SignalAggregator] = None):
|
|
13
|
+
self.findings_dir = Path(findings_dir)
|
|
14
|
+
self.iterations_dir = self.findings_dir / "iterations"
|
|
15
|
+
self.recommendations_dir = self.findings_dir / "recommendations"
|
|
16
|
+
self.feedback_dir = self.findings_dir / "feedback"
|
|
17
|
+
|
|
18
|
+
self.iterations_dir.mkdir(parents=True, exist_ok=True)
|
|
19
|
+
self.recommendations_dir.mkdir(parents=True, exist_ok=True)
|
|
20
|
+
self.feedback_dir.mkdir(parents=True, exist_ok=True)
|
|
21
|
+
|
|
22
|
+
self._context_manager = IterationContextManager(str(self.iterations_dir))
|
|
23
|
+
self._recommendation_tracker = RecommendationTracker(
|
|
24
|
+
str(self.recommendations_dir / "tracked.yaml")
|
|
25
|
+
)
|
|
26
|
+
self._feedback_collector = ModelFeedbackCollector()
|
|
27
|
+
self._signal_aggregator = signal_aggregator or SignalAggregator()
|
|
28
|
+
|
|
29
|
+
self._current_context: Optional[IterationContext] = None
|
|
30
|
+
self._latest_feedback: Optional[ModelFeedback] = None
|
|
31
|
+
|
|
32
|
+
def start_new_iteration(self, trigger: IterationTrigger) -> IterationContext:
|
|
33
|
+
ctx = IterationContext.create_new(str(self.findings_dir), trigger)
|
|
34
|
+
self._current_context = ctx
|
|
35
|
+
self._save_current_context()
|
|
36
|
+
self._context_manager.set_current(ctx.iteration_id)
|
|
37
|
+
return ctx
|
|
38
|
+
|
|
39
|
+
def start_child_iteration(self, trigger: IterationTrigger) -> IterationContext:
|
|
40
|
+
if self._current_context is None:
|
|
41
|
+
return self.start_new_iteration(trigger)
|
|
42
|
+
|
|
43
|
+
child = self._current_context.create_child(trigger)
|
|
44
|
+
self._current_context = child
|
|
45
|
+
self._save_current_context()
|
|
46
|
+
self._context_manager.set_current(child.iteration_id)
|
|
47
|
+
return child
|
|
48
|
+
|
|
49
|
+
def get_current_iteration(self) -> Optional[IterationContext]:
|
|
50
|
+
if self._current_context is not None:
|
|
51
|
+
return self._current_context
|
|
52
|
+
return self._context_manager.get_current()
|
|
53
|
+
|
|
54
|
+
def update_status(self, status: IterationStatus) -> None:
|
|
55
|
+
if self._current_context:
|
|
56
|
+
self._current_context.update_status(status)
|
|
57
|
+
self._save_current_context()
|
|
58
|
+
|
|
59
|
+
def record_model_metrics(self, metrics: Dict[str, float],
|
|
60
|
+
artifact_path: Optional[str] = None) -> None:
|
|
61
|
+
if self._current_context:
|
|
62
|
+
self._current_context.set_model_metrics(metrics, artifact_path)
|
|
63
|
+
self._save_current_context()
|
|
64
|
+
|
|
65
|
+
def get_recommendation_tracker(self) -> RecommendationTracker:
|
|
66
|
+
return self._recommendation_tracker
|
|
67
|
+
|
|
68
|
+
def track_recommendation(self, recommendation: TrackedRecommendation) -> None:
|
|
69
|
+
self._recommendation_tracker.add(recommendation)
|
|
70
|
+
|
|
71
|
+
def apply_recommendation(self, recommendation_id: str) -> None:
|
|
72
|
+
if self._current_context:
|
|
73
|
+
self._recommendation_tracker.mark_applied(
|
|
74
|
+
recommendation_id, self._current_context.iteration_id
|
|
75
|
+
)
|
|
76
|
+
self._current_context.add_applied_recommendation(recommendation_id)
|
|
77
|
+
self._save_current_context()
|
|
78
|
+
|
|
79
|
+
def skip_recommendation(self, recommendation_id: str, reason: str) -> None:
|
|
80
|
+
self._recommendation_tracker.mark_skipped(recommendation_id, reason)
|
|
81
|
+
if self._current_context:
|
|
82
|
+
self._current_context.add_skipped_recommendation(recommendation_id)
|
|
83
|
+
self._save_current_context()
|
|
84
|
+
|
|
85
|
+
def collect_feedback(self, feedback: ModelFeedback) -> None:
|
|
86
|
+
self._latest_feedback = feedback
|
|
87
|
+
feedback_path = self.feedback_dir / f"feedback_{feedback.iteration_id}.yaml"
|
|
88
|
+
self._feedback_collector.save_feedback(feedback, str(feedback_path))
|
|
89
|
+
|
|
90
|
+
def get_latest_feedback(self) -> Optional[ModelFeedback]:
|
|
91
|
+
return self._latest_feedback
|
|
92
|
+
|
|
93
|
+
def check_for_iteration_triggers(self) -> Tuple[bool, Optional[IterationTrigger]]:
|
|
94
|
+
return self._signal_aggregator.should_trigger_iteration()
|
|
95
|
+
|
|
96
|
+
def trigger_manual_iteration(self, reason: str) -> None:
|
|
97
|
+
self._signal_aggregator.add_manual_signal(reason, {})
|
|
98
|
+
|
|
99
|
+
def prepare_iteration_from_feedback(self, feedback: ModelFeedback,
|
|
100
|
+
trigger: IterationTrigger) -> IterationContext:
|
|
101
|
+
new_ctx = self.start_child_iteration(trigger)
|
|
102
|
+
|
|
103
|
+
insights = self._feedback_collector.analyze_feature_importance(feedback)
|
|
104
|
+
[i.feature_name for i in insights if i.recommendation_to_drop]
|
|
105
|
+
|
|
106
|
+
return new_ctx
|
|
107
|
+
|
|
108
|
+
def get_refined_recommendations(self, findings, feedback: ModelFeedback) -> Dict[str, Any]:
|
|
109
|
+
insights = self._feedback_collector.analyze_feature_importance(feedback)
|
|
110
|
+
|
|
111
|
+
features_to_drop = [i.feature_name for i in insights if i.recommendation_to_drop]
|
|
112
|
+
top_features = self._feedback_collector.get_top_features(feedback, n=5)
|
|
113
|
+
|
|
114
|
+
refined = {
|
|
115
|
+
"features_to_drop": features_to_drop,
|
|
116
|
+
"top_features": [f[0] for f in top_features],
|
|
117
|
+
"feature_insights": [
|
|
118
|
+
{
|
|
119
|
+
"name": i.feature_name,
|
|
120
|
+
"rank": i.importance_rank,
|
|
121
|
+
"score": i.importance_score,
|
|
122
|
+
"drop": i.recommendation_to_drop
|
|
123
|
+
}
|
|
124
|
+
for i in insights
|
|
125
|
+
],
|
|
126
|
+
"next_actions": self._feedback_collector.suggest_next_actions(feedback)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return refined
|
|
130
|
+
|
|
131
|
+
def get_iteration_history(self) -> List[IterationContext]:
|
|
132
|
+
if self._current_context:
|
|
133
|
+
return self._context_manager.get_iteration_history(
|
|
134
|
+
self._current_context.iteration_id
|
|
135
|
+
)
|
|
136
|
+
return self._context_manager.list_iterations()
|
|
137
|
+
|
|
138
|
+
def compare_iterations(self, iteration_id_1: str,
|
|
139
|
+
iteration_id_2: str) -> Dict[str, Any]:
|
|
140
|
+
ctx1 = self._context_manager.get_by_id(iteration_id_1)
|
|
141
|
+
ctx2 = self._context_manager.get_by_id(iteration_id_2)
|
|
142
|
+
|
|
143
|
+
if ctx1 is None or ctx2 is None:
|
|
144
|
+
return {"error": "Iteration not found"}
|
|
145
|
+
|
|
146
|
+
return ctx2.compare(ctx1)
|
|
147
|
+
|
|
148
|
+
def save_state(self) -> None:
|
|
149
|
+
if self._current_context:
|
|
150
|
+
self._save_current_context()
|
|
151
|
+
self._recommendation_tracker.save()
|
|
152
|
+
|
|
153
|
+
def load_state(self) -> None:
|
|
154
|
+
self._recommendation_tracker.load()
|
|
155
|
+
|
|
156
|
+
current = self._context_manager.get_current()
|
|
157
|
+
if current:
|
|
158
|
+
self._current_context = current
|
|
159
|
+
|
|
160
|
+
feedback_files = sorted(self.feedback_dir.glob("feedback_*.yaml"))
|
|
161
|
+
if feedback_files:
|
|
162
|
+
self._latest_feedback = self._feedback_collector.load_feedback(
|
|
163
|
+
str(feedback_files[-1])
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
def _save_current_context(self) -> None:
|
|
167
|
+
if self._current_context:
|
|
168
|
+
self._context_manager.save_iteration(self._current_context)
|