
    (ph                     J    S SK rS SKJr  S SKJr  \" SSS/5      rSSS.S	 jrg)
    N)
coo_matrix)_make_tuple_bunchCrosstabResultelementscountF)levelssparsec           
        ^ [        U5      nUS:X  a  [        S5      e[        US   5      m[        U4S jUSS  5       5      (       d  [        S5      eU(       a  US:w  a  [        S5      eU c0  [	        U Vs/ s H  n[
        R                  " US	S
9PM     sn6 u  pVGOr[        U 5      U:w  a  [        S5      eU Vs/ s H  n[
        R                  " U5      PM     nn[
        R                  " UT4[
        R                  S9n[
        R                  " UT4[
        R                  S9n	/ n[        [	        X5      5       H  u  n
u  pUc'  [
        R                  " US	S
9u  oU
SS24'   S	XSS24'   OjU[
        R                  " U5      R                  SS5      :H  n[
        R                  " USS9XSS24'   UR                  R                  5       nUS   XUS   4'   UR!                  U5        M     UR                  SS9n[#        U	SS2U4   5      nU(       aI  [%        [
        R&                  " [        US   5      [(        S9US   US   445      nUR+                  5         OTU Vs/ s H  n[        U5      PM     nn[
        R                  " U[(        S9n[
        R,                  R/                  XS5        [1        X_5      $ s  snf s  snf s  snf )au  
Return table of counts for each possible unique combination in ``*args``.

When ``len(args) > 1``, the array computed by this function is
often referred to as a *contingency table* [1]_.

The arguments must be sequences with the same length.  The second return
value, `count`, is an integer array with ``len(args)`` dimensions.  If
`levels` is None, the shape of `count` is ``(n0, n1, ...)``, where ``nk``
is the number of unique elements in ``args[k]``.

Parameters
----------
*args : sequences
    A sequence of sequences whose unique aligned elements are to be
    counted.  The sequences in args must all be the same length.
levels : sequence, optional
    If `levels` is given, it must be a sequence that is the same length as
    `args`.  Each element in `levels` is either a sequence or None.  If it
    is a sequence, it gives the values in the corresponding sequence in
    `args` that are to be counted.  If any value in the sequences in `args`
    does not occur in the corresponding sequence in `levels`, that value
    is ignored and not counted in the returned array `count`.  The default
    value of `levels` for ``args[i]`` is ``np.unique(args[i])``
sparse : bool, optional
    If True, return a sparse matrix.  The matrix will be an instance of
    the `scipy.sparse.coo_matrix` class.  Because SciPy's sparse matrices
    must be 2-d, only two input sequences are allowed when `sparse` is
    True.  Default is False.

Returns
-------
res : CrosstabResult
    An object containing the following attributes:

    elements : tuple of numpy.ndarrays.
        Tuple of length ``len(args)`` containing the arrays of elements
        that are counted in `count`.  These can be interpreted as the
        labels of the corresponding dimensions of `count`. If `levels` was
        given, then if ``levels[i]`` is not None, ``elements[i]`` will
        hold the values given in ``levels[i]``.
    count : numpy.ndarray or scipy.sparse.coo_matrix
        Counts of the unique elements in ``zip(*args)``, stored in an
        array. Also known as a *contingency table* when ``len(args) > 1``.

See Also
--------
numpy.unique

Notes
-----
.. versionadded:: 1.7.0

References
----------
.. [1] "Contingency table", http://en.wikipedia.org/wiki/Contingency_table

Examples
--------
>>> from scipy.stats.contingency import crosstab

Given the lists `a` and `x`, create a contingency table that counts the
frequencies of the corresponding pairs.

>>> a = ['A', 'B', 'A', 'A', 'B', 'B', 'A', 'A', 'B', 'B']
>>> x = ['X', 'X', 'X', 'Y', 'Z', 'Z', 'Y', 'Y', 'Z', 'Z']
>>> res = crosstab(a, x)
>>> avals, xvals = res.elements
>>> avals
array(['A', 'B'], dtype='<U1')
>>> xvals
array(['X', 'Y', 'Z'], dtype='<U1')
>>> res.count
array([[2, 3, 0],
       [1, 0, 4]])

So ``('A', 'X')`` occurs twice, ``('A', 'Y')`` occurs three times, etc.

Higher dimensional contingency tables can be created.

>>> p = [0, 0, 0, 0, 1, 1, 1, 0, 0, 1]
>>> res = crosstab(a, x, p)
>>> res.count
array([[[2, 0],
        [2, 1],
        [0, 0]],
       [[1, 0],
        [0, 0],
        [1, 3]]])
>>> res.count.shape
(2, 3, 2)

The values to be counted can be set by using the `levels` argument.
It allows the elements of interest in each input sequence to be
given explicitly instead finding the unique elements of the sequence.

For example, suppose one of the arguments is an array containing the
answers to a survey question, with integer values 1 to 4.  Even if the
value 1 does not occur in the data, we want an entry for it in the table.

>>> q1 = [2, 3, 3, 2, 4, 4, 2, 3, 4, 4, 4, 3, 3, 3, 4]  # 1 does not occur.
>>> q2 = [4, 4, 2, 2, 2, 4, 1, 1, 2, 2, 4, 2, 2, 2, 4]  # 3 does not occur.
>>> options = [1, 2, 3, 4]
>>> res = crosstab(q1, q2, levels=(options, options))
>>> res.count
array([[0, 0, 0, 0],
       [1, 1, 0, 1],
       [1, 4, 0, 1],
       [0, 3, 0, 3]])

If `levels` is given, but an element of `levels` is None, the unique values
of the corresponding argument are used. For example,

>>> res = crosstab(q1, q2, levels=(None, options))
>>> res.elements
[array([2, 3, 4]), [1, 2, 3, 4]]
>>> res.count
array([[1, 1, 0, 1],
       [1, 4, 0, 1],
       [0, 3, 0, 3]])

If we want to ignore the pairs where 4 occurs in ``q2``, we can
give just the values [1, 2] to `levels`, and the 4 will be ignored:

>>> res = crosstab(q1, q2, levels=(None, [1, 2]))
>>> res.elements
[array([2, 3, 4]), [1, 2]]
>>> res.count
array([[1, 1],
       [1, 4],
       [0, 3]])

Finally, let's repeat the first example, but return a sparse matrix:

>>> res = crosstab(a, x, sparse=True)
>>> res.count
<COOrdinate sparse matrix of dtype 'int64'
    with 4 stored elements and shape (2, 3)>
>>> res.count.toarray()
array([[2, 3, 0],
       [1, 0, 4]])

r   z(At least one input sequence is required.c              3   @   >#    U  H  n[        U5      T:H  v   M     g 7f)N)len).0alen0s     H/var/www/html/venv/lib/python3.13/site-packages/scipy/stats/_crosstab.py	<genexpr>crosstab.<locals>.<genexpr>   s     0x!s1v~xs      Nz.All input sequences must have the same length.   z<When `sparse` is True, only two input sequences are allowed.T)return_inversez4len(levels) must equal the number of input sequences)dtype)axis)r   	TypeErrorall
ValueErrorzipnpuniqueasarrayzerosbool_intp	enumeratereshapeanyTnonzeroappendtupler   onesintsum_duplicatesaddatr   )r   r	   argsnargsr   actual_levelsindicesargmaskinvklevels_listqqnzmask_allr   ushaper   s                     @r   crosstabr=      s   ` IEzBCCtAw<D0tABx000IJJ%1* ( ) 	) ~!$04'6041 (*yy4'H04'6 "7w v;% ) * * ,004C

340xxRXX6hht}BGG4%.s6/@%A!A!")+3t)L&AY!T
2::k2::2qAAVVAA.T
cckkm!$Qs1vI  - &B 888#AxK()BGGC
O3?$QZ46 7!./AQ/c*
		%!$-//G'6 1. 0s   8K = K%K
)numpyr   scipy.sparser   scipy._lib._bunchr   r   r=        r   <module>rC      s1     # / #z7+
   A0rB   