o
    ÊxijÊu  ã                   @  sv   d dl mZ d dlZd dlZd dlZd dlmZ d dlmZm	Z	 d dl
Zd dlmZ d dlmZmZ G dd„ dƒZdS )	é    )ÚannotationsN)ÚPath)ÚOptionalÚSequence)Ú+AdaptiveRLFeatureSubspaceBoostingClassifier)Ú)ClassAwareHeterogeneousStackingClassifierÚbuild_standard_base_estimatorsc                   @  sZ  e Zd ZdZ											
												
			
															 		!	"		#			$	d…d†dTdU„ZdVdW„ ZdXdY„ ZedZd[„ ƒZed\d]„ ƒZ	ed^d_„ ƒZ
e		d‡d`da„ƒZ	bdˆdcdd„Zdedf„ Zdgdh„ Zdidj„ Z		d‡dkdl„Zdmdn„ Zdodp„ Zd‰dqdr„Zdsdt„ Zdudv„ Zdwdx„ Zdydz„ Zd{d|„ Zd}d~„ Zdd€„ Zd�d‚„ Zedƒd„„ ƒZdS )ŠÚBelowOneRLGradeRegressora�  Two-stage low-grade classifier + regular-grade regressor.

    Stage 1
    -------
    Predict whether the grade is below ``boundary`` using a heterogeneous,
    class-aware stacking classifier composed of:

      * ARL-FSGB
      * full-feature RL gradient boosting classifier
      * balanced Random Forest
      * balanced Extra Trees
      * Histogram Gradient Boosting
      * LightGBM when installed

    Stage 2
    -------
    For samples predicted as regular class, predict the grade using the
    supplied RL regressor.

    For samples predicted as the low class, the returned grade is controlled by
    ``low_grade_output``:

      * "classifier_probability":
          boundary * (1 - P(low)); produces a continuous value in [0,boundary]
      * "zero":
          always predicts 0 for low-class samples
      * "training_mean":
          predicts the mean low-grade target observed during training
      * numeric value:
          predicts that fixed value, clipped to [0,boundary]

    Important
    ---------
    X_val/y_val must be taken from the training fold. Do not use the test fold
    for adaptive feature selection, stacking, or threshold optimization.
    ç      ð?Nç      @Úclassifier_probabilityé   ©ç      Ð?gš™™™™™Ù?gš™™™™™á?ç333333ë?é   çš™™™™™É?ç
×£p=
ï?çffffffÖ?é   é   çÍÌÌÌÌÌì?é   ç      à?Tç…ëQ¸…ï?ç333333Ó?ç333333Ã?çš™™™™™©?éd   é
   ç�íµ ÷Æ°>é@   Úsqrt_balancedr   é   ç-Cëâ6?çš™™™™™¹?é   é   ©r   r   r
   ç      ø?g       @ç      @é*   ÚboundaryÚfloatÚlow_grade_thresholdúOptional[float]Úlow_class_weightÚclassifier_feature_namesúOptional[Sequence[str]]Úclassifier_n_roundsÚintÚ classifier_correlation_thresholdÚ'classifier_maximum_features_per_clusterÚclassifier_exploration_rateÚclassifier_exploration_decayÚ classifier_feature_learning_rateÚclassifier_max_selected_modelsÚclassifier_entropy_binsÚ#classifier_maximum_missing_fractionÚ classifier_minimum_unique_valuesÚstacking_blender_fractionÚstacking_include_lightgbmÚboolÚ'stacking_include_hist_gradient_boostingÚ#stacking_include_full_rl_classifierÚ stacking_minimum_majority_recallÚ stacking_minimum_minority_recallÚ"stacking_reward_minority_f1_weightÚ&stacking_reward_minority_recall_weightÚ&stacking_reward_majority_recall_weightÚ(stacking_reward_balanced_accuracy_weightÚclassifier_learning_rateÚclassifier_n_estimatorsÚclassifier_min_samplesÚclassifier_max_depthÚclassifier_min_gainÚclassifier_max_thresholdsÚ#classifier_minority_weight_fractionÚclassifier_prior_strengthÚ classifier_early_stopping_roundsÚclassifier_minimum_improvementÚrl_learning_rateÚrl_n_estimatorsÚrl_min_samplesÚrl_max_depthÚregression_lower_boundÚregression_upper_boundÚrandom_stateÚverbosec1           1      C  s
  t |ƒ| _|d u rd nt |ƒ| _t |ƒ| _|| _|d u rd nt|ƒ| _t|ƒ| _t	|ƒ| _
t |ƒ| _t|	ƒ| _t |
ƒ| _t |ƒ| _t |ƒ| _t|ƒ| _t|ƒ| _t |ƒ| _t|ƒ| _t |ƒ| _t|ƒ| _t|ƒ| _t|ƒ| _t |ƒ| _t |ƒ| _t |ƒ| _t |ƒ| _t |ƒ| _t |ƒ| _t |ƒ| _t|ƒ| _ t|ƒ| _!t|ƒ| _"t |ƒ| _#t| ƒ| _$|!| _%t |"ƒ| _&t |#ƒ| _'t|$ƒ| _(t |%ƒ| _)t |&ƒ| _*t|'ƒ| _+t|(ƒ| _,t|)ƒ| _-t|*ƒ| _.|+d u rá| jnt |+ƒ| _/t |,ƒ| _0|-| _1|.| _2t|/ƒ| _3t|0ƒ| _4|  5¡  |  6¡  d S ©N)7r-   r,   r.   r0   Úlow_grade_outputÚlistr1   r4   r3   ÚtupleÚclassifier_subspace_sizesr5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r@   r?   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   Úclassifier_class_weightrO   rP   rQ   rR   rS   rT   rU   rV   Ú
rl_actionsrW   rX   Úrl_classifier_classÚrl_regressor_classrY   rZ   Ú_validate_parametersÚ_reset_fitted_state)1Úselfr,   r.   r0   r\   r1   r3   r_   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   r`   rO   rP   rQ   rR   rS   rT   rU   rV   ra   rW   rX   rb   rc   rY   rZ   © rg   úM/root/disk_3tb/earlywarning/30072026/below_one_rl_grade_regressor_stacking.pyÚ__init__:   sâ   
Kÿý
ÿý
ÿÿÿÿÿÿÿ
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿ


ÿÿÿÿÿ




ÿýÿ

z!BelowOneRLGradeRegressor.__init__c                 C  sz   | j dk r	tdƒ‚| j| jk rtdƒ‚| j| j | j | j }t 	|d¡s)tdƒ‚| j
d u r2tdƒ‚| jd u r;tdƒ‚d S )Nr   zboundary must be non-negative.z9regression_upper_bound must be >= regression_lower_bound.r
   z/The four stacking reward weights must sum to 1.zBPass rl_classifier_class=RL_GradientBoostingClassifierfromScratch.zaPass rl_regressor_class=RL_GradientBoostingRegressorScratch (or your current RL regressor class).)r,   Ú
ValueErrorrX   rW   rE   rF   rG   rH   ÚnpÚiscloserb   rc   )rf   Ú
reward_sumrg   rg   rh   rd     s:   
ÿÿÿþýÿÿ
ÿ
ÿÿz-BelowOneRLGradeRegressor._validate_parametersc                 C  s4   d | _ d | _d | _d| _d| _d| _d| _d| _d S )Nç        r   r   F)Úlow_grade_classifierÚregular_grade_regressorÚn_features_in_Úlow_grade_training_mean_Úclassification_threshold_Ún_low_train_Ún_regular_train_Ú
is_fitted_©rf   rg   rg   rh   re   +  s   
z,BelowOneRLGradeRegressor._reset_fitted_statec                 C  sP   t | dƒr	|  ¡ } tj| tjd�} | jdkrtd| j› d�ƒ‚tj| dddd�S )NÚto_numpy©Údtyper   z"X must be 2-dimensional; received Ú.rn   )ÚnanÚposinfÚneginf)	Úhasattrrx   rk   ÚasarrayÚfloat64Úndimrj   ÚshapeÚ
nan_to_num)ÚXrg   rg   rh   Ú
_prepare_X8  s   

ÿüz#BelowOneRLGradeRegressor._prepare_Xc                 C  sT   t | dƒr	|  ¡ } tj| tjd� d¡} | jdkrtdƒ‚t | ¡ 	¡ s(tdƒ‚| S )Nrx   ry   éÿÿÿÿr   zy cannot be empty.z"y contains NaN or infinite values.)
r   rx   rk   r€   r�   ÚreshapeÚsizerj   ÚisfiniteÚall)Úyrg   rg   rh   Ú
_prepare_yK  s   

z#BelowOneRLGradeRegressor._prepare_yc                   s6   t  | j¡}|j‰ ‡ fdd„| ¡ D ƒ}| di |¤ŽS )zEConstruct a custom estimator without assuming every parameter exists.c                   s   i | ]\}}|ˆ v r||“qS rg   rg   )Ú.0ÚkeyÚvalue©Ú	supportedrg   rh   Ú
<dictcomp>d  s
    þzPBelowOneRLGradeRegressor._construct_with_supported_arguments.<locals>.<dictcomp>Nrg   )ÚinspectÚ	signatureri   Ú
parametersÚitems)Úestimator_classÚcandidate_argumentsr•   Úfilteredrg   r‘   rh   Ú#_construct_with_supported_argumentsZ  s   
þz<BelowOneRLGradeRegressor._construct_with_supported_argumentsc                 C  s^   t  | j¡}|j}i }|d ur#|d ur#d|v r#d|v r#||d< ||d< | j||fi |¤Ž | S )NÚX_valÚy_val)r”   r•   Úfitr–   )Ú	estimatorr…   rŒ   rœ   r�   r•   r–   Úkeyword_argumentsrg   rg   rh   Ú_fit_with_optional_validationl  s   z6BelowOneRLGradeRegressor._fit_with_optional_validationFc                 C  sš   i d| j “d| j“d| j“d| j“d| j“d| j“d| j“d| j“d	d
“d| j“dd“dd“d| j	“d| j
“d| j“d| j“d|“d|i¥}|  | j|¡S )NÚlearning_rateÚn_estimatorsÚmin_samplesÚ	max_depthÚmin_gainÚmax_thresholdsÚclass_weightÚminority_weight_fractionÚpositive_classé   Úprior_strengthÚdecision_thresholdÚautoÚthreshold_metricÚconstrained_f1Úminimum_majority_recallÚminimum_minority_recallÚearly_stopping_roundsÚminimum_improvementrY   rZ   )rI   rJ   rK   rL   rM   rN   r`   rO   rP   rC   rD   rQ   rR   r›   rb   )rf   ÚseedrZ   r™   rg   rg   rh   Ú_build_rl_classifier…  s\   ÿþýüûúø	öôóñðîëçäá à#þz-BelowOneRLGradeRegressor._build_rl_classifierc                 C  s   | j | jdd�}td6i d|“d| j“dd“d| j“d| j“d	| j“d
d“dd“dd“d| j“dd“d| j“d| j	“dd“d| j
“d| j“d| j“dd“d| j“dd“dd “d!| j“d"| j“d#| j“d$| j“d%| j“d&| j“d'd(“d)d“d*| j“d+d,“d-d.“d/d“d0d1“d2d3“d4| j“d5| j“ŽS )7NF©rµ   rZ   Úbase_estimatorÚfeature_namesrª   r«   Úentropy_binsÚmaximum_missing_fractionÚminimum_unique_valuesÚinitial_information_gain_weightç      è?Úinitial_entropy_weightr   Úinitial_coverage_weightr%   Úcorrelation_thresholdÚcorrelation_sample_sizeià.  Ún_roundsÚsubspace_sizesÚminimum_features_per_subspaceé   Úmaximum_features_per_clusterÚexploration_rateÚexploration_decayÚminimum_exploration_rater   Úfeature_learning_rateÚfeature_probability_floorgñhãˆµøä>Úprobability_temperaturegš™™™™™é?Úreward_minority_f1_weightÚreward_minority_recall_weightÚreward_majority_recall_weightÚreward_balanced_accuracy_weightr±   r²   Úmajority_recall_penaltyr)   Úminority_recall_penaltyÚmax_selected_modelsÚdiversity_strengthr   r­   r®   Úthreshold_minÚthreshold_maxçffffffî?Úthreshold_grid_sizeéµ   rY   rZ   rg   )r¶   rY   r   r1   r;   r<   r=   r5   r3   r_   r6   r7   r8   r9   rE   rF   rG   rH   rC   rD   r:   rZ   )rf   Úbase_classifierrg   rg   rh   Ú_build_arl_fsgb_classifier²  sž   þÿþýûù
öóòñîìêéèæãâá!ß#Ý$Ü'Ù*Ö-Ó0Ð4Ì7É9Ç:Æ=Ã?ÁA¿B¾C½D¼FºG¹z3BelowOneRLGradeRegressor._build_arl_fsgb_classifierc                 C  sÈ   |   ¡ }d }| jr| j| jd dd�}t||| j| jd�}| js&| dd ¡ tdi d|“dd“d| j	“d	| j
“d
| j“d| j“d| j“d| j“d| j“dd“dd“dd“dd“dd“d| j“d| j“ŽS )Nr«   Fr·   )Úarl_fsgb_estimatorÚrl_classifier_estimatorrY   Úinclude_lightgbmÚhist_gradient_boostingÚ
estimatorsrª   Úblender_fractionrÎ   rÏ   rÐ   rÑ   r±   r²   rÒ   r)   rÓ   r¾   rÖ   r   r×   rØ   rÙ   rÚ   rY   rZ   rg   )rÜ   rB   r¶   rY   r   r?   rA   Úpopr   r>   rE   rF   rG   rH   rC   rD   rZ   )rf   Úarl_fsgbÚfull_rl_classifierrá   rg   rg   rh   Ú_build_stacking_classifier  s`   þüÿýú
öòîêæâ!ß"Þ$Ü%Û&Ú(Ø)×z3BelowOneRLGradeRegressor._build_stacking_classifierc                 C  s0   | j | j| j| j| j| j| jdœ}|  | j|¡S )N)r¢   r£   r¤   r¥   ÚactionsrY   rZ   )	rS   rT   rU   rV   ra   rY   rZ   r›   rc   )rf   r™   rg   rg   rh   Ú_build_regular_grade_regressorC  s   øþz7BelowOneRLGradeRegressor._build_regular_grade_regressorc                 C  sü  |d u s|d u rt dƒ‚|  |¡}|  |¡}|  |¡}|  |¡}|jd |jd kr.t dƒ‚|jd |jd kr<t dƒ‚|jd |jd krJt dƒ‚|  ¡  |jd | _| jd uret| jƒ| jkret dƒ‚|| jk  	t
j¡}|| jk  	t
j¡}tt
 |dk¡ƒ| _tt
 |dk¡ƒ| _| jdkr”t dƒ‚| jdkr�t d	ƒ‚tt
 ||dk ¡ƒ| _| jràtd
|j› d|j› �ƒ td|j› d|j› �ƒ td| j› �ƒ td| j› �ƒ td| j| j d›�ƒ |  ¡ | _| jj||||d� t| jjƒ| _| jd urÿ| j| _|dk}|dk}|| }	|| }
|| }|| }| j�r'td|j› d|j› �ƒ |  ¡ | _| j| j|	|
||d� d| _| j�r|tdƒ td| jd›�ƒ td| j› �ƒ td| j› �ƒ td| jd›�ƒ td| jd›�ƒ td| jd›d| j d›d�ƒ | S )NzfX_val and y_val are required. They must be split from the training fold, not taken from the test fold.r   z%X and y have different sample counts.z-X_val and y_val have different sample counts.r«   z*X and X_val have different feature counts.zCclassifier_feature_names does not match the number of columns in X.z4The training data contains no grades below boundary.z7The training data contains no grades at/above boundary.zX.shape, y_is_low.shape: Ú z!X_val.shape, y_val_is_low.shape: zPositive samples: zNegative samples: zRaw scale_pos_weight: z.6f)rœ   r�   z*X_val_regular.shape, y_val_regular.shape: Tz+
Below-one heterogeneous grade model fittedzClassification boundary: z.2fzSamples below boundary: zSamples at/above boundary: zFinal low-grade threshold: z.4fzLow-grade class weight: zRegression range: [z, ú])!rj   r†   r�   rƒ   re   rq   r1   Úlenr,   Úastyperk   Úint64r4   Úsumrt   ru   r-   Úmeanrr   rZ   Úprintræ   ro   rž   rs   r.   rè   rp   r¡   rv   r0   rW   rX   )rf   r…   rŒ   rœ   r�   Úy_is_lowÚy_val_is_lowÚregular_train_maskÚregular_val_maskÚ	X_regularÚ	y_regularÚX_val_regularÚy_val_regularrg   rg   rh   rž   T  s
  ÿ



ÿÿ
ÿÿþ
ÿ
ÿÿ
ÿÿÿÿÿÿÿ	ÿüÿ
ÿÿÿÿÿûÿ
ÿÿÿÿÿÿÿÿþÿzBelowOneRLGradeRegressor.fitc                 C  s   | j stdƒ‚d S )Nz.BelowOneRLGradeRegressor must be fitted first.)rv   ÚRuntimeErrorrw   rg   rg   rh   Ú_check_fittedú  s
   ÿÿz&BelowOneRLGradeRegressor._check_fittedc                 C  sL   |   ¡  |  |¡}|jd | jkr td| j› d|jd › d�ƒ‚| j |¡S )Nr«   z	Expected z features; received r{   )rú   r†   rƒ   rq   rj   ro   Úpredict_positive_proba)rf   r…   rg   rg   rh   Úpredict_low_probability   s   

ÿÿþz0BelowOneRLGradeRegressor.predict_low_probabilityc                 C  s0   |d u r| j nt|ƒ}|  |¡}||k tj¡S r[   )rs   r-   rü   rì   rk   rí   )rf   r…   Ú	thresholdÚprobabilityrg   rg   rh   Úpredict_low_class  s   ÿý
z*BelowOneRLGradeRegressor.predict_low_classc                 C  s    t | jtƒr<| j ¡ }|dkr| jd|  }n1|dkr$tj|tjd�}n$|dkr3tj|| j	tjd�}nt
d| j›d�ƒ‚tj|t| jƒtjd�}t |d| j¡S )	Nr   r
   Úzerory   Útraining_meanz#Unknown low_grade_output strategy: r{   rn   )Ú
isinstancer\   ÚstrÚlowerr,   rk   Ú
zeros_liker�   Ú	full_likerr   rj   r-   Úclip)rf   Úlow_probabilityÚstrategyr�   rg   rg   rh   Ú_predict_low_grade_value  s8   
ÿþýÿÿýz1BelowOneRLGradeRegressor._predict_low_grade_valuec                 C  s~   |   ¡  |  |¡}|  |¡}|| jk}tj| j |¡tjd� 	d¡}t 
|| j| j¡}| ¡ }t |¡r=|  || ¡||< |S )Nry   r‡   )rú   r†   rü   rs   rk   r€   rp   Úpredictr�   rˆ   r  rW   rX   ÚcopyÚanyr
  )rf   r…   r  Úlow_maskÚregular_predictionÚ
predictionrg   rg   rh   r  ?  s2   

ÿÿ
þýý
ÿÿz BelowOneRLGradeRegressor.predictc                 C  sv   |   ¡  |  |¡}|  |¡}|| jk tj¡}tj| j 	|¡tj
d� d¡}t || j| j¡}|  	|¡}||||dœS )z Return diagnostics for each row.ry   r‡   )r  Ú	low_classÚregular_grade_predictionÚfinal_grade_prediction)rú   r†   rü   rs   rì   rk   rí   r€   rp   r  r�   rˆ   r  rW   rX   )rf   r…   r  r  r  Úfinal_predictionrg   rg   rh   Úpredict_componentsb  s2   

ÿý
þýý
üz+BelowOneRLGradeRegressor.predict_componentsc                 C  sB   |   ¡  |  |¡}|  |¡}|| jk  tj¡}| jj||| j	d�S )N)rý   )
rú   r†   r�   r,   rì   rk   rí   ro   Úevaluate_binaryrs   )rf   r…   rŒ   rñ   rg   rg   rh   Úevaluate_classifier‚  s   

ýz,BelowOneRLGradeRegressor.evaluate_classifierc                 C  ó   |   ¡  | j ¡ S r[   )rú   ro   Úget_base_model_reportrw   rg   rg   rh   Úget_base_classifier_report�  ó   þz3BelowOneRLGradeRegressor.get_base_classifier_reportc                 C  r  r[   )rú   ro   Úget_meta_coefficientsrw   rg   rg   rh   Ú get_meta_classifier_coefficients–  r  z9BelowOneRLGradeRegressor.get_meta_classifier_coefficientsc                 C  s@   |   ¡  | jj d¡}|du rg S t|ddƒ}|du rg S |ƒ S )z@Return the ARL feature-policy report when ARL-FSGB was retained.rä   NÚget_feature_policy_report)rú   ro   Úfitted_estimators_ÚgetÚgetattr)rf   Ú	arl_modelÚmethodrg   rg   rh   Úget_arl_feature_report�  s   ýýz/BelowOneRLGradeRegressor.get_arl_feature_reportc                 C  sT   t |ƒ}|jjddd� | d¡�}t | |¡ W d   ƒ d S 1 s#w   Y  d S )NT)ÚparentsÚexist_okÚwb)r   ÚparentÚmkdirÚopenÚpickleÚdump)rf   ÚpathÚfilerg   rg   rh   Ú
save_model³  s
   "ÿz#BelowOneRLGradeRegressor.save_modelc                 C  sN   t | ƒ d¡�}t |¡}W d   ƒ n1 sw   Y  t|tƒs%tdƒ‚|S )NÚrbz2The loaded object is not BelowOneRLGradeRegressor.)r   r*  r+  Úloadr  r	   Ú	TypeError)r-  r.  Úmodelrg   rg   rh   Ú
load_modelº  s   ÿ
ÿz#BelowOneRLGradeRegressor.load_model)0r
   Nr   r   Nr   r   r   r   r   r   r   r   r   r   r   r   TTTr   r   r   r   r   r   r   r   r   r   r    r!   r"   r   r   r#   r$   r%   r&   r   r'   r(   Nr*   NNr+   T)Tr,   r-   r.   r/   r0   r-   r1   r2   r3   r4   r5   r-   r6   r4   r7   r-   r8   r-   r9   r-   r:   r4   r;   r4   r<   r-   r=   r4   r>   r-   r?   r@   rA   r@   rB   r@   rC   r-   rD   r-   rE   r-   rF   r-   rG   r-   rH   r-   rI   r-   rJ   r4   rK   r4   rL   r4   rM   r-   rN   r4   rO   r-   rP   r-   rQ   r4   rR   r-   rS   r-   rT   r4   rU   r4   rV   r4   rW   r/   rX   r-   rY   r4   rZ   r@   )NN)Fr[   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__ri   rd   re   Ústaticmethodr†   r�   r›   r¡   r¶   rÜ   ræ   rè   rž   rú   rü   rÿ   r
  r  r  r  r  r  r$  r/  r4  rg   rg   rg   rh   r	      s¬    '· M%


û
ý-PA
û '
%# r	   )Ú
__future__r   r  r”   r+  Úpathlibr   Útypingr   r   Únumpyrk   Ú%adaptive_rl_feature_subspace_boostingr   Ú"class_aware_heterogeneous_stackingr   r   r	   rg   rg   rg   rh   Ú<module>   s    