`L iOdZddlZddlmZddlmZddlZddlm Z ddl m Z ddl m Z d d lmZmZd d lmZmZdd Zdd ZeeedZdZdZdZdZddZddZddZdZy)zAUtilities to handle multiclass/multioutput target in classifiers.N)Sequence)chain)issparse) get_namespace)VisibleDeprecationWarning) attach_unique cached_unique)_assert_all_finite check_arrayct||\}}t|ds|rt|j||St |S)Nxp __array__)rhasattrr asarraysetyris_array_api_compliants ^/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sklearn/utils/multiclass.py_unique_multiclassrs?!.qR!8Bq+"8RZZ]r221v czt||\}}|jt|dgdjdS)Nrrcsrcsccoo) input_name accept_sparser )raranger shape)rr_s r_unique_indicatorr%s< ! #EB 99A#5JKQQRST r)binary multiclassmultilabel-indicatorc t|ddi}t|\}t|dk(r tdt d|D}|ddhk(rdh}t|dkDrtd |z|j }|d k(r)tt d |DdkDr td t j|d stdt|z|r8j|Dcgc] }|c}}j|St tjfd|D}tt d|DdkDr tdjt|Scc}w)aExtract an ordered array of unique labels. We don't allow: - mix of multilabel and multiclass (single label) targets - mix of label indicator matrix and anything else, because there are no explicit labels) - mix of label indicator matrices of different sizes - mix of string and integer labels At the moment, we also don't allow "multiclass-multioutput" input type. Parameters ---------- *ys : array-likes Label values. Returns ------- out : ndarray of shape (n_unique_labels,) An ordered array of unique labels. Examples -------- >>> from sklearn.utils.multiclass import unique_labels >>> unique_labels([3, 5, 5, 5, 7, 7]) array([3, 5, 7]) >>> unique_labels([1, 2, 3, 4], [2, 2, 3, 4]) array([1, 2, 3, 4]) >>> unique_labels([1, 2, 10], [5, 11]) array([ 1, 2, 5, 10, 11]) return_tupleTrzNo argument has been passed.c32K|]}t|ywN)type_of_target).0xs r z unique_labels..Os1>!$1sr&r'r z'Mix type of y not allowed, got types %sr(c3TK|] }t|gdjd"yw)r)r!r N)r r#)r.rs rr0z unique_labels..\s(QR A-BCII!Ls&(zCMulti-label binary indicator input with different numbers of labelsNzUnknown label type: %src3@K|]}d|Dyw)c3 K|]}|ywr,)r.is rr0z*unique_labels...qsA1QAs rNr4)r.r_unique_labelsrs rr0z unique_labels..qs!NaAqR(@AANsc3<K|]}t|tywr,) isinstancestr)r.labels rr0z unique_labels..ts=%z%%=sz,Mix of label input types (string and number))r rlen ValueErrorrpop_FN_UNIQUE_LABELSgetreprconcat unique_valuesr from_iterablersorted) ysrys_types label_typer unique_ys ys_labelsr6rs @@r unique_labelsrJ)s@  . .B!.!3B 2w!|7881b11HHl++ > 8}qBXMNNJ ,,  VX    Q  '**:tBGHH ::fY' ((Es(E<c t|\}}|j|jdxrQt|j |j |j ||j |j|k(S)N real floating)risdtypedtypeboolallastypeint64rs r_is_integral_floatrSzsd!.q!1B ::agg / D ryy"))Arxx0177;q@A5rct|\}}t|dst|ts|rWt dddddd}t j 5t jdt t|fddi|}dddt|d r!|jd k(r|jd d kDsyt!|r|j"d vr|j%}|j'|j(}t+|j(dk(xsM|j,d k(xs|j,d k(xrd|vxr%|j.j0dvxs t3|St5||}|jddkxr)|j7|j.dxs t3|S#ttf$r8}t|jd rt|fdti|}Yd}~md}~wwxYw#1swYwxYw)a}Check if ``y`` is in a multilabel format. Parameters ---------- y : ndarray of shape (n_samples,) Target values. Returns ------- out : bool Return ``True``, if ``y`` is in a multilabel format, else ``False``. Examples -------- >>> import numpy as np >>> from sklearn.utils.multiclass import is_multilabel >>> is_multilabel([0, 1, 0, 1]) False >>> is_multilabel([[1], [0, 2], []]) False >>> is_multilabel(np.array([[1, 0], [0, 0]])) True >>> is_multilabel(np.array([[1], [0], [0]])) False >>> is_multilabel(np.array([[1, 0, 0]])) True rTFrr!allow_ndensure_all_finite ensure_2densure_min_samplesensure_min_featureserrorrNNComplex data not supportedr#rr )doklilbiur)rOzsigned integerzunsigned integer)rrr8rdictwarningscatch_warnings simplefilterrr r<r9 startswithobjectndimr#rformattocsrrBdatar;sizerNkindrSr rM)rrrcheck_y_kwargselabelss r is_multilabelrps8"/q!1Bq+*Q"9=S# !   $ $ & C  ! !'+D E C@@@ C Aw AFFaKAGGAJN{ 88~ % A!!!&&)166{a [[A  G6;;!#3"F!v+ F&D*B  C C Cs0G1.F''G.6-G)#G1)G..G11G;cDt|d}|dvrtd|dy)aAEnsure that target y is of a non-regression type. Only the following target types (as defined in type_of_target) are allowed: 'binary', 'multiclass', 'multiclass-multioutput', 'multilabel-indicator', 'multilabel-sequences' Parameters ---------- y : array-like Target values. rr )r&r'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<)ry_types rcheck_classification_targetsrtsBA# .F "6(+8 8  rc`t\}}fd}ttxstxs t dxrtt  xs|}|st dzjjdv}|r t dtrytddddd d }tj5tjd tts tfd d i|d d d  tr d gd d fnd } t| t$r t'dt | ds+t| trt| t s t dj*dvr|St-j.sj*dk(ry|Sts7j0t"k(r$tj2d t s|Sj*dk(rj.ddkDrd} nd} |j5j0drStr j6n} |j9| |j;| t<k7rt?| d| zSt r | j6} tA} j.d dkDr]j.d | j.d cxkDrtCdj.d zkDrnntjDdtFd| j.d dkDsj*dk(rtI| dkDrd| zSy#tt f$r8} t | j!drtfd t"i|Yd } ~ d } ~ wwxYw#1swYxYw#t($rYAwxYw)a Determine the type of data indicated by the target. Note that this type is the most specific type that can be inferred. For example: * ``binary`` is more specific but compatible with ``multiclass``. * ``multiclass`` of integers is more specific but compatible with ``continuous``. * ``multilabel-indicator`` is more specific but compatible with ``multiclass-multioutput``. Parameters ---------- y : {array-like, sparse matrix} Target values. If a sparse matrix, `y` is expected to be a CSR/CSC matrix. input_name : str, default="" The data name used to construct the error message. .. versionadded:: 1.1.0 raise_unknown : bool, default=False If `True`, raise an error when the type of target returned by :func:`~sklearn.utils.multiclass.type_of_target` is `"unknown"`. .. versionadded:: 1.6 Returns ------- target_type : str One of: * 'continuous': `y` is an array-like of floats that are not all integers, and is 1d or a column vector. * 'continuous-multioutput': `y` is a 2d array of floats that are not all integers, and both dimensions are of size > 1. * 'binary': `y` contains <= 2 discrete values and is 1d or a column vector. * 'multiclass': `y` contains more than two discrete values, is not a sequence of sequences, and is 1d or a column vector. * 'multiclass-multioutput': `y` is a 2d array that contains more than two discrete values, is not a sequence of sequences, and both dimensions are of size > 1. * 'multilabel-indicator': `y` is a label indicator matrix, an array of two dimensions with at least two columns, and at most 2 unique values. * 'unknown': `y` is array-like but none of the above, such as a 3d array, sequence of sequences, or an array of non-sequence objects. Examples -------- >>> from sklearn.utils.multiclass import type_of_target >>> import numpy as np >>> type_of_target([0.1, 0.6]) 'continuous' >>> type_of_target([1, -1, -1, 1]) 'binary' >>> type_of_target(['a', 'b', 'a']) 'binary' >>> type_of_target([1.0, 2.0]) 'binary' >>> type_of_target([1, 0, 2]) 'multiclass' >>> type_of_target([1.0, 0.0, 3.0]) 'multiclass' >>> type_of_target(['a', 'b', 'c']) 'multiclass' >>> type_of_target(np.array([[1, 2], [3, 1]])) 'multiclass-multioutput' >>> type_of_target([[1, 2]]) 'multilabel-indicator' >>> type_of_target(np.array([[1.5, 2.0], [3.0, 1.6]])) 'continuous-multioutput' >>> type_of_target(np.array([[0, 1], [1, 1]])) 'multilabel-indicator' c8rrnd}td|dy)zdDepending on the value of raise_unknown, either raise an error or return 'unknown'. rjzUnknown label type for z: unknown)r<)inputr raise_unknownrs r_raise_or_returnz(type_of_target.._raise_or_return3s- ",J&E6ugRuEF Frrz:Expected array-like (array or non-string sequence), got %r) SparseSeries SparseArrayz1y cannot be class 'SparseSeries' or 'SparseArray'r(TFrrUr[rNNr\zkSupport for labels represented as bytes is not supported. Convert the labels to a string or integer format.zYou 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.)r rr r&rz -multioutputrLrr continuousg?zThe number of unique classes is greater than 50% of the number of samples. `y` could represent a regression problem, not a classification problem.) stacklevelr')%rr8rrrr9r< __class____name__rprarbrcrdrr rerfbytes TypeError IndexErrorrgminr#rNflatrMrjanyrQintr r roundwarn UserWarningr;)rr ryrrrzvalid sparse_pandasrmrnfirst_row_or_valsuffixrjclassess``` rr-r-s|Z"/q!1B Ax JHQK J71k3J #1c" "    H1 L  KK((,KKMLMMQ%  N  " Cg'@A{ C@@@ C (0 1aS!V91 & .<  (+6+X6/5;  vvV!! qww< 66Q;!! A;177f,Zq 35O!! vv{qwwqzA~ zz!''?+!!qvv! 66$"))D#.. / t ;&( ( !+00AGwwqzB1771: a(8R5qwwqzAQ;RR  &   }}Q!! 4D0E0If$$].z: Cq6$$%AB BB>B  C C CD    sC&N(M ?A,N N-N N NNN N-,N-ct|dd | td|ct|ddEtj|jt |std|d|jyt ||_yy)a"Private helper function for factorizing common classes param logic. Estimators that implement the ``partial_fit`` API need to be provided with the list of possible classes at the first call to partial_fit. Subsequent calls to partial_fit should check that ``classes`` is still consistent with a previous value of ``clf.classes_`` when provided. This function returns True if it detects that this was the first call to ``partial_fit`` on ``clf``. In that case the ``classes_`` attribute is also set on ``clf``. classes_Nz8classes must be passed on the first call to partial_fit.z `classes=z7` is not the same as on last call to partial_fit, was: TF)getattrr<np array_equalrrJ)clfrs r_check_partial_fit_first_callrssJ%-'/STT   3 D ) 5>>#,, g0FG 18#,,H  )1CL rc,g}g}g}|j\}}|tj|}t|r|j }tj |j }t|D]}|j|j ||j |dz} |1|| } tj|tj| z } nd} |jd||z } tj|j|j ||j |dzd\} } tj| | }d| vr|| dk(xx| z cc<d| vrC|||jdkr.tj| dd} tj|d| }|j| |j| jd|j||jz nt|D]}tj|dd|fd\} } |j| |j| jdtj| |}|j||jz |||fS)azCompute class priors from multioutput-multiclass target data. Parameters ---------- y : {array-like, sparse matrix} of size (n_samples, n_outputs) The labels for each example. sample_weight : array-like of shape (n_samples,), default=None Sample weights. Returns ------- classes : list of size n_outputs of ndarray of size (n_classes,) List of classes for each column. n_classes : list of int of size n_outputs Number of classes in each column. class_prior : list of size n_outputs of ndarray of size (n_classes,) Class distribution of each column. Nr rT)return_inverse)weights)r#rrrtocscdiffindptrrangeindicessumuniquerjbincountinsertappend)r sample_weightr n_classes class_prior n_samples n_outputsy_nnzk col_nonzeronz_samp_weightzeros_samp_weight_sum classes_ky_k class_prior_ks rclass_distributionrsU,GIK77Iy  =1 { GGI!y! DA))AHHQK!((1q5/BK(!.{!;(*}(=~@V(V%!%() U1X(=%YYqxx{QXXa!e_5dNIsKK^DMI~i1n-1FF- !eAh&;IIiA6 " -rJrSrprtr-rrrr4rrrssG $!,317!$-N)bE P 6JZ FG-T*+r