a
    �ù»j2b  ã                   @   sØ   d Z ddlZddlmZmZ ddlZddlmZ	 ddl
mZmZmZmZmZ ddlmZ ddlmZmZ ddlmZ dd	lmZ dd
lmZ ddlmZmZmZmZm Z m!Z! G dd„ deeeƒZ"G dd„ deeeƒZ#dS )z6Dummy estimators that implement simple rules of thumb.é    N)ÚIntegralÚRealé   )ÚBaseEstimatorÚClassifierMixinÚMultiOutputMixinÚRegressorMixinÚ_fit_context)Úcheck_random_state)ÚIntervalÚ
StrOptions)Úclass_distribution)Ú_random_choice_csc©Ú_weighted_percentile)Ú_check_sample_weightÚ_num_samplesÚcheck_arrayÚcheck_consistent_lengthÚcheck_is_fittedÚvalidate_datac                       s–   e Zd ZU dZeh d£ƒgdgeeddgdœZee	d< ddddœd	d
„Z
edd�ddd„ƒZdd„ Zdd„ Zdd„ Z‡ fdd„Zd‡ fdd„	Z‡  ZS )ÚDummyClassifiera]  DummyClassifier makes predictions that ignore the input features.

    This classifier serves as a simple baseline to compare against other more
    complex classifiers.

    The specific behavior of the baseline is selected with the `strategy`
    parameter.

    All strategies make predictions that ignore the input feature values passed
    as the `X` argument to `fit` and `predict`. The predictions, however,
    typically depend on values observed in the `y` parameter passed to `fit`.

    Note that the "stratified" and "uniform" strategies lead to
    non-deterministic predictions that can be rendered deterministic by setting
    the `random_state` parameter if needed. The other strategies are naturally
    deterministic and, once fit, always return the same constant prediction
    for any value of `X`.

    Read more in the :ref:`User Guide <dummy_estimators>`.

    .. versionadded:: 0.13

    Parameters
    ----------
    strategy : {"most_frequent", "prior", "stratified", "uniform",             "constant"}, default="prior"
        Strategy to use to generate predictions.

        * "most_frequent": the `predict` method always returns the most
          frequent class label in the observed `y` argument passed to `fit`.
          The `predict_proba` method returns the matching one-hot encoded
          vector.
        * "prior": the `predict` method always returns the most frequent
          class label in the observed `y` argument passed to `fit` (like
          "most_frequent"). ``predict_proba`` always returns the empirical
          class distribution of `y` also known as the empirical class prior
          distribution.
        * "stratified": the `predict_proba` method randomly samples one-hot
          vectors from a multinomial distribution parametrized by the empirical
          class prior probabilities.
          The `predict` method returns the class label which got probability
          one in the one-hot vector of `predict_proba`.
          Each sampled row of both methods is therefore independent and
          identically distributed.
        * "uniform": generates predictions uniformly at random from the list
          of unique classes observed in `y`, i.e. each class has equal
          probability.
        * "constant": always predicts a constant label that is provided by
          the user. This is useful for metrics that evaluate a non-majority
          class.

          .. versionchanged:: 0.24
             The default value of `strategy` has changed to "prior" in version
             0.24.

    random_state : int, RandomState instance or None, default=None
        Controls the randomness to generate the predictions when
        ``strategy='stratified'`` or ``strategy='uniform'``.
        Pass an int for reproducible output across multiple function calls.
        See :term:`Glossary <random_state>`.

    constant : int or str or array-like of shape (n_outputs,), default=None
        The explicit constant as predicted by the "constant" strategy. This
        parameter is useful only for the "constant" strategy.

    Attributes
    ----------
    classes_ : ndarray of shape (n_classes,) or list of such arrays
        Unique class labels observed in `y`. For multi-output classification
        problems, this attribute is a list of arrays as each output has an
        independent set of possible classes.

    n_classes_ : int or list of int
        Number of label for each output.

    class_prior_ : ndarray of shape (n_classes,) or list of such arrays
        Frequency of each class observed in `y`. For multioutput classification
        problems, this is computed independently for each output.

    n_features_in_ : int
        Number of features seen during :term:`fit`.

    feature_names_in_ : ndarray of shape (`n_features_in_`,)
        Names of features seen during :term:`fit`. Defined only when `X` has
        feature names that are all strings.

    n_outputs_ : int
        Number of outputs.

    sparse_output_ : bool
        True if the array returned from predict is to be in sparse CSC format.
        Is automatically set to True if the input `y` is passed in sparse
        format.

    See Also
    --------
    DummyRegressor : Regressor that makes predictions using simple rules.

    Examples
    --------
    >>> import numpy as np
    >>> from sklearn.dummy import DummyClassifier
    >>> X = np.array([-1, 1, 1, 1])
    >>> y = np.array([0, 1, 1, 1])
    >>> dummy_clf = DummyClassifier(strategy="most_frequent")
    >>> dummy_clf.fit(X, y)
    DummyClassifier(strategy='most_frequent')
    >>> dummy_clf.predict(X)
    array([1, 1, 1, 1])
    >>> dummy_clf.score(X, y)
    0.75
    >   ÚconstantÚ
stratifiedÚpriorÚuniformÚmost_frequentÚrandom_stateú
array-likeN©Ústrategyr   r   Ú_parameter_constraintsr   c                C   s   || _ || _|| _d S ©Nr   )Úselfr    r   r   © r$   ú3E:\soccer\ml_env\Lib\site-packages\sklearn/dummy.pyÚ__init__œ   s    zDummyClassifier.__init__T©Zprefer_skip_nested_validationc                    sš  t | |dd� | j| _| jdkr>t |¡r>| ¡ }t dt¡ t |¡| _	| j	sdt
 |¡}t
 |¡}|jdkrzt
 |d¡}|jd | _t||ƒ |dur¢t||ƒ}| jdkrò| jdu rÀtd	ƒ‚n2t
 t
 | j¡d¡‰ ˆ jd
 | jkròtd| j ƒ‚t||ƒ\| _| _| _| jdk�rft| jƒD ]F‰t‡ ‡fdd„| jˆ D ƒƒ�sd | j| jˆ  ¡ ¡}t|ƒ‚�q| jdk�r–| jd
 | _| jd
 | _| jd
 | _| S )aÆ  Fit the baseline classifier.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            Training data.

        y : array-like of shape (n_samples,) or (n_samples, n_outputs)
            Target values.

        sample_weight : array-like of shape (n_samples,), default=None
            Sample weights.

        Returns
        -------
        self : object
            Returns the instance itself.
        T©Zskip_check_arrayr   zªA local copy of the target data has been converted to a numpy array. Predicting on sparse target data with the uniform strategy would not save memory and would be slower.r   ©éÿÿÿÿr   Nr   úMConstant target value has to be specified when the constant strategy is used.r   ú0Constant target value should have shape (%d, 1).c                 3   s   | ]}ˆ ˆ d  |kV  qdS )r   Nr$   ©Ú.0Úc©r   Úkr$   r%   Ú	<genexpr>é   ó    z&DummyClassifier.fit.<locals>.<genexpr>zrThe constant target value must be present in the training data. You provided constant={}. Possible values are: {}.)r   r    Ú	_strategyÚspÚissparseZtoarrayÚwarningsÚwarnÚUserWarningÚsparse_output_ÚnpZasarrayZ
atleast_1dÚndimÚreshapeÚshapeÚ
n_outputs_r   r   r   Ú
ValueErrorr   Úclasses_Ú
n_classes_Úclass_prior_ÚrangeÚanyÚformatÚtolist)r#   ÚXÚyÚsample_weightÚerr_msgr$   r0   r%   Úfit¡   s\    ù







ÿÿÿÿ ýÿzDummyClassifier.fitc                    s¾  t | ƒ t|ƒ‰t| jƒ‰| j‰| j‰| j‰ | j}| jdkrTˆg‰ˆg‰ˆ g‰ |g}| j	dkrx|  
|¡‰| jdkrxˆg‰| jrêd}| j	dv rœdd„ ˆ D ƒ‰n<| j	dkr¬ˆ }n,| j	dkrÀtdƒ‚n| j	d	krØd
d„ |D ƒ‰tˆˆ|| jƒ}nÐ| j	dv �rt ‡ ‡fdd„t| jƒD ƒˆdg¡}n†| j	dk�rNt ‡‡fdd„t| jƒD ƒ¡j}nV| j	dk�r†‡‡‡‡fdd„t| jƒD ƒ}t |¡j}n| j	d	k�r¤t | jˆdf¡}| jdk�rºt |¡}|S )a;  Perform classification on test vectors X.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            Test data.

        Returns
        -------
        y : array-like of shape (n_samples,) or (n_samples, n_outputs)
            Predicted target values for X.
        r   r   N)r   r   c                 S   s   g | ]}t  | ¡ g¡‘qS r$   )r;   ÚarrayÚargmax)r.   Úcpr$   r$   r%   Ú
<listcomp>#  r3   z+DummyClassifier.predict.<locals>.<listcomp>r   zCSparse target prediction is not supported with the uniform strategyr   c                 S   s   g | ]}t  |g¡‘qS r$   )r;   rM   r-   r$   r$   r%   rP   /  r3   c                    s    g | ]}ˆ| ˆ |   ¡  ‘qS r$   ©rN   ©r.   r1   )rC   rA   r$   r%   rP   5  s   ÿc                    s$   g | ]}ˆ | ˆ| j d d� ‘qS )r   ©ÚaxisrQ   rR   )rA   Úprobar$   r%   rP   >  s   ÿc                    s&   g | ]}ˆ | ˆj ˆ| ˆd � ‘qS )©Úsize)ÚrandintrR   )rA   rB   Ú	n_samplesÚrsr$   r%   rP   E  s   ÿ)r   r   r
   r   rB   rA   rC   r   r?   r4   Úpredict_probar:   r@   r   r;   ZtilerD   ZvstackÚTÚravel)r#   rH   r   Z
class_probrI   Úretr$   )rC   rA   rB   rY   rU   rZ   r%   Úpredictü   sh    







ÿ
þûþÿþ
zDummyClassifier.predictc                 C   s–  t | ƒ t|ƒ}t| jƒ}| j}| j}| j}| j}| jdkrT|g}|g}|g}|g}g }t	| jƒD �]}	| j
dkr¨||	  ¡ }
tj|||	 ftjd�}d|dd…|
f< nÊ| j
dkrÊt |df¡||	  }n¨| j
dkrö|jd||	 |d�}| tj¡}n|| j
d	k�r(tj|||	 ftjd�}|||	  }nJ| j
d
k�rrt ||	 ||	 k¡}
tj|||	 ftjd�}d|dd…|
f< | |¡ qb| jdk�r’|d }|S )aÊ  
        Return probability estimates for the test vectors X.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            Test data.

        Returns
        -------
        P : ndarray of shape (n_samples, n_classes) or list of such arrays
            Returns the probability of the sample for each class in
            the model, where classes are ordered arithmetically, for each
            output.
        r   r   ©Údtypeç      ð?Nr   r   rV   r   r   r   )r   r   r
   r   rB   rA   rC   r   r?   rD   r4   rN   r;   ÚzerosZfloat64ZonesZmultinomialZastypeÚwhereÚappend)r#   rH   rY   rZ   rB   rA   rC   r   ÚPr1   ÚindÚoutr$   r$   r%   r[   S  sD    




zDummyClassifier.predict_probac                 C   s0   |   |¡}| jdkrt |¡S dd„ |D ƒS dS )aÚ  
        Return log probability estimates for the test vectors X.

        Parameters
        ----------
        X : {array-like, object with finite length or shape}
            Training data.

        Returns
        -------
        P : ndarray of shape (n_samples, n_classes) or list of such arrays
            Returns the log probability of the sample for each class in
            the model, where classes are ordered arithmetically for each
            output.
        r   c                 S   s   g | ]}t  |¡‘qS r$   )r;   Úlog)r.   Úpr$   r$   r%   rP   ¦  r3   z5DummyClassifier.predict_log_proba.<locals>.<listcomp>N)r[   r?   r;   ri   )r#   rH   rU   r$   r$   r%   Úpredict_log_proba’  s    


z!DummyClassifier.predict_log_probac                    s$   t ƒ  ¡ }d|j_d|j_d|_|S ©NT)ÚsuperÚ__sklearn_tags__Ú
input_tagsÚsparseZclassifier_tagsÚ
poor_scoreÚno_validation©r#   Útags©Ú	__class__r$   r%   rn   ¨  s
    
z DummyClassifier.__sklearn_tags__c                    s,   |du rt jt|ƒdfd�}tƒ  |||¡S )ak  Return the mean accuracy on the given test data and labels.

        In multi-label classification, this is the subset accuracy
        which is a harsh metric since you require for each sample that
        each label set be correctly predicted.

        Parameters
        ----------
        X : None or array-like of shape (n_samples, n_features)
            Test samples. Passing None as test samples gives the same result
            as passing real test samples, since DummyClassifier
            operates independently of the sampled observations.

        y : array-like of shape (n_samples,) or (n_samples, n_outputs)
            True labels for X.

        sample_weight : array-like of shape (n_samples,), default=None
            Sample weights.

        Returns
        -------
        score : float
            Mean accuracy of self.predict(X) w.r.t. y.
        Nr   ©r>   ©r;   rc   Úlenrm   Úscore©r#   rH   rI   rJ   ru   r$   r%   rz   ¯  s    zDummyClassifier.score)N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Ústrr!   ÚdictÚ__annotations__r&   r	   rL   r_   r[   rk   rn   rz   Ú__classcell__r$   r$   ru   r%   r   "   s   
s
ÿ
ûZW?r   c                       s    e Zd ZU dZeh d£ƒgeedddd�dgeedddd�d	dgd
œZee	d< ddddœdd„Z
edd�ddd„ƒZddd„Z‡ fdd„Zd‡ fdd„	Z‡  ZS )ÚDummyRegressoraŸ  Regressor that makes predictions using simple rules.

    This regressor is useful as a simple baseline to compare with other
    (real) regressors. Do not use it for real problems.

    Read more in the :ref:`User Guide <dummy_estimators>`.

    .. versionadded:: 0.13

    Parameters
    ----------
    strategy : {"mean", "median", "quantile", "constant"}, default="mean"
        Strategy to use to generate predictions.

        * "mean": always predicts the mean of the training set
        * "median": always predicts the median of the training set
        * "quantile": always predicts a specified quantile of the training set,
          provided with the quantile parameter.
        * "constant": always predicts a constant value that is provided by
          the user.

    constant : int or float or array-like of shape (n_outputs,), default=None
        The explicit constant as predicted by the "constant" strategy. This
        parameter is useful only for the "constant" strategy.

    quantile : float in [0.0, 1.0], default=None
        The quantile to predict using the "quantile" strategy. A quantile of
        0.5 corresponds to the median, while 0.0 to the minimum and 1.0 to the
        maximum.

    Attributes
    ----------
    constant_ : ndarray of shape (1, n_outputs)
        Mean or median or quantile of the training targets or constant value
        given by the user.

    n_features_in_ : int
        Number of features seen during :term:`fit`.

    feature_names_in_ : ndarray of shape (`n_features_in_`,)
        Names of features seen during :term:`fit`. Defined only when `X` has
        feature names that are all strings.

    n_outputs_ : int
        Number of outputs.

    See Also
    --------
    DummyClassifier: Classifier that makes predictions using simple rules.

    Examples
    --------
    >>> import numpy as np
    >>> from sklearn.dummy import DummyRegressor
    >>> X = np.array([1.0, 2.0, 3.0, 4.0])
    >>> y = np.array([2.0, 3.0, 5.0, 10.0])
    >>> dummy_regr = DummyRegressor(strategy="mean")
    >>> dummy_regr.fit(X, y)
    DummyRegressor()
    >>> dummy_regr.predict(X)
    array([5., 5., 5., 5.])
    >>> dummy_regr.score(X, y)
    0.0
    >   ÚquantileÚmeanr   Úmediang        rb   Zboth)ÚclosedNZneitherr   )r    r…   r   r!   r†   ©r    r   r…   c                C   s   || _ || _|| _d S r"   r‰   )r#   r    r   r…   r$   r$   r%   r&     s    zDummyRegressor.__init__Tr'   c                    sº  t | |dd� tˆddd�‰tˆƒdkr0tdƒ‚ˆjdkrFt ˆd	¡‰ˆjd | _t	|ˆˆƒ ˆd
urpt
ˆ|ƒ‰| jdkr�tjˆdˆd�| _�n| jdkrÒˆd
u r´tjˆdd�| _n‡‡fdd„t| jƒD ƒ| _nÔ| jdk�r8| jd
u rðtdƒ‚| jd ‰ ˆd
u �rtjˆdˆ d�| _n‡ ‡‡fdd„t| jƒD ƒ| _nn| jdk�r¦| jd
u �rXtdƒ‚t| jg d¢ddd�| _| jdk�r¦| jjd ˆjd k�r¦tdˆjd  ƒ‚t | jd¡| _| S )aº  Fit the baseline regressor.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            Training data.

        y : array-like of shape (n_samples,) or (n_samples, n_outputs)
            Target values.

        sample_weight : array-like of shape (n_samples,), default=None
            Sample weights.

        Returns
        -------
        self : object
            Fitted estimator.
        Tr(   FrI   )Ú	ensure_2dZ
input_namer   zy must not be empty.r   r)   Nr†   )rT   Úweightsr‡   rS   c                    s&   g | ]}t ˆd d …|f ˆ dd�‘qS )Ng      I@©Ú
percentiler   rR   )rJ   rI   r$   r%   rP   H  s   ÿz&DummyRegressor.fit.<locals>.<listcomp>r…   z^When using `strategy='quantile', you have to specify the desired quantile in the range [0, 1].g      Y@)rT   Úqc                    s&   g | ]}t ˆd d …|f ˆˆ d�‘qS )NrŒ   r   rR   ©r�   rJ   rI   r$   r%   rP   W  s   ÿr   r+   )ZcsrZcscZcoo)Zaccept_sparserŠ   Zensure_min_samplesr,   )r   r*   )r   r   ry   r@   r<   r;   r=   r>   r?   r   r   r    ZaverageÚ	constant_r‡   rD   r…   r�   r   Ú	TypeErrorr{   r$   r�   r%   rL     s\    



þ

ÿ

þ
ÿü$ÿzDummyRegressor.fitFc                 C   sp   t | ƒ t|ƒ}tj|| jf| jt | j¡jd�}t || jf¡}| jdkr`t 	|¡}t 	|¡}|rl||fS |S )a’  Perform classification on test vectors X.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            Test data.

        return_std : bool, default=False
            Whether to return the standard deviation of posterior prediction.
            All zeros in this case.

            .. versionadded:: 0.20

        Returns
        -------
        y : array-like of shape (n_samples,) or (n_samples, n_outputs)
            Predicted target values for X.

        y_std : array-like of shape (n_samples,) or (n_samples, n_outputs)
            Standard deviation of predictive distribution of query points.
        r`   r   )
r   r   r;   Úfullr?   r�   rM   ra   rc   r]   )r#   rH   Z
return_stdrY   rI   Zy_stdr$   r$   r%   r_   r  s    ý


zDummyRegressor.predictc                    s$   t ƒ  ¡ }d|j_d|j_d|_|S rl   )rm   rn   ro   rp   Zregressor_tagsrq   rr   rs   ru   r$   r%   rn   ˜  s
    
zDummyRegressor.__sklearn_tags__c                    s,   |du rt jt|ƒdfd�}tƒ  |||¡S )aŽ  Return the coefficient of determination R^2 of the prediction.

        The coefficient R^2 is defined as `(1 - u/v)`, where `u` is the
        residual sum of squares `((y_true - y_pred) ** 2).sum()` and `v` is the
        total sum of squares `((y_true - y_true.mean()) ** 2).sum()`. The best
        possible score is 1.0 and it can be negative (because the model can be
        arbitrarily worse). A constant model that always predicts the expected
        value of y, disregarding the input features, would get a R^2 score of
        0.0.

        Parameters
        ----------
        X : None or array-like of shape (n_samples, n_features)
            Test samples. Passing None as test samples gives the same result
            as passing real test samples, since `DummyRegressor`
            operates independently of the sampled observations.

        y : array-like of shape (n_samples,) or (n_samples, n_outputs)
            True values for X.

        sample_weight : array-like of shape (n_samples,), default=None
            Sample weights.

        Returns
        -------
        score : float
            R^2 of `self.predict(X)` w.r.t. y.
        Nr   rw   rx   r{   ru   r$   r%   rz   Ÿ  s    zDummyRegressor.score)N)F)N)r|   r}   r~   r   r   r   r   r!   r�   r‚   r&   r	   rL   r_   rn   rz   rƒ   r$   r$   ru   r%   r„   Í  s   
Býý
S
&r„   )$r   r7   Únumbersr   r   Únumpyr;   Zscipy.sparserp   r5   Úbaser   r   r   r   r	   Úutilsr
   Zutils._param_validationr   r   Zutils.multiclassr   Zutils.randomr   Zutils.statsr   Zutils.validationr   r   r   r   r   r   r   r„   r$   r$   r$   r%   Ú<module>   s     
   .