bundles / scipy 1.17.1 / scipy / stats / _crosstab / crosstab
function
scipy.stats._crosstab:crosstab
source: /scipy/stats/_crosstab.py :11
Signature
def crosstab ( * args , levels = None , sparse = False ) Summary
Return table of counts for each possible unique combination in *args.
Extended Summary
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: sequencesA sequence of sequences whose unique aligned elements are to be counted. The sequences in args must all be the same length.
levels: sequence, optionalIf
levelsis given, it must be a sequence that is the same length asargs. Each element inlevelsis either a sequence or None. If it is a sequence, it gives the values in the corresponding sequence inargsthat are to be counted. If any value in the sequences inargsdoes not occur in the corresponding sequence inlevels, that value is ignored and not counted in the returned arraycount. The default value oflevelsforargs[i]isnp.unique(args[i])sparse: bool, optionalIf 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
sparseis True. Default is False.
Returns
res: CrosstabResultAn object containing the following attributes:
elements
elements
count
count
Notes
Examples
from scipy.stats.contingency import crosstab
✓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 xvals res.count✓
p = [0, 0, 0, 0, 1, 1, 1, 0, 0, 1] res = crosstab(a, x, p)✓
res.count
✗res.count.shape
✓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✓
res = crosstab(q1, q2, levels=(None, options)) res.elements res.count✓
res = crosstab(q1, q2, levels=(None, [1, 2])) res.elements res.count✓
res = crosstab(a, x, sparse=True)
✓res.count
✗res.count.toarray()
✓See also
- numpy.unique
Aliases
-
scipy.stats._crosstab.crosstab