{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "f35e94ca",
            "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": "dd9f5529",
            "cell_type": "markdown",
            "source": "Store sparse array/matrix to disk, and load it again:\n",
            "metadata": {}
        },
        {
            "id": "b6d8c7d8",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nimport scipy as sp\nsparse_array = sp.sparse.csc_array([[0, 0, 3], [4, 0, 0]])\nsparse_array",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "<Compressed Sparse Column sparse array of dtype 'int64'\n    with 2 stored elements and shape (2, 3)>"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "84451979",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "sparse_array.toarray()",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[0, 0, 3],\n       [4, 0, 0]], dtype=int64)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "c0f48688",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "sp.sparse.save_npz('/tmp/sparse_array.npz', sparse_array)\nsparse_array = sp.sparse.load_npz('/tmp/sparse_array.npz')",
            "outputs": []
        },
        {
            "id": "f6038822",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "sparse_array",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "<Compressed Sparse Column sparse array of dtype 'int64'\n    with 2 stored elements and shape (2, 3)>"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "27ac626c",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "sparse_array.toarray()",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[0, 0, 3],\n       [4, 0, 0]], dtype=int64)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "53fda2e6",
            "cell_type": "markdown",
            "source": "In this example we force the result to be csr_array from csr_matrix",
            "metadata": {}
        },
        {
            "id": "95e3f44f",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "sparse_matrix = sp.sparse.csc_matrix([[0, 0, 3], [4, 0, 0]])\nsp.sparse.save_npz('/tmp/sparse_matrix.npz', sparse_matrix)\ntmp = sp.sparse.load_npz('/tmp/sparse_matrix.npz')\nsparse_array = sp.sparse.csr_array(tmp)",
            "outputs": []
        }
    ]
}