CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
multiclass.cpython-310.pyc270 linesDownload Raw Back to __pycache__
1o

2�Yi�Q�@s�dZddlZddlmZddlmZddlZddlm	Z	ddl3mZddlm
Z
d	d4lmZmZd	dlmZmZd#dd
�Zd#dd�Zeeed�Zdd�Zdd�Zdd�Zdd�Zd$dd�Zd#dd�Zd#dd �Zd!d"�ZdS)%zAUtilities to handle multiclass/multioutput target in classifiers.�N)�Sequence)�chain)�issparse�)�
get_namespace)�VisibleDeprecationWarning�)�
attach_unique�
cached_unique)�_assert_all_finite�check_arraycCs8t||d�\}}t|d�s|rt|�|�|d�St|�S)N��xp�	__array__)r�hasattrr5�asarray�set��yr�is_array_api_compliant�r��E:\DocsHouse\542 percep lab latest\PerceptionLab\PerceptionLab_Portable\python_embed\Lib\site-packages\sklearn/utils/multiclass.py�_unique_multiclasssrcCs.t||d�\}}|�t|dgd�d�jd�S)Nr
r�ZcsrZcscZcoo)�6input_name�
accept_sparser)rZaranger�shape)rr�_rrr�_unique_indicators�r)�binary�7multiclass�multilabel-indicatorcs2t|ddi�}t|�\�}t|�dkrtd��tdd�|D��}|ddhkr)dh}t|�d	kr5td8|��|��}|dkrNttdd�|D���d	krNtd
��t�|d���s^tdt|���|rr��	��fdd�|D��}��9|�Stt���fdd�|D���}ttdd�|D���d	kr�td����
t|��S)a�Extract an ordered array of unique labels.10 11    We don't allow:12        - mix of multilabel and multiclass (single label) targets13        - mix of label indicator matrix and anything else,14          because there are no explicit labels)15        - mix of label indicator matrices of different sizes16        - mix of string and integer labels17 18    At the moment, we also don't allow "multiclass-multioutput" input type.19 20    Parameters21    ----------22    *ys : array-likes23        Label values.24 25    Returns26    -------27    out : ndarray of shape (n_unique_labels,)28        An ordered array of unique labels.29 30    Examples31    --------32    >>> from sklearn.utils.multiclass import unique_labels33    >>> unique_labels([3, 5, 5, 5, 7, 7])34    array([3, 5, 7])35    >>> unique_labels([1, 2, 3, 4], [2, 2, 3, 4])36    array([1, 2, 3, 4])37    >>> unique_labels([1, 2, 10], [5, 11])38    array([ 1,  2,  5, 10, 11])39    Zreturn_tupleTrzNo argument has been passed.css�|]}t|�VqdS�N)�type_of_target)�.0�xrrr�	<genexpr>Os�z unique_labels.<locals>.<genexpr>rr rz'Mix type of y not allowed, got types %sr!css&�|]}t|gd�d�jdVqdS)r)rrN)rr�r$rrrrr&\s�40�zCMulti-label binary indicator input with different numbers of labelsNzUnknown label type: %scsg|]}�|�d��qS)r
rr'�Z_unique_labelsrrr�41<listcomp>msz!unique_labels.<locals>.<listcomp>c3s&�|]}dd��|�d�D�VqdS)css�|]}|VqdSr"r)r$�irrrr&qs�z*unique_labels.<locals>.<genexpr>.<genexpr>r
Nrr'r(rrr&qs�$css�|]}t|t�VqdSr")�42isinstance�str)r$�labelrrrr&ts�z,Mix of label input types (string and number))r	r�len�43ValueErrorr�pop�_FN_UNIQUE_LABELS�get�repr�concat�
unique_valuesr�
from_iterabler�sorted)ZysrZys_typesZ44label_typeZ	unique_ysZ	ys_labelsrr(r�
unique_labels)sB ����45�r8c	Cs@t|�\}}|�|jd�ot|�|�|�||j�|j�|k��S)N�
real floating)r�isdtype�dtype�bool�all�astypeZint64rrrr�_is_integral_floatzs �r?cCs�t|�\}}t|d�st|t�s|ritddddddd�}t���At�dt�zt	|fddi|��}Wn&tt46fyY}zt|��d	�rE�t	|fdt
i|��}WYd}~nd}~wwWd�n1sdwYt|d47�rz|jdkrz|jddks|dSt|�r�|jd
vr�|��}|�|j�}t|j�dkp�|jdks�|jdko�d|vo�|jjdvp�t|�St||d�}|jddko�|�|jd�p�t|�S)a}Check if ``y`` is in a multilabel format.48 49    Parameters50    ----------51    y : ndarray of shape (n_samples,)52        Target values.53 54    Returns55    -------56    out : bool57        Return ``True``, if ``y`` is in a multilabel format, else ``False``.58 59    Examples60    --------61    >>> import numpy as np62    >>> from sklearn.utils.multiclass import is_multilabel63    >>> is_multilabel([0, 1, 0, 1])64    False65    >>> is_multilabel([[1], [0, 2], []])66    False67    >>> is_multilabel(np.array([[1, 0], [0, 0]]))68    True69    >>> is_multilabel(np.array([[1], [0], [0]]))70    False71    >>> is_multilabel(np.array([[1, 0, 0]]))72    True73    rTFr�rZallow_ndZensure_all_finiteZ	ensure_2dZensure_min_samplesZensure_min_features�errorr;N�Complex data not supportedrrr)ZdokZlilZbiur
�)r<zsigned integerzunsigned integer)rrr+r�dict�warnings�catch_warnings�simplefilterrrr/r,�74startswith�object�ndimrr�formatZtocsrr5�datar.�sizer;�kindr?r75r:)rrr�check_y_kwargs�e�labelsrrr�
is_multilabel�sJ�76 ����"77��rRcCs(t|dd�}|dvrtd|�d���dS)aAEnsure that target y is of a non-regression type.78 79    Only the following target types (as defined in type_of_target) are allowed:80        'binary', 'multiclass', 'multiclass-multioutput',81        'multilabel-indicator', 'multilabel-sequences'82 83    Parameters84    ----------85    y : array-like86        Target values.87    r�r)rr zmulticlass-multioutputr!zmultilabel-sequenceszUnknown label type: zy. Maybe you are trying to fit a classifier, which expects discrete classes on a regression target with continuous values.N)r#r/)rZy_typerrr�check_classification_targets�s88��rT�Fcst��\}}���fdd�}t�t�st��st�d�o!t�t�p#|}|s,td����jjdv}|r8td��t	��r>dSt89ddd	d	d90d91d�}t���Et�
dt�t��s�zt�fd
di|���Wn&ttfy�}	zt|	��d�ru�t�fd
ti|���WYd}	~	nd}	~	wwWd�n1s�wYz/t��r��d92gdd�fn�d93}94t|95t�r�td��t|96d�s�t|97t�r�t|98t�s�td��Wn	ty�Ynw�jdvr�|�St�j�s�jdkr�dS|�St��s��jtkr�t�jd99t�s�|�S�jdk�r�jddk�rd}nd}|��jd��r8t���r �jn�}|�||�|t�k��r8t|�d�d|St|100��r@|101j}102t ��}
�jd103dk�ro�jd104|
jd105k�ret!d�jd106�k�ronntj"dt#dd�|
jd107dk�s��jdk�r�t$|108�dk�r�d|SdS) a�109Determine the type of data indicated by the target.110 111    Note that this type is the most specific type that can be inferred.112    For example:113 114    * ``binary`` is more specific but compatible with ``multiclass``.115    * ``multiclass`` of integers is more specific but compatible with ``continuous``.116    * ``multilabel-indicator`` is more specific but compatible with117      ``multiclass-multioutput``.118 119    Parameters120    ----------121    y : {array-like, sparse matrix}122        Target values. If a sparse matrix, `y` is expected to be a123        CSR/CSC matrix.124 125    input_name : str, default=""126        The data name used to construct the error message.127 128        .. versionadded:: 1.1.0129 130    raise_unknown : bool, default=False131        If `True`, raise an error when the type of target returned by132        :func:`~sklearn.utils.multiclass.type_of_target` is `"unknown"`.133 134        .. versionadded:: 1.6135 136    Returns137    -------138    target_type : str139        One of:140 141        * 'continuous': `y` is an array-like of floats that are not all142          integers, and is 1d or a column vector.143        * 'continuous-multioutput': `y` is a 2d array of floats that are144          not all integers, and both dimensions are of size > 1.145        * 'binary': `y` contains <= 2 discrete values and is 1d or a column146          vector.147        * 'multiclass': `y` contains more than two discrete values, is not a148          sequence of sequences, and is 1d or a column vector.149        * 'multiclass-multioutput': `y` is a 2d array that contains more150          than two discrete values, is not a sequence of sequences, and both151          dimensions are of size > 1.152        * 'multilabel-indicator': `y` is a label indicator matrix, an array153          of two dimensions with at least two columns, and at most 2 unique154          values.155        * 'unknown': `y` is array-like but none of the above, such as a 3d156          array, sequence of sequences, or an array of non-sequence objects.157 158    Examples159    --------160    >>> from sklearn.utils.multiclass import type_of_target161    >>> import numpy as np162    >>> type_of_target([0.1, 0.6])163    'continuous'164    >>> type_of_target([1, -1, -1, 1])165    'binary'166    >>> type_of_target(['a', 'b', 'a'])167    'binary'168    >>> type_of_target([1.0, 2.0])169    'binary'170    >>> type_of_target([1, 0, 2])171    'multiclass'172    >>> type_of_target([1.0, 0.0, 3.0])173    'multiclass'174    >>> type_of_target(['a', 'b', 'c'])175    'multiclass'176    >>> type_of_target(np.array([[1, 2], [3, 1]]))177    'multiclass-multioutput'178    >>> type_of_target([[1, 2]])179    'multilabel-indicator'180    >>> type_of_target(np.array([[1.5, 2.0], [3.0, 1.6]]))181    'continuous-multioutput'182    >>> type_of_target(np.array([[0, 1], [1, 1]]))183    'multilabel-indicator'184    cs(�r�r�nd}td|�d�����dS)zdDepending on the value of raise_unknown, either raise an error or return185        'unknown'.186        rLzUnknown label type for z: �unknown)r/)�input�r�
raise_unknownrrr�_raise_or_return3sz(type_of_target.<locals>._raise_or_returnrz:Expected array-like (array or non-string sequence), got %r)ZSparseSeriesZSparseArrayz1y cannot be class 'SparseSeries' or 'SparseArray'r!TFrr@rAr;NrBzkSupport for labels represented as bytes is not supported. Convert the labels to a string or integer format.z�You appear to be using a legacy multi-label data representation. Sequence of sequences are no longer supported; use a binary array or sparse matrix instead - the MultiLabelBinarizer transformer can convert to this format.)rrrrrz-multioutputrUr9rSZ187continuous�g�?z�The number of unique classes is greater than 50% of the number of samples. `y` could represent a regression problem, not a classification problem.)�188stacklevelr )%rr+rrrr,r/�	__class__�__name__rRrDrErFrGrrrHrI�bytes�	TypeError�189IndexErrorrJ�minrr;Zflatr:rL�anyr>�intrr190�round�warn�UserWarningr.)rrrYrrrZ�validZ
sparse_pandasrOrPZfirst_row_or_val�suffixrL�classesrrXrr#�s�M191����192	 ����
"193�������194195196"197F�*r#cCsrt|dd�dur|durtd��|dur7t|dd�dur0t�|jt|��s.td||jf��dSt|�|_dSdS)a"Private helper function for factorizing common classes param logic.198 199    Estimators that implement the ``partial_fit`` API need to be provided with200    the list of possible classes at the first call to partial_fit.201 202    Subsequent calls to partial_fit should check that ``classes`` is still203    consistent with a previous value of ``clf.classes_`` when provided.204 205    This function returns True if it detects that this was the first call to206    ``partial_fit`` on ``clf``. In that case the ``classes_`` attribute is also207    set on ``clf``.208 209    �classes_Nz8classes must be passed on the first call to partial_fit.zD`classes=%r` is not the same as on last call to partial_fit, was: %rTF)�getattrr/�npZarray_equalrkr8)Zclfrjrrr�_check_partial_fit_first_call�s��210�rncCs�g}g}g}|j\}}|durt�|�}t|�r�|��}t�|j�}t|�D]�}|j|j||j|d�}	|durJ||	}211t�	|�t�	|212�}nd}213|jd||}tj214|j|j||j|d�dd�\}}
tj|
|215d�}d|vr�||dk|7<d|vr�|||jdkr�t�
|dd�}t�
|d|�}|�|�|�|jd�|�||�	��q&n3t|�D].}tj216|dd�|fdd�\}}
|�|�|�|jd�tj|
|d�}|�||�	��q�|||fS)azCompute class priors from multioutput-multiclass target data.217 218    Parameters219    ----------220    y : {array-like, sparse matrix} of size (n_samples, n_outputs)221        The labels for each example.222 223    sample_weight : array-like of shape (n_samples,), default=None224        Sample weights.225 226    Returns227    -------228    classes : list of size n_outputs of ndarray of size (n_classes,)229        List of classes for each column.230 231    n_classes : list of int of size n_outputs232        Number of classes in each column.233 234    class_prior : list of size n_outputs of ndarray of size (n_classes,)235        Class distribution of each column.236    NrrT)Zreturn_inverse)�weights)rrmrrZtocsc�diffZindptr�range�indices�sum�uniquerLZbincount�insert�append)rZ
sample_weightrj�	n_classesZclass_prior�	n_samplesZ	n_outputsZy_nnz�kZcol_nonzeroZnz_samp_weightZzeros_samp_weight_sumZ	classes_kZy_kZ
class_prior_krrr�class_distribution�sH237238239�240�241242rzc243Cs|jd}t�||f�}t�||f�}d}t|�D]X}t|d|�D]N}|dd�|f|dd�|f8<|dd�|f|dd�|f7<||dd�|fdk|fd7<||dd�|fdk|fd7<|d7}q"q|dt�|�d}	||	S)ayCompute a continuous, tie-breaking OvR decision function from OvO.244 245    It is important to include a continuous value, not only votes,246    to make computing AUC or calibration meaningful.247 248    Parameters249    ----------250    predictions : array-like of shape (n_samples, n_classifiers)251        Predicted classes for each binary classifier.252 253    confidences : array-like of shape (n_samples, n_classifiers)254        Decision functions or predicted probabilities for positive class255        for each binary classifier.256 257    n_classes : int258        Number of classes. n_classifiers must be259        ``n_classes * (n_classes - 1 ) / 2``.260    rrNrC)rrmZzerosrq�abs)261ZpredictionsZconfidencesrwrxZvotesZsum_of_confidencesryr*�jZtransformed_confidencesrrr�_ovr_decision_functions 262$$$$263��r}r")rUF)�__doc__rE�collections.abcr�	itertoolsr�numpyrmZscipy.sparserZutils._array_apirZutils.fixesr�_uniquer	r264Z265validationrrrrr1r8r?rRrTr#rnrzr}rrrr�<module>s2266267�QH268269N270#J
Aluode/PerceptionLabPortable · CoolFace