{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "d4f8e3c9-aaef-d1cb-d6c0-c1ceff1a247d"
      },
      "source": [
        "Sparse matrix with explicit zeroes!\n",
        "-----------------------------------\n",
        "\n",
        "## Example: create a sparse matrix, check its (dense) content, modify it by adding explicit zeroes, check again its (dense) content"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "2f7a6c69-a1be-d333-7112-dae00749ee1b"
      },
      "source": [
        "----------\n",
        "\n",
        "\n",
        "**Lets load what we need for the example:**"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "8806dad1-cd1b-354a-b09a-e7df85d2e255"
      },
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "from scipy.sparse import lil_matrix\n",
        "from scipy.sparse import csr_matrix"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "2ab5fe65-9496-e027-c3fc-ea7d2146b528"
      },
      "source": [
        "----------\n",
        "\n",
        "\n",
        "**Lets create a sparse modifiable matrix (3 rows, 2 columns)**"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "eca3f4d3-9b2c-01b1-4679-543cd9797ec9"
      },
      "outputs": [],
      "source": [
        "csr = csr_matrix(np.array([[0, 0],\n",
        "        [2, 1],\n",
        "        [np.nan, np.nan]]))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "23ead712-8a9a-d2d4-b26d-34d4b2cc8d19"
      },
      "source": [
        "----------\n",
        "\n",
        "\n",
        "**Lets check the dense matrix**"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "41a0d2c0-03cb-c3f6-bb28-4148d86faf5e"
      },
      "outputs": [],
      "source": [
        "csr.todense()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "cf8cd2ed-1464-b699-5ed3-4b23644c0fd0"
      },
      "source": [
        "----------\n",
        "\n",
        "\n",
        "**Can we put explicit zeroes in the first row? Yes we can.**"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "fc1c9ed3-fe92-eb5a-6a9d-92c856f48688"
      },
      "outputs": [],
      "source": [
        "csr[0, :] = [0, 0]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "7426f526-40e0-8432-4c1f-2628e1d4b3ae"
      },
      "source": [
        "----------\n",
        "\n",
        "\n",
        "**What is inside the sparse matrix now? 6 elements instead of 4!**"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "13b9490e-0e97-7480-1c86-f12d1b853a2a"
      },
      "outputs": [],
      "source": [
        "csr"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "120f9862-95de-f16b-0469-dbc9b57a3c5e"
      },
      "source": [
        "----------\n",
        "\n",
        "\n",
        "**Do we have what we had at the beginning? Yes!**"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "cc7862a1-423d-2a11-9d72-b726be944cba"
      },
      "outputs": [],
      "source": [
        "csr.todense()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "f157fdf7-60f8-e8b1-412d-a1fb98eaab4e"
      },
      "source": [
        "----------\n",
        "\n",
        "\n",
        "**Indirect conversion to lil_matrix (if you want to make many modifications) is working as intended**"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "bd00c113-c034-f025-727f-d484f26b7e01"
      },
      "outputs": [],
      "source": [
        "csr.tolil()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "127a1a81-82d1-297a-75cf-db6d84b360e4"
      },
      "source": [
        "----------\n",
        "\n",
        "\n",
        "**Lets try directly with a lil_matrix!**"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a2a748f1-b3e6-d607-5198-59b68fb07941"
      },
      "outputs": [],
      "source": [
        "lil = lil_matrix(np.array([[0, 0],\n",
        "        [2, 1],\n",
        "        [np.nan, np.nan]]))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "597e5cf5-e769-7a8d-9b21-57c33b4aa793"
      },
      "source": [
        "----------\n",
        "\n",
        "\n",
        "**Setting up explicit zeroes...**"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "f75c8168-0717-1114-b8e0-9d10e02d9793"
      },
      "outputs": [],
      "source": [
        "lil[0, :] = [0, 0]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "6768ba7d-ea81-7ef4-49e9-e04dd47c8af5"
      },
      "source": [
        "----------\n",
        "\n",
        "\n",
        "**NOPE! Specifying explicit zeroes in lil_matrix will NOT work (along with any non-compressed formats)**"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7447751d-d74d-b3ce-22ae-401144f3e827"
      },
      "outputs": [],
      "source": [
        "lil"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "c831a39f-dfde-2e84-90e2-8821a210fa88"
      },
      "source": [
        "----------\n",
        "\n",
        "\n",
        "**Maybe because the row is full of zeroes...?**"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "f8443e8f-1b30-e895-ceb1-0304e19b8c96"
      },
      "outputs": [],
      "source": [
        "lil[0, :] = [0, 1]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "24367213-d465-7458-c8c1-558d9b143f97"
      },
      "source": [
        "----------\n",
        "\n",
        "\n",
        "**NOPE! Only 1 element was added, the zero was discarded**"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "80861ed1-bc94-387b-5a6c-6e6d27dc557a"
      },
      "outputs": [],
      "source": [
        "lil"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "b82f757c-dfa9-8189-7450-21c5f5cce4e3"
      },
      "source": [
        "----------\n",
        "\n",
        "\n",
        "**Only the zero is not explicit!**"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "8d8d5d43-4fbe-0d3c-0366-1cdc58d6687a"
      },
      "outputs": [],
      "source": [
        "lil.tocsr()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "49f116e7-d8a5-c6b5-0435-9b9c65689da5"
      },
      "source": [
        "----------\n",
        "\n",
        "\n",
        "**Lets check directly if it is true with the number of non zero entries function: yep, the XYZ in \"with [XYZ] stored elements\" is the .nnz value**"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "254611a5-1c85-237a-da2c-486109827936"
      },
      "outputs": [],
      "source": [
        "lil.tocsr().nnz"
      ]
    }
  ],
  "metadata": {
    "_change_revision": 0,
    "_is_fork": false,
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.5.2"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}