{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "8d22d430",
            "cell_type": "markdown",
            "source": "<div class='alert alert-warning'>\n\nSciPy's interactive examples with Jupyterlite are experimental and may not always work as expected. Execution of cells containing imports may result in large downloads (up to 60MB of content for the first import from SciPy). Load times when importing from SciPy may take roughly 10-20 seconds. If you notice any problems, feel free to open an [issue](https://github.com/scipy/scipy/issues/new/choose).\n\n</div>",
            "metadata": {}
        },
        {
            "id": "826ac39a",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.stats.contingency import crosstab",
            "outputs": []
        },
        {
            "id": "c3447a40",
            "cell_type": "markdown",
            "source": "Given the lists `a` and `x`, create a contingency table that counts the\nfrequencies of the corresponding pairs.\n",
            "metadata": {}
        },
        {
            "id": "cac6d735",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "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",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array(['A', 'B'], dtype='<U1')"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "0e76c668",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "xvals",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array(['X', 'Y', 'Z'], dtype='<U1')"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "bbdd599e",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "res.count",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[2, 3, 0],\n       [1, 0, 4]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "1a197231",
            "cell_type": "markdown",
            "source": "So ``('A', 'X')`` occurs twice, ``('A', 'Y')`` occurs three times, etc.\n\nHigher dimensional contingency tables can be created.\n",
            "metadata": {}
        },
        {
            "id": "1e9cc8d0",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "p = [0, 0, 0, 0, 1, 1, 1, 0, 0, 1]\nres = crosstab(a, x, p)\nres.count",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[[2, 0],\n        [2, 1],\n        [0, 0]],\n       [[1, 0],\n        [0, 0],\n        [1, 3]]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "8a7565ee",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "res.count.shape",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(2, 3, 2)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "c717dd02",
            "cell_type": "markdown",
            "source": "The 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",
            "metadata": {}
        },
        {
            "id": "e1226d7d",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "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",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[0, 0, 0, 0],\n       [1, 1, 0, 1],\n       [1, 4, 0, 1],\n       [0, 3, 0, 3]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "d34d64bc",
            "cell_type": "markdown",
            "source": "If `levels` is given, but an element of `levels` is None, the unique values\nof the corresponding argument are used. For example,\n",
            "metadata": {}
        },
        {
            "id": "2ababb31",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "res = crosstab(q1, q2, levels=(None, options))\nres.elements",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "[array([2, 3, 4]), [1, 2, 3, 4]]"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "840b3722",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "res.count",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[1, 1, 0, 1],\n       [1, 4, 0, 1],\n       [0, 3, 0, 3]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "c573aca6",
            "cell_type": "markdown",
            "source": "If 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",
            "metadata": {}
        },
        {
            "id": "22262ed6",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "res = crosstab(q1, q2, levels=(None, [1, 2]))\nres.elements",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "[array([2, 3, 4]), [1, 2]]"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "727d74bb",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "res.count",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[1, 1],\n       [1, 4],\n       [0, 3]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "9e3e6d03",
            "cell_type": "markdown",
            "source": "Finally, let's repeat the first example, but return a sparse matrix:\n",
            "metadata": {}
        },
        {
            "id": "274da875",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "res = crosstab(a, x, sparse=True)\nres.count",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "<COOrdinate sparse matrix of dtype 'int64'\n    with 4 stored elements and shape (2, 3)>"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "22c2c28a",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "res.count.toarray()",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[2, 3, 0],\n       [1, 0, 4]])"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}