
    \!h                     0    S SK r S SKrS SKJr     SS jrg)    N)minimizec                   ^ ^^^^^^ Ub  Sm[        TS5      (       d  [        S5      eOSmUc  TmOUm[        R                  R	                  U5      nX7R                  UR                  S   5         n[        R                  " [        R                  " USS9T -
  5      mU4S jmUUUU UU4S jn	[        XUS	S
9n
U
S   (       d  [        R                  " U
S   5        U
S   nU$ )a  
Implementation of the counterfactual method by Wachter et al. 2017

References:

- Wachter, S., Mittelstadt, B., & Russell, C. (2017).
Counterfactual explanations without opening the black box:
 Automated decisions and the GDPR. Harv. JL & Tech., 31, 841.,
 https://arxiv.org/abs/1711.00399

Parameters
----------

x_reference : array-like, shape=[m_features]
    The data instance (training example) to be explained.

y_desired : int
    The desired class label for `x_reference`.

model : estimator
    A (scikit-learn) estimator implementing `.predict()` and/or
    `predict_proba()`.
    - If `model` supports `predict_proba()`, then this is used by
    default for the first loss term,
    `(lambda * model.predict[_proba](x_counterfact) - y_desired[_proba])^2`
    - Otherwise, method will fall back to `predict`.

X_dataset : array-like, shape=[n_examples, m_features]
    A (training) dataset for picking the initial counterfactual
    as initial value for starting the optimization procedure.

y_desired_proba : float (default: None)
    A float within the range [0, 1] designating the desired
    class probability for `y_desired`.
    - If `y_desired_proba=None` (default), the first loss term
    is `(lambda * model(x_counterfact) - y_desired)^2` where `y_desired`
    is a class label
    - If `y_desired_proba` is not None, the first loss term
    is `(lambda * model(x_counterfact) - y_desired_proba)^2`

lammbda : Weighting parameter for the first loss term,
    `(lambda * model(x_counterfact) - y_desired[_proba])^2`

random_seed : int (default=None)
    If int, random_seed is the seed used by
    the random number generator for selecting the inital counterfactual
    from `X_dataset`.

Tpredict_probazhYour `model` does not support `predict_proba`. Set `y_desired_proba`  to `None` to use `predict`instead.Fr   )axisc                 f   > [         R                  " X-
  5      n[         R                  " UT-  5      $ )N)npabssum)x_referencex_counterfact	numeratormads      rC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\mlxtend/evaluate/counterfactual.pydist#create_counterfactual.<locals>.dista   s'    FF;67	vvi#o&&    c                    > T(       a3  TR                  U R                  SS5      5      R                  5       T   nO!TR                  U R                  SS5      5      nXT	-
  S-  -  nUT" TU 5      -   $ )N      )r   reshapeflattenpredict)
r   lammbda	y_predictdiffr   model	use_probar   	y_desiredy_to_be_annealed_tos
       r   loss#create_counterfactual.<locals>.losse   sv    ++M,A,A!R,HIQQSI m&;&;Ar&BCI&99a??d;666r   zNelder-Mead)argsmethodsuccessmessagex)hasattrAttributeErrorr   randomRandomStaterandintshaper	   medianr   warningswarn)r   r   r   	X_datasety_desired_probar   random_seedrngr   r!   resr   r   r   r    s   ```        @@@@r   create_counterfactualr6      s    t "	uo.. 6  / 	' . ))


,Ckk)//!*<=>M &&91-;
<C'
7 
7 4g}
MCy>c)n%HMr   )Ng?N)r/   numpyr   scipy.optimizer   r6    r   r   <module>r:      s!      # jr   