o
    ¹Shj··  ã                   @  s^   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	m
Z
 d dlZG dd„ dƒZdS )é    )ÚannotationsN)ÚPath)ÚOptionalÚSequenceÚUnionc                %   @  s¼  e Zd ZdZdddddddd	d
dddddddddddddddddddddej ddddddf%d˜dJdK„ZdLdM„ ZdNdO„ Ze	dPdQ„ ƒZ
edRdS„ ƒZe	dTdU„ ƒZe	dVdW„ ƒZdXdY„ ZdZd[„ Zd\d]„ Zd^d_„ Zd`da„ Zdbdc„ Zddde„ Zdfdg„ Zdhdi„ Ze	djdk„ ƒZdldm„ Zdndo„ Zdpdq„ Zdrds„ Zdtdu„ Ze	dvdw„ ƒZdxdy„ Zdzd{„ Z d|d}„ Z!d~d„ Z"d™d€d�„Z#d‚dƒ„ Z$d„d…„ Z%d†d‡„ Z&dšdˆd‰„Z'dŠd‹„ Z(dšdŒd�„Z)dŽd�„ Z*d�d‘„ Z+d’d“„ Z,d”d•„ Z-e	d–d—„ ƒZ.dS )›Ú+AdaptiveRLFeatureSubspaceBoostingClassifierab  Adaptive Reinforcement-Learning Feature-Subspace ensemble.

    Designed to wrap an existing binary classifier such as
    RL_GradientBoostingClassifierfromScratch.

    Required base-estimator interface:
        fit(X, y, X_val=None, y_val=None)
        predict_proba(X)
        predict(X)
        classes_

    Pipeline:
        1. Remove constant / mostly-missing features.
        2. Compute feature entropy and target information gain.
        3. Build correlation clusters to reduce redundancy.
        4. Sample adaptive feature subspaces.
        5. Train one classifier per RL round.
        6. Calculate validation reward:
             0.35 * minority F1
           + 0.30 * minority recall
           + 0.20 * majority recall
           + 0.15 * balanced accuracy
        7. Update feature probabilities from reward advantage.
        8. Select a validation-quality/diversity ensemble.
        9. Calibrate the final classification threshold.

    Notes:
        - X_val/y_val must come from the training fold, not the test fold.
        - Positive class should be the minority/low-grade class, normally 1.
    Né   é   gÍÌÌÌÌÌì?é   g      è?ç333333Ã?çš™™™™™¹?g333333ë?ià.  é   )ç      Ð?gš™™™™™Ù?gš™™™™™á?é   é   çš™™™™™É?g
×£p=
ï?gš™™™™™©?çffffffÖ?gñhãˆµøä>gš™™™™™é?g333333Ó?g…ëQ¸…ï?g      ø?é   r   Úautogffffffî?éµ   é*   TÚfeature_namesúOptional[Sequence[str]]Úentropy_binsÚintÚmaximum_missing_fractionÚfloatÚminimum_unique_valuesÚinitial_information_gain_weightÚinitial_entropy_weightÚinitial_coverage_weightÚcorrelation_thresholdÚcorrelation_sample_sizeÚn_roundsÚsubspace_sizesúSequence[Union[int, float]]Úminimum_features_per_subspaceÚmaximum_features_per_clusterÚexploration_rateÚexploration_decayÚminimum_exploration_rateÚfeature_learning_rateÚfeature_probability_floorÚprobability_temperatureÚreward_minority_f1_weightÚreward_minority_recall_weightÚreward_majority_recall_weightÚreward_balanced_accuracy_weightÚminimum_majority_recallÚminimum_minority_recallÚmajority_recall_penaltyÚminority_recall_penaltyÚmax_selected_modelsÚdiversity_strengthÚminimum_model_rewardÚdecision_thresholdúUnion[str, float]Úthreshold_minÚthreshold_maxÚthreshold_grid_sizeÚrandom_stateÚverboseÚboolc'           '      C  s�  || _ |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)|&ƒ| _*|  +¡  |  ,¡  d S ©N)-Úbase_estimatorÚlistr   Úpositive_classr   r   r   r   r   r   r   r    r!   r"   r#   Útupler$   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r;   r<   r=   r>   r@   r?   Ú_validate_parametersÚ_reset_fitted_state)'ÚselfrB   r   rD   r   r   r   r   r   r    r!   r"   r#   r$   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r;   r<   r=   r>   r?   © rI   úM/root/disk_3tb/earlywarning/30072026/adaptive_rl_feature_subspace_boosting.pyÚ__init__,   sT   8




















ÿ











z4AdaptiveRLFeatureSubspaceBoostingClassifier.__init__c                 C  s  | j dk r	tdƒ‚d| j  krdkstdƒ‚ tdƒ‚| j| j | j }t |d¡s/tdƒ‚| j| j	 | j
 | j }t |d¡sEtdƒ‚| jdk rNtd	ƒ‚| jsUtd
ƒ‚| jdk r^tdƒ‚d| j  krldksqtdƒ‚ tdƒ‚d| j  k r…| j  k r…dk sŠtdƒ‚ tdƒ‚d S )Nr
   z entropy_bins must be at least 2.ç        ç      ð?z1maximum_missing_fraction must be between 0 and 1.z,Initial feature-score weights must sum to 1.zReward weights must sum to 1.r   zn_rounds must be at least 1.zsubspace_sizes cannot be empty.z'max_selected_models must be at least 1.z%diversity_strength must be in [0, 1].z0Threshold limits must satisfy 0 < min < max < 1.)r   Ú
ValueErrorr   r   r   r    ÚnpÚiscloser.   r/   r0   r1   r#   r$   r6   r7   r;   r<   )rH   Úinitial_weight_sumÚreward_weight_sumrI   rI   rJ   rF   ˜   sV   
ÿÿÿÿþÿÿþýÿ

ÿ ÿÿz@AdaptiveRLFeatureSubspaceBoostingClassifier._validate_parametersc                 C  sÈ   d | _ d | _d | _d | _d | _d | _d | _d | _d | _d | _	d | _
d | _d | _d | _d | _d | _d | _d | _d | _d | _g | _d | _g | _g | _d | _d| _d | _d | _d| _tj | j ¡| _!d| _"d S )Nç      à?rL   F)#Ún_features_in_Úclasses_Úpositive_class_Únegative_class_Úpositive_class_index_Únegative_class_index_Úfeature_entropy_Úfeature_information_gain_Ú$feature_normalized_information_gain_Úfeature_missing_fraction_Úfeature_unique_counts_Úfeature_initial_scores_Úfeature_logits_Úfeature_probabilities_Úfeature_selection_counts_Úfeature_reward_sums_Úusable_feature_mask_Úusable_feature_indices_Úexcluded_feature_indices_Úfeature_cluster_labels_Úfeature_clusters_Úcluster_probabilities_Úround_results_Úselected_models_Úselected_model_weights_Úclassification_threshold_Úvalidation_ensemble_metrics_Úfeature_importances_Úreward_baseline_rO   ÚrandomÚdefault_rngr>   Úrng_Ú
is_fitted_©rH   rI   rI   rJ   rG   ¹   s>   
z?AdaptiveRLFeatureSubspaceBoostingClassifier._reset_fitted_statec                 C  s\   t | dƒr	|  ¡ } tj| tjd�} | jdkrtd| j› d�ƒ‚|  ¡ } tj	| t 
| ¡ < | S )NÚto_numpy©Údtyper
   zX must be 2D, received shape Ú.)Úhasattrrv   rO   ÚasarrayÚfloat64ÚndimrN   ÚshapeÚcopyÚnanÚisfinite)ÚXrI   rI   rJ   Ú_prepare_X_rawß   s   

z:AdaptiveRLFeatureSubspaceBoostingClassifier._prepare_X_rawc                 C  s   |   |¡}tj|dddd�S )NrL   ©r€   ÚposinfÚneginf)rƒ   rO   Ú
nan_to_num)Úclsr‚   rI   rI   rJ   Ú
_prepare_Xê   s   
z6AdaptiveRLFeatureSubspaceBoostingClassifier._prepare_Xc                 C  s8   t | dƒr	|  ¡ } t | ¡ d¡} | jdkrtdƒ‚| S )Nrv   éÿÿÿÿr   zy cannot be empty.)rz   rv   rO   r{   ÚreshapeÚsizerN   )ÚyrI   rI   rJ   Ú
_prepare_yï   s   

z6AdaptiveRLFeatureSubspaceBoostingClassifier._prepare_yc                 C  sN   t j| t jd�} |  ¡ }|dkrdS | | dk | }tt  |t  |¡ ¡ ƒS )Nrw   r   rL   )rO   r{   r|   Úsumr   Úlog2)ÚcountsÚtotalÚprobabilityrI   rI   rJ   Ú_entropy_from_countsø   s   z@AdaptiveRLFeatureSubspaceBoostingClassifier._entropy_from_countsc                   sì   t j|t jd�}t  |¡}t jt|ƒdt jd�}|| }|jdkr#|S t  |¡}|j| j	krJdd„ t
|ƒD ƒ‰ t j‡ fdd„|D ƒt jd�||< |S t  t  |t  dd	| j	d
 ¡¡¡}|jdkrfd||< |S t j||d
d… dd�||< |S )Nrw   rŠ   r   c                 S  s   i | ]\}}||“qS rI   rI   )Ú.0ÚindexÚvaluerI   rI   rJ   Ú
<dictcomp>  s    zSAdaptiveRLFeatureSubspaceBoostingClassifier._discretize_feature.<locals>.<dictcomp>c                   s   g | ]}ˆ | ‘qS rI   rI   ©r•   r—   ©ÚmappingrI   rJ   Ú
<listcomp>  s    zSAdaptiveRLFeatureSubspaceBoostingClassifier._discretize_feature.<locals>.<listcomp>rL   rM   r   r
   T)Úright)rO   r{   r|   r�   ÚfullÚlenÚint64rŒ   Úuniquer   Ú	enumerateÚquantileÚlinspaceÚdigitize)rH   ÚvaluesÚfiniteÚencodedÚfinite_valuesr¡   ÚedgesrI   rš   rJ   Ú_discretize_feature  s:   



þþÿ
û

ýz?AdaptiveRLFeatureSubspaceBoostingClassifier._discretize_featurec                 C  s´   t  |¡| _| jjdkrtd| j› d�ƒ‚| j| jvr(td| j›d| j› d�ƒ‚| j| _| j| j| jk d | _tt  	| j| jk¡d d ƒ| _
tt  	| j| jk¡d d ƒ| _d S )Nr
   z2This classifier requires exactly two classes; got ry   zpositive_class=z is not in r   )rO   r¡   rU   rŒ   rN   rD   rV   rW   r   ÚwhererX   rY   )rH   r�   rI   rI   rJ   Ú_initialize_classes#  s*   ÿÿ
ÿþÿ
ÿz?AdaptiveRLFeatureSubspaceBoostingClassifier._initialize_classesc                 C  sR   | j d u rdd„ t| jƒD ƒ| _ t| j ƒ| jkr'tdt| j ƒ› d| j› d�ƒ‚d S )Nc                 S  s   g | ]}d |› �‘qS )Úfeature_rI   ©r•   r–   rI   rI   rJ   rœ   :  ó    
ÿzYAdaptiveRLFeatureSubspaceBoostingClassifier._initialize_feature_names.<locals>.<listcomp>zfeature_names contains z names but X contains z	 columns.)r   ÚrangerT   rŸ   rN   ru   rI   rI   rJ   Ú_initialize_feature_names8  s   
ÿÿÿÿzEAdaptiveRLFeatureSubspaceBoostingClassifier._initialize_feature_namesc              
   C  s¾  || j k tj¡}|  tj|dd�¡}|jd }tj|tjd�}tj|tjd�}tj|tjd�}tj|tjd�}	tj|tjd�}
tj	|t
d�}t|ƒD ]•}|d d …|f }t |¡}dtt |¡ƒ |	|< tt || ¡jƒ|
|< |	| | jks|
| | jk r„d||< qK|  |¡d }t |¡}|  |¡}t tt |¡dƒ¡}|t|dƒ ||< d}t |¡D ]}||k}|tt |¡ƒ|  tj|| dd�¡ 7 }q°t|| dƒ}|||< |t|dƒ ||< qKd|	 }| j| | j|  | j|  }d|| < || _|| _|| _|	| _|
| _ || _!|| _"t #|¡| _$t #| ¡| _%| j$jd	k�r&t&d
ƒ‚|| j$ }|t |¡ }t '|tj( ¡| _)|t| j*dƒ | j)| j$< tj|tjd�| _+tj|tjd�| _,|  -¡  d S )Nr
   )Ú	minlengthr   rw   rM   Fçê-�™—q=rL   r   z*No usable features remain after profiling.ç�íµ ÷Æ°>).rV   ÚastyperO   r    r”   Úbincountr~   Úzerosr|   Úonesr@   r±   r�   r   Úmeanr   r¡   rŒ   r   r   r«   Úmathr�   ÚmaxÚcount_nonzeror   r   r    rZ   r[   r\   r]   r^   r_   rd   Úflatnonzerore   rf   rN   rž   Úinfr`   r-   rb   rc   Ú_refresh_feature_probabilities)rH   ÚX_rawr�   ÚpositiveÚtarget_entropyÚ
n_featuresÚentropyÚinformation_gainÚnormalized_information_gainÚmissing_fractionÚunique_countsÚusableÚfeature_indexr¦   r§   ÚbinsÚ
bin_countsÚraw_entropyÚmaximum_entropyÚconditional_entropyÚbin_idÚmaskÚgainÚcoverageÚinitial_scoresÚusable_scoresrI   rI   rJ   Ú_profile_featuresC  sª   ÿ
þ
ÿÿÿ

ÿÿÿÿÿÿþýÿ
ÿ

ÿþþz=AdaptiveRLFeatureSubspaceBoostingClassifier._profile_featuresc                 C  sb   | j | j }|t |¡ }t |¡}t || j¡}|| ¡  }tj| j	tj
d�| _|| j| j< d S )Nrw   )r`   re   rO   r¼   ÚexpÚmaximumr,   r�   r¸   rT   r|   ra   )rH   Úlogitsr“   rI   rI   rJ   rÀ   ¦  s   
þþÿzJAdaptiveRLFeatureSubspaceBoostingClassifier._refresh_feature_probabilitiesc                   s†  ˆ j ‰ˆjdkr'tjˆ jdtjd�ˆ _dˆ jˆ< ˆ ¡ gˆ _t 	dg¡ˆ _
d S |jd }|ˆ jkrFˆ jj|ˆ jdd�}|| d d …ˆf }n|d d …ˆf }| ¡ }tj|dd�}d	|t |¡ < t t |¡ ¡\}}|| |||f< tj|dd�}|d
k}	t |¡}
|d d …|	f tj|d d …|	f dd� ||	  |
d d …|	f< t tj|
dd�¡}tj|d	d	d	d�}t |d¡ ttˆjƒƒ}g }|�rt|‡ ‡fdd„d�}|g}tƒ }|�r| ¡ }||vråqÙ| |¡ | |¡ t || ˆ j k¡}|D ]}t!|ƒ}||v �r| "|¡ qû|sÜ| "ˆtj	t#|ƒtjd� ¡ |sÈ|ˆ _tjˆ jdtjd�}t$|ƒD ]	\}}|||< �q0|ˆ _ˆ  %¡  d S )Nr   rŠ   rw   r   rM   F)rŒ   Úreplace)ÚaxisrL   r´   )Úrowvarr„   c                   s   ˆ j ˆ|   S rA   )r_   )Úlocal_index©rH   rÊ   rI   rJ   Ú<lambda>ñ  s    ÿzYAdaptiveRLFeatureSubspaceBoostingClassifier._build_correlation_clusters.<locals>.<lambda>©Úkey)&re   rŒ   rO   rž   rT   r    rg   r   rh   r{   ri   r~   r"   rs   ÚchoiceÚ	nanmedianr�   r¬   ÚstdÚ
zeros_likerº   ÚabsÚcorrcoefr‡   Úfill_diagonalÚsetr±   r¼   ÚpopÚremoveÚaddr¾   r!   r   ÚappendÚsortedr¢   Ú_refresh_cluster_probabilities)rH   rÁ   Ú	row_countÚrowsÚsampleÚmediansÚmissing_rowsÚmissing_columnsÚstandard_deviationÚvariableÚstandardizedÚcorrelationÚ
unassignedÚlocal_clustersÚseedÚqueueÚ	componentÚcurrentÚ
neighboursÚ	neighbourÚlabelsÚcluster_indexÚclusterrI   rß   rJ   Ú_build_correlation_clusters¸  sš   
ý


ý
ÿýÿüþ

ÿ

€óÿçzGAdaptiveRLFeatureSubspaceBoostingClassifier._build_correlation_clustersc                   s@   t j‡ fdd„ˆ jD ƒt jd�}t  |ˆ j¡}|| ¡  ˆ _d S )Nc                   s   g | ]	}ˆ j |  ¡ ‘qS rI   )ra   r�   )r•   r  ru   rI   rJ   rœ     s    ÿÿz^AdaptiveRLFeatureSubspaceBoostingClassifier._refresh_cluster_probabilities.<locals>.<listcomp>rw   )rO   r{   rh   r|   rÙ   r,   r�   ri   )rH   Úcluster_scoresrI   ru   rJ   rð     s   
þûþzJAdaptiveRLFeatureSubspaceBoostingClassifier._refresh_cluster_probabilitiesc                 C  sp   t | jƒ}t|tƒr%d|  k rdkstdƒ‚ tdƒ‚tt|| ƒƒ}nt|ƒ}t|| jƒ}tt	 
|d|¡ƒS )NrL   rM   z'Float subspace sizes must be in (0, 1].r   )rŸ   re   Ú
isinstancer   rN   r   Úroundr¼   r&   rO   Úclip)rH   Úrequested_sizeÚusable_countrŒ   rI   rI   rJ   Ú_resolve_subspace_size   s   

ÿÿÿzBAdaptiveRLFeatureSubspaceBoostingClassifier._resolve_subspace_sizec                   sú  |   |¡}g }tƒ ‰ tjt| jƒtjd�}d}td|d ƒ}t|ƒ|k r°||k r°|d7 }| j 	¡ |k r?t
| j t| jƒ¡ƒ}nt
| jjt| jƒ| jd�ƒ}|| | jkrUq| j| }	tj‡ fdd„|	D ƒtjd�}
|
jdkroq| j 	¡ |k rt
| j |
¡ƒ}n| j|
 }|| ¡  }t
| jj|
|d�ƒ}| |¡ ˆ  |¡ ||  d7  < t|ƒ|k r°||k s(t|ƒ|k rótj‡ fd	d„| jD ƒtjd�}t|t|ƒ |jƒ}|dkró| j| }|| ¡  }| jj||d
|d�}| dd„ |D ƒ¡ tjt|ƒtjd�S )Nrw   r   iè  éd   r   )Úpc                   ó   g | ]
}t |ƒˆ vr|‘qS rI   ©r   ©r•   Úfeature©Ú
chosen_setrI   rJ   rœ   Q  ó
    þzPAdaptiveRLFeatureSubspaceBoostingClassifier._sample_subspace.<locals>.<listcomp>c                   r  rI   r  r  r  rI   rJ   rœ   k  r  F)rŒ   rÛ   r  c                 s  s   � | ]}t |ƒV  qd S rA   r  r™   rI   rI   rJ   Ú	<genexpr>|  s   € zOAdaptiveRLFeatureSubspaceBoostingClassifier._sample_subspace.<locals>.<genexpr>)r  rê   rO   r¸   rŸ   rh   r    r¼   rs   rq   r   Úintegersrã   ri   r'   r{   rŒ   ra   r�   rî   rí   re   ÚminÚextendrï   )rH   r  ÚepsilonÚtarget_sizeÚchosenÚcluster_countsÚattemptsÚmaximum_attemptsr  r  Ú	availabler  r“   Ú	remainingÚneededÚextrarI   r  rJ   Ú_sample_subspace.  s€   
þÿþÿÿ

þú

ÿ

Ô/
þú
üz<AdaptiveRLFeatureSubspaceBoostingClassifier._sample_subspacec                 C  s$  | |k}||k}t t ||@ ¡ƒ}t t | |@ ¡ƒ}t t || @ ¡ƒ}t t | | @ ¡ƒ}|t|| dƒ }	|t|| dƒ }
|t|| dƒ }d|	 |
 t|	|
 dƒ }d|
|  }t t|
dƒt|dƒ ¡}|| tt| ƒdƒ }t|ƒt|	ƒt|
ƒt|ƒt|ƒt|ƒt|ƒ||||dœS )Nr   g       @gVçž¯Ò<rS   rL   )ÚaccuracyÚminority_precisionÚminority_recallÚmajority_recallÚminority_f1Úbalanced_accuracyÚgmeanÚtrue_positiveÚfalse_positiveÚfalse_negativeÚtrue_negative)r   rO   r�   r¼   r»   ÚsqrtrŸ   r   )Úy_trueÚy_predrD   rÂ   Úpredicted_positiveÚtpÚfpÚfnÚtnÚ	precisionr(  r)  r*  r+  r,  r&  rI   rI   rJ   Ú_binary_metrics€  sD   
ÿÿÿÿÿõz;AdaptiveRLFeatureSubspaceBoostingClassifier._binary_metricsc                 C  sŒ   | j |d  | j|d   | j|d   | j|d   }|d | jk r/|| j| j|d   8 }|d | jk rB|| j| j|d   8 }t|ƒS )Nr*  r(  r)  r+  )	r.   r/   r0   r1   r2   r4   r3   r5   r   )rH   ÚmetricsÚrewardrI   rI   rJ   Ú_calculate_reward¨  s:   ÿÿþÿüÿúÿÿÿÿÿz=AdaptiveRLFeatureSubspaceBoostingClassifier._calculate_rewardc                 C  sV   t  | j¡}| j| d }t|dƒr||_t|dƒr!tj |¡|_t|dƒr)d|_	|S )Nr   r>   Úrngr?   F)
r   ÚdeepcopyrB   r>   rz   rO   rq   rr   r>  r?   )rH   Úround_indexÚ	estimatorrý   rI   rI   rJ   Ú_clone_estimatorÂ  s   


z<AdaptiveRLFeatureSubspaceBoostingClassifier._clone_estimatorc                 C  sP   t  |j¡}t  || jk¡d }|jdkrtdƒ‚t  |¡d d …t|d ƒf S )Nr   r   z7The base estimator did not preserve the positive class.)rO   r{   rU   r¬   rV   rŒ   ÚRuntimeErrorr   )rH   rA  ÚprobabilitiesÚestimator_classesÚ	positionsrI   rI   rJ   Ú_positive_probabilityÏ  s   ÿþ
ÿzAAdaptiveRLFeatureSubspaceBoostingClassifier._positive_probabilityc                   s¢   g }t  ˆ jˆ jˆ j¡D ]$}t  ||kˆ jˆ j¡}ˆ  ||ˆ j¡}ˆ  	|¡}| 
t|ƒ||f¡ q‡ fdd„|D ƒ}‡ fdd„|D ƒ}	|pH|	pH|}
t|
dd„ d�S )Nc                   ó4   g | ]}|d  d ˆ j kr|d  d ˆ jkr|‘qS )r
   r)  r(  ©r2   r3   ©r•   Ú	candidateru   rI   rJ   rœ   î  ó    
ÿ
ÿüz_AdaptiveRLFeatureSubspaceBoostingClassifier._best_threshold_for_probability.<locals>.<listcomp>c                   s"   g | ]}|d  d ˆ j kr|‘qS )r
   r)  )r2   rJ  ru   rI   rJ   rœ   ö  s    
ÿþc                 S  s8   | d | d d | d d | d d t | d d ƒ fS )Nr   r
   r*  r+  r)  r   rS   )rç   )rK  rI   rI   rJ   rà      s   


ûz]AdaptiveRLFeatureSubspaceBoostingClassifier._best_threshold_for_probability.<locals>.<lambda>rá   )rO   r¤   r;   r<   r=   r¬   rV   rW   r:  r=  rî   r   r¼   )rH   Úy_valr“   Ú
candidatesÚ	thresholdÚ
predictionr;  r<  ÚstrictÚmajority_validÚeligiblerI   ru   rJ   Ú_best_threshold_for_probabilityÚ  s:   ýýý

þ
þþzKAdaptiveRLFeatureSubspaceBoostingClassifier._best_threshold_for_probabilityc           	      C  sô   |dkr|| _ nd}d| | j  ||  | _ || j  }| j| }| j|  |7  < | j|  d7  < | j|  |7  < tj| j|dd�}|jr]| j|  |t	|ƒ t
t	|ƒdƒ d 8  < | j| j }t |dd	¡}|| j| j< |  ¡  |  ¡  d S )
Nr   r   rM   r   T)Úassume_uniquer   g      4Àg      4@)rp   r+   r`   rb   rc   rO   Ú	setdiff1dre   rŒ   rŸ   r¼   r
  rÀ   rð   )	rH   Úindicesr<  r@  Úbaseline_rateÚ	advantageÚupdateÚnot_selectedÚusable_logitsrI   rI   rJ   Ú_update_feature_policy	  s8   ÿÿ

ý
ÿÿzBAdaptiveRLFeatureSubspaceBoostingClassifier._update_feature_policyc                 C  sP   t  | ¡dk st  |¡dk rdS t  | |¡d }t  |¡sdS tt  |dd¡ƒS )Nr´   rM   )r   r   g      ð¿)rO   rå   rè   r�   r   r
  )ÚfirstÚsecondrú   rI   rI   rJ   Ú_safe_correlation1  s   
z=AdaptiveRLFeatureSubspaceBoostingClassifier._safe_correlationc           	      C  sœ   t |  |d |d ¡dƒ}tt |d |d k¡ƒ}t|d  ¡ ƒ}t|d  ¡ ƒ}t||B ƒ}t||@ ƒt |dƒ }tt d| d|  d|  dd	¡ƒS )
NÚvalidation_positive_probabilityrL   Úvalidation_predictionsrW  r   gÍÌÌÌÌÌÜ?r   r   rM   )	r¼   r`  r   rO   rº   rê   ÚtolistrŸ   r
  )	rH   r^  r_  Úprobability_similarityÚprediction_agreementÚfirst_featuresÚsecond_featuresÚunionÚjaccardrI   rI   rJ   Ú_model_similarity:  s<   þûÿÿÿÿÿþûÿz=AdaptiveRLFeatureSubspaceBoostingClassifier._model_similarityc                   s\  ‡fdd„ˆj D ƒ}|stdƒ‚‡fdd„|D ƒ}|r|}t|dd„ dd�}| d	¡g}|ryt|ƒˆjk ryd }tj }|D ](‰ t‡ ‡fd
d„|D ƒƒ}d| }dˆj	 ˆ d  ˆj	|  }	|	|kre|	}ˆ }q=| 
|¡ | |¡ |ryt|ƒˆjk s5|ˆ_tjdd„ |D ƒtjd�}
t |
¡}
|
 ¡ d	ks›t |
¡ ¡ s¥tjt|ƒtjd�}
|
|
 ¡  ˆ_d S )Nc                   s   g | ]}|d  ˆ j kr|‘qS )r<  )r8   ©r•   Úresultru   rI   rJ   rœ   Z  s
    þzTAdaptiveRLFeatureSubspaceBoostingClassifier._select_final_models.<locals>.<listcomp>z'No adaptive subspace model was trained.c                   rH  )r;  r)  r(  rI  rk  ru   rI   rJ   rœ   b  rL  c                 S  ó   | d S ©Nr<  rI   )rl  rI   rI   rJ   rà   o  ó    zRAdaptiveRLFeatureSubspaceBoostingClassifier._select_final_models.<locals>.<lambda>T©râ   Úreverser   c                 3  s   � | ]	}ˆ  ˆ |¡V  qd S rA   )rj  )r•   r  ©rK  rH   rI   rJ   r  y  s
   € 
ÿ
ÿzSAdaptiveRLFeatureSubspaceBoostingClassifier._select_final_models.<locals>.<genexpr>rM   r<  c                 S  s   g | ]}t |d  dƒd ‘qS )r<  rL   rµ   )r¼   rk  rI   rI   rJ   rœ   Ž  s    ÿÿrw   )rj   rC  rï   rë   rŸ   r6   rO   r¿   r¼   r7   rî   rì   rk   r{   r|   Úsquarer�   r�   Úallr¹   rl   )rH   ÚpoolÚconstrainedr"  ÚselectedÚbest_candidateÚbest_selection_scoreÚmaximum_similarityÚ	diversityÚselection_scoreÚraw_weightsrI   rr  rJ   Ú_select_final_modelsY  s`   
þ
þýþÿþÿ€

ëþû
z@AdaptiveRLFeatureSubspaceBoostingClassifier._select_final_modelsc                 C  s    t  dd„ | jD ƒ¡}|| j S )Nc                 S  s   g | ]}|d  ‘qS )ra  rI   rk  rI   rI   rJ   rœ   ›  s    ÿÿz`AdaptiveRLFeatureSubspaceBoostingClassifier._ensemble_validation_probability.<locals>.<listcomp>)rO   Úcolumn_stackrk   rl   )rH   ÚmatrixrI   rI   rJ   Ú _ensemble_validation_probability™  s   þÿ
zLAdaptiveRLFeatureSubspaceBoostingClassifier._ensemble_validation_probabilityc                 C  s¬   t j| jt jd�}t| j| jƒD ]4\}}t|d dd ƒ}|d u r'| j|d  }t j	|t jd�}|j
|d j
kr8q||d   || 7  < q| ¡ }|dkrQ|| }|| _d S )Nrw   Úmodelro   rW  r   )rO   r¸   rT   r|   Úziprk   rl   Úgetattrra   r{   rŒ   r�   ro   )rH   Ú
importancerl  Úmodel_weightÚlocalr’   rI   rI   rJ   Ú _build_global_feature_importance¢  s    þ
zLAdaptiveRLFeatureSubspaceBoostingClassifier._build_global_feature_importancec                   sT  |d u s|d u rt dƒ‚ˆ  |¡}ˆ  |¡}tj|dddd�}tj|dddd�}ˆ  |¡}ˆ  |¡}t|ƒt|ƒkr>t dƒ‚t|ƒt|ƒkrJt dƒ‚|jd |jd krXt dƒ‚ˆ  ¡  |jd ˆ _ˆ  	|¡ ˆ  
¡  ˆ  ||¡ ˆ  |¡ ˆ jrŸtdtˆ jƒ› d	ˆ j› �ƒ td
tˆ jƒ› dˆ jd›d�ƒ tdˆ j› �ƒ ˆ j}	tˆ jƒD ]÷}
ˆ j|
tˆ jƒ  }ˆ  ||	¡}ˆ  |
¡}z<|j|d d …|f ||d d …|f |d� | |d d …|f ¡}ˆ  ||¡}ˆ  ||¡\}}}t ||kˆ jˆ j¡}W n3 t�y- } z&ˆ j�rtd|
d d›dt |ƒj!› d|› �ƒ t"ˆ j#|	ˆ j$ ƒ}	W Y d }~q§d }~ww |
d |‡ fdd„|D ƒt|ƒ|t%|ƒt%|ƒ|t%|	ƒ| &¡ | &¡ dœ}ˆ j' (|¡ ˆ  )||tˆ j'ƒd ¡ ˆ j�r•td|
d d›dt|ƒd›d|d›d|d›d|d d›d|d  d›d!|d" d›d#|	d›�ƒ t"ˆ j#|	ˆ j$ ƒ}	q§ˆ j'�s§t*d$ƒ‚ˆ  +¡  ˆ  ,¡ }ˆ j-d%k�rÅˆ  ||¡\}}}|ˆ _.|ˆ _/nt%ˆ j-ƒ}t ||kˆ jˆ j¡}|ˆ _.ˆ  0||ˆ j¡ˆ _/ˆ  1¡  d&ˆ _2ˆ j�r(td'ƒ t3ˆ j4ˆ j5ƒD ]\}}td(|d) d›d*|d›d+|d, d›d-|d. d›�ƒ �q÷td/ˆ j.d›�ƒ td0ˆ j/› �ƒ ˆ S )1NznX_val and y_val are required. Split them from the training fold; do not use the test fold for adaptive search.rL   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.zARL-FSGB usable features: ú/zCorrelation clusters: z (threshold=z.2fú)zAdaptive RL rounds: )ÚX_valrM  z  Round Ú02dz
: SKIPPED z: c                   s   g | ]}ˆ j | ‘qS rI   )r   r¯   ru   rI   rJ   rœ      r°   zCAdaptiveRLFeatureSubspaceBoostingClassifier.fit.<locals>.<listcomp>)r	  rW  r   rÄ   r‚  rO  r<  r;  r  ra  rb  z | features=Ú3dz
 | reward=z.4fz | threshold=z.3fz | majR=r)  z | minR=r(  z | F1=r*  z | epsilon=z%Every adaptive subspace round failed.r   Tz
Selected ARL-FSGB models:z  round=r	  z weight=z
 features=rÄ   z reward=r<  zFinal ensemble threshold: zValidation ensemble metrics: )6rN   rƒ   rO   r‡   rŽ   rŸ   r~   rG   rT   r­   r²   r×   r  r?   Úprintre   rh   r!   r#   r(   r±   r$   r%  rB  ÚfitÚpredict_probarG  rT  r¬   rV   rW   Ú	ExceptionÚtypeÚ__name__r¼   r*   r)   r   r   rj   rî   r]  rC  r~  r�  r9   rm   rn   r:  rˆ  rt   rƒ  rk   rl   )rH   r‚   r�   r‹  rM  rÁ   Ú	X_val_rawÚX_cleanÚX_val_cleanr  r@  r  rW  rA  Úraw_probabilityÚpositive_probabilityrO  r<  r;  rb  Úerrorrl  Úensemble_probabilityÚ_rP  ÚweightrI   ru   rJ   r�  ¸  sˆ  ÿ

üü

ÿ

ÿÿÿÿÿÿþ
üÿþþÿýÿÿÿþ€ö
ÿðýÿþýüûúùÿþÿÿþÿ
ýýÿþÿþýÿÿÿÿÿz/AdaptiveRLFeatureSubspaceBoostingClassifier.fitc                 C  s   | j stdƒ‚d S )Nz-The ARL-FSGB classifier must be fitted first.)rt   rC  ru   rI   rI   rJ   Ú_check_fitted…  s
   ÿÿz9AdaptiveRLFeatureSubspaceBoostingClassifier._check_fittedc                 C  s”   |   ¡  |  |¡}|jd | jkr td| j› d|jd › d�ƒ‚g }| jD ]}|d  |d d …|d f ¡}| |  |d |¡¡ q%t	 
|¡| j S )Nr   z	Expected z features, received ry   r‚  rW  )r�  r‰   r~   rT   rN   rk   r�  rî   rG  rO   r  rl   )rH   r‚   Úprobability_matrixrl  r—  rI   rI   rJ   Úpredict_positive_proba‹  s.   

ÿÿ
ÿþÿÿÿzBAdaptiveRLFeatureSubspaceBoostingClassifier.predict_positive_probac                 C  sN   |   |¡}tjt|ƒdftjd�}||d d …| jf< d| |d d …| jf< |S )Nr
   rw   rM   )rŸ  rO   r¸   rŸ   r|   rX   rY   )rH   r‚   r˜  rl  rI   rI   rJ   r�  ¥  s   

þÿz9AdaptiveRLFeatureSubspaceBoostingClassifier.predict_probac                 C  s6   |d u r| j nt|ƒ}|  |¡}t ||k| j| j¡S rA   )rm   r   rŸ  rO   r¬   rV   rW   )rH   r‚   rO  r˜  rI   rI   rJ   Úpredict±  s   ÿý
ýz3AdaptiveRLFeatureSubspaceBoostingClassifier.predictc                 C  s"   |   |¡}tt |  |¡|k¡ƒS rA   )rŽ   r   rO   rº   r   )rH   r‚   r�   rI   rI   rJ   Úscore¾  s   
z1AdaptiveRLFeatureSubspaceBoostingClassifier.scorec                 C  s(   |   |¡}| j||d�}|  ||| j¡S )N)rO  )rŽ   r   r:  rV   )rH   r‚   r�   rO  rP  rI   rI   rJ   Úevaluate_binaryÂ  s   
ýz;AdaptiveRLFeatureSubspaceBoostingClassifier.evaluate_binaryc                 C  s   t dd„ | jD ƒdd„ dd�S )Nc                 S  s8   g | ]}|d  |d |d |d |d dœ|d ¥‘qS )r	  rÄ   rO  r<  r  )r	  rÄ   rO  r<  r  r;  rI   rk  rI   rI   rJ   rœ   Í  s    	ùûúÿzQAdaptiveRLFeatureSubspaceBoostingClassifier.get_round_summary.<locals>.<listcomp>c                 S  rm  rn  rI   ©ÚrowrI   rI   rJ   rà   Ø  ro  zOAdaptiveRLFeatureSubspaceBoostingClassifier.get_round_summary.<locals>.<lambda>Trp  )rï   rj   ru   rI   rI   rJ   Úget_round_summaryË  s   	÷óz=AdaptiveRLFeatureSubspaceBoostingClassifier.get_round_summaryc                 C  sÞ   |   ¡  g }t| jƒD ]Z\}}t| j| ƒ}|dkr#t| j| | ƒnd}| ||t| j	| ƒt| j
| ƒt| j| ƒt| j| ƒt| j| ƒt| j| ƒt| j| ƒt| j| ƒ||t| j| ƒdœ¡ qt|dd„ dd�S )Nr   rL   )rË   Úfeature_namerÊ   r  Úunique_valuesrÈ   Únormalized_entropyrÇ   Úinitial_scoreÚfinal_probabilityÚselection_countÚaverage_selected_rewardÚensemble_importancec                 S  s   | d | d | d fS )NrÊ   rª  r­  rI   r£  rI   rI   rJ   rà     s   ýzWAdaptiveRLFeatureSubspaceBoostingClassifier.get_feature_policy_report.<locals>.<lambda>Trp  )r�  r¢   r   r   rb   r   rc   rî   r@   rd   rg   r^   r]   rZ   r\   r_   ra   ro   rï   )rH   rò   r–   ÚnameÚcountÚaverage_rewardrI   rI   rJ   Úget_feature_policy_reportÜ  sZ   ÿýÿÿÿÿÿÿÿÿåÿ"ùzEAdaptiveRLFeatureSubspaceBoostingClassifier.get_feature_policy_reportc              	   C  sZ   |   ¡  g }t| j| jƒD ]\}}| |d t|ƒ|d |d |d dœ|d ¥¡ q|S )Nr	  rÄ   rO  r<  )r	  rœ  rÄ   rO  r<  r;  )r�  rƒ  rk   rl   rî   r   )rH   rò   rl  rœ  rI   rI   rJ   Úget_selected_model_summary  s"   þûúÿ
zFAdaptiveRLFeatureSubspaceBoostingClassifier.get_selected_model_summaryc                 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)rH   ÚpathÚfilerI   rI   rJ   Ú
save_model'  s
   "ÿz6AdaptiveRLFeatureSubspaceBoostingClassifier.save_modelc                 C  sN   t | ƒ d¡�}t |¡}W d   ƒ n1 sw   Y  t|tƒs%tdƒ‚|S )NÚrbz0The loaded object is not an ARL-FSGB classifier.)r   r¸  r¹  Úloadr  r   Ú	TypeError)r»  r¼  r‚  rI   rI   rJ   Ú
load_model-  s   ÿþÿz6AdaptiveRLFeatureSubspaceBoostingClassifier.load_model)Hr   r   r   r   r   r   r   r   r   r   r   r   r    r   r!   r   r"   r   r#   r   r$   r%   r&   r   r'   r   r(   r   r)   r   r*   r   r+   r   r,   r   r-   r   r.   r   r/   r   r0   r   r1   r   r2   r   r3   r   r4   r   r5   r   r6   r   r7   r   r8   r   r9   r:   r;   r   r<   r   r=   r   r>   r   r?   r@   )NNrA   )/r“  Ú
__module__Ú__qualname__Ú__doc__rO   r¿   rK   rF   rG   Ústaticmethodrƒ   Úclassmethodr‰   rŽ   r”   r«   r­   r²   r×   rÀ   r  rð   r  r%  r:  r=  rB  rG  rT  r]  r`  rj  r~  r�  rˆ  r�  r�  rŸ  r�  r   r¡  r¢  r¥  r±  r²  r½  rÁ  rI   rI   rI   rJ   r      s¬    "Êl!&




"cZR
'/(
@	
 N

	7r   )Ú
__future__r   r   r»   r¹  Úpathlibr   Útypingr   r   r   ÚnumpyrO   r   rI   rI   rI   rJ   Ú<module>   s    