
    ^j                     .   d Z ddlmZmZmZ ddlZddlmZm	Z	m
Z
 ddlmZmZ dedefd	Zded
ee
   de
fdZdedeeef   dee	   defdZdddddddedeeef   deeeef      ded
ee
   dee	   dedeej&                  ej&                  f   fdZdgZy)z.Interpretability functions for XGBoost models.    )OptionalTupleUnionN   )	ArrayLikeFloatCompatibleIterationRange)BoosterDMatrixmodelreturnc                     t        | t              r| S t        | dd       }t        |      st	        d       |       }t        |t              st	        d      |S )Nget_boosterzC`model` must be an xgboost.Booster or an object with get_booster().z5`model.get_booster()` must return an xgboost.Booster.)
isinstancer
   getattrcallable	TypeError)r   r   boosters      \/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/xgboost/interpret.py_as_boosterr      s[    %!%5KK Q
 	
 mGgw'OPPN    iteration_rangec                 :    t        | dd       }| ||      S |y|S )N_get_iteration_range)r   r   )r   )r   r   get_iteration_ranges      r   r   r      s4     "%)?F&"?33r   Xmissingc                     t        |t              r|t        d      |S t        |||nt        | dd       t        | dd       t        | dd       t        | dd            S )Nz6`missing` must not be specified when `X` is a DMatrix.r   n_jobsfeature_typesenable_categoricalF)r   nthreadr    r!   )r   r   
ValueErrorr   )r   r   r   s      r   _as_prediction_dmatrixr$   $   sm     !WUVV	".GE9d4Sx.e_d;"5*>F r   FT)X_backgroundoutput_marginr   r   validate_featuresr%   r&   r'   c          	          |t        d      |}t        |       }t        | ||      }	|j                  |	d|t	        | |            }
|
dddf   }|
d   }||fS )aF  Return SHAP values for an XGBoost model.

    .. warning::

      This function is still working in progress.

    This function accepts either a :py:class:`xgboost.Booster` or an sklearn-style
    XGBoost model and returns feature contributions together with the separated
    bias term.

    Parameters
    ----------
    model :
        XGBoost booster or sklearn-style XGBoost model.
    X :
        Input data.
    X_background :
        Background data for interventional SHAP values. This is reserved for a
        future implementation and is currently unsupported.
    output_margin :
        Accepted for API compatibility. SHAP contributions currently correspond
        to the model margin.
    iteration_range :
        Specifies which layer of trees are used in prediction.
    missing :
        Value in array-like ``X`` to treat as missing. When None, use the
        model's missing value if available, otherwise ``np.nan``. This must not
        be specified when ``X`` is already a DMatrix.
    validate_features :
        Validate feature names between the model and input data.

    Returns
    -------
    values, bias :
        ``values`` contains feature SHAP values with the bias term removed.
        ``bias`` contains the separated bias term. For multi-target models, the
        output shape follows the corresponding prediction shape with the final
        feature dimension split into ``values`` and ``bias``.

    Notes
    -----
    To use GPU algorithms, configure the model before calling this function, for
    example with ``booster.set_param({"device": "cuda"})``.
    Nz$`X_background` is not yet supported.T)pred_contribsr'   r   .).r*   )NotImplementedErrorr   r$   predictr   )r   r   r%   r&   r   r   r'   _r   datacontribsvaluesbiass                r   shap_valuesr2   5   s    l !"HII 	A% G!%G4D+,UOD	  H c3B3hFGD4<r   r2   )__doc__typingr   r   r   numpynp_typingr   r   r	   corer
   r   objectr   r   r$   boolndarrayr2   __all__ r   r   <module>r>      s-   4 ) )  ? ? "v ' $,^$<GY./:B?:S* 9=04)-"GGWi G 5)!345	G
 G n-G o&G G 2::rzz!"GT /r   