{
  "__type": "IngestedDoc",
  "__tag": 4010,
  "_content": {
    "Notes": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Admonition",
          "__tag": 4056,
          "kind": "versionadded",
          "base_type": "neutral",
          "children": [
            {
              "__type": "AdmonitionTitle",
              "__tag": 4055,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "versionadded 1.7.0"
                }
              ]
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Warns": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Raises": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Yields": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Methods": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Returns": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Parameters",
          "__tag": 4026,
          "children": [
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "res",
              "annotation": "CrosstabResult",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "An object containing the following attributes:"
                    }
                  ]
                },
                {
                  "__type": "DefList",
                  "__tag": 4033,
                  "children": [
                    {
                      "__type": "DefListItem",
                      "__tag": 4037,
                      "dt": {
                        "__type": "Paragraph",
                        "__tag": 4045,
                        "children": [
                          {
                            "__type": "Text",
                            "__tag": 4046,
                            "value": "elements"
                          }
                        ]
                      },
                      "dd": [
                        {
                          "__type": "Paragraph",
                          "__tag": 4045,
                          "children": [
                            {
                              "__type": "Text",
                              "__tag": 4046,
                              "value": "elements"
                            }
                          ]
                        }
                      ]
                    },
                    {
                      "__type": "DefListItem",
                      "__tag": 4037,
                      "dt": {
                        "__type": "Paragraph",
                        "__tag": 4045,
                        "children": [
                          {
                            "__type": "Text",
                            "__tag": 4046,
                            "value": "count"
                          }
                        ]
                      },
                      "dd": [
                        {
                          "__type": "Paragraph",
                          "__tag": 4045,
                          "children": [
                            {
                              "__type": "Text",
                              "__tag": 4046,
                              "value": "count"
                            }
                          ]
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Summary": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Return table of counts for each possible unique combination in "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "*args"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "."
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Receives": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Warnings": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Attributes": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Parameters": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Parameters",
          "__tag": 4026,
          "children": [
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "*args",
              "annotation": "sequences",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "A sequence of sequences whose unique aligned elements are to be counted.  The sequences in args must all be the same length."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "levels",
              "annotation": "sequence, optional",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "If "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "levels"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " is given, it must be a sequence that is the same length as "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "args"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ".  Each element in "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "levels"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " is either a sequence or None.  If it is a sequence, it gives the values in the corresponding sequence in "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "args"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " that are to be counted.  If any value in the sequences in "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "args"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " does not occur in the corresponding sequence in "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "levels"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ", that value is ignored and not counted in the returned array "
                    },
                    {
                      "__type": "InlineRole",
                      "__tag": 4003,
                      "value": "count",
                      "domain": null,
                      "role": null,
                      "inventory": null
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ".  The default value of "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "levels"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " for "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "args[i]"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " is "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "np.unique(args[i])"
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "sparse",
              "annotation": "bool, optional",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "If True, return a sparse matrix.  The matrix will be an instance of the "
                    },
                    {
                      "__type": "CrossRef",
                      "__tag": 4002,
                      "value": "scipy.sparse.coo_matrix",
                      "reference": {
                        "__type": "RefInfo",
                        "__tag": 4000,
                        "module": "scipy",
                        "version": "*",
                        "kind": "api",
                        "path": "scipy.sparse._coo:coo_matrix"
                      },
                      "kind": "module"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " class.  Because SciPy's sparse matrices must be 2-d, only two input sequences are allowed when "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "sparse"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " is True.  Default is False."
                    }
                  ]
                }
              ]
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Extended Summary": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "When "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "len(args) > 1"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", the array computed by this function is often referred to as a "
            },
            {
              "__type": "Emphasis",
              "__tag": 4047,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "contingency table"
                }
              ]
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " "
            },
            {
              "__type": "FootnoteReference",
              "__tag": 4066,
              "label": "1"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "The arguments must be sequences with the same length.  The second return value, "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "count",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", is an integer array with "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "len(args)"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " dimensions.  If "
            },
            {
              "__type": "ParamRef",
              "__tag": 4071,
              "name": "levels"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " is None, the shape of "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "count",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " is "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "(n0, n1, ...)"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", where "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "nk"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " is the number of unique elements in "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "args[k]"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "."
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Other Parameters": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    }
  },
  "_ordered_sections": [
    "Summary",
    "Extended Summary",
    "Parameters",
    "Attributes",
    "Methods",
    "Returns",
    "Yields",
    "Receives",
    "Other Parameters",
    "Raises",
    "Warns",
    "Warnings",
    "Notes"
  ],
  "item_file": "/scipy/stats/_crosstab.py",
  "item_line": 11,
  "item_type": "function",
  "aliases": [
    "scipy.stats._crosstab.crosstab"
  ],
  "example_section_data": {
    "__type": "Section",
    "__tag": 4015,
    "children": [
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "from scipy.stats.contingency import crosstab\n",
        "execution_status": "success"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nGiven the lists `a` and `x`, create a contingency table that counts the\nfrequencies of the corresponding pairs.\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "a = ['A', 'B', 'A', 'A', 'B', 'B', 'A', 'A', 'B', 'B']\nx = ['X', 'X', 'X', 'Y', 'Z', 'Z', 'Y', 'Y', 'Z', 'Z']\nres = crosstab(a, x)\navals, xvals = res.elements\navals\nxvals\nres.count\n",
        "execution_status": "success"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nSo ``('A', 'X')`` occurs twice, ``('A', 'Y')`` occurs three times, etc.\n\nHigher dimensional contingency tables can be created.\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "p = [0, 0, 0, 0, 1, 1, 1, 0, 0, 1]\nres = crosstab(a, x, p)\n",
        "execution_status": "success"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "res.count\n",
        "execution_status": "failure"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "res.count.shape\n",
        "execution_status": "success"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nThe values to be counted can be set by using the `levels` argument.\nIt allows the elements of interest in each input sequence to be\ngiven explicitly instead finding the unique elements of the sequence.\n\nFor example, suppose one of the arguments is an array containing the\nanswers to a survey question, with integer values 1 to 4.  Even if the\nvalue 1 does not occur in the data, we want an entry for it in the table.\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "q1 = [2, 3, 3, 2, 4, 4, 2, 3, 4, 4, 4, 3, 3, 3, 4]  # 1 does not occur.\nq2 = [4, 4, 2, 2, 2, 4, 1, 1, 2, 2, 4, 2, 2, 2, 4]  # 3 does not occur.\noptions = [1, 2, 3, 4]\nres = crosstab(q1, q2, levels=(options, options))\nres.count\n",
        "execution_status": "success"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nIf `levels` is given, but an element of `levels` is None, the unique values\nof the corresponding argument are used. For example,\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "res = crosstab(q1, q2, levels=(None, options))\nres.elements\nres.count\n",
        "execution_status": "success"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nIf we want to ignore the pairs where 4 occurs in ``q2``, we can\ngive just the values [1, 2] to `levels`, and the 4 will be ignored:\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "res = crosstab(q1, q2, levels=(None, [1, 2]))\nres.elements\nres.count\n",
        "execution_status": "success"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nFinally, let's repeat the first example, but return a sparse matrix:\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "res = crosstab(a, x, sparse=True)\n",
        "execution_status": "success"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "res.count\n",
        "execution_status": "failure"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "res.count.toarray()\n",
        "execution_status": "success"
      }
    ],
    "title": [],
    "level": 0,
    "target": null
  },
  "see_also": [
    {
      "__type": "SeeAlsoItem",
      "__tag": 4028,
      "name": {
        "__type": "CrossRef",
        "__tag": 4002,
        "value": "numpy.unique",
        "reference": {
          "__type": "RefInfo",
          "__tag": 4000,
          "module": "current-module",
          "version": "current-version",
          "kind": "to-resolve",
          "path": "numpy.unique"
        },
        "kind": "module"
      },
      "descriptions": [],
      "type": null
    }
  ],
  "signature": {
    "__type": "SignatureNode",
    "__tag": 4029,
    "kind": "function",
    "parameters": [
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "args",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "VAR_POSITIONAL",
        "default": {
          "__type": "Empty",
          "__tag": 4031
        }
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "levels",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "KEYWORD_ONLY",
        "default": "None"
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "sparse",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "KEYWORD_ONLY",
        "default": "False"
      }
    ],
    "return_annotation": {
      "__type": "Empty",
      "__tag": 4031
    },
    "target_name": "crosstab"
  },
  "references": [
    ".. [1] \"Contingency table\", http://en.wikipedia.org/wiki/Contingency_table"
  ],
  "qa": "scipy.stats._crosstab:crosstab",
  "arbitrary": [],
  "local_refs": [
    "*args",
    "levels",
    "res",
    "sparse"
  ]
}