{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "collapsed_sections": [
        "ub-OhPsJ2Sto",
        "VoWJhNhfyVeb",
        "azQ2OYYA0k5Y",
        "U8Wemfh13oDd",
        "tyEA-akV7iMW",
        "gwLrewQb-UbG"
      ]
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# NumPY"
      ],
      "metadata": {
        "id": "QGYkDuponq9a"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Import library\n"
      ],
      "metadata": {
        "id": "lvtbpGYhn_Hj"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Not recommended way"
      ],
      "metadata": {
        "id": "8za1lDpHqZs3"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import numpy\n",
        "numpy.array([1, 2, 3])"
      ],
      "metadata": {
        "id": "WUaqTPhBqo2V",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "aaa7b20a-36a5-4362-e593-b8f2196f7955"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([1, 2, 3])"
            ]
          },
          "metadata": {},
          "execution_count": 1
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "from numpy import *\n",
        "array([1, 2, 3, 4])"
      ],
      "metadata": {
        "id": "aIg78lR9rVVG",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "62123596-13cb-4ab4-af41-b9696c725dff"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([1, 2, 3, 4])"
            ]
          },
          "metadata": {},
          "execution_count": 3
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Recommended way:"
      ],
      "metadata": {
        "id": "GzRDrulmqSA-"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import numpy as np\n",
        "np.array([1, 2])"
      ],
      "metadata": {
        "id": "1OauaVesp7jo",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "47849617-b543-491c-a9ee-b6018a1ce769"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([1, 2])"
            ]
          },
          "metadata": {},
          "execution_count": 4
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "###Arrays"
      ],
      "metadata": {
        "id": "T2X_4RMVsm5c"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "a = np.array([1, 2, 3, 4])\n",
        "type(a)"
      ],
      "metadata": {
        "id": "5evY9E47ssOG",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "eb305c5a-13bf-4600-86c1-446dbc6d6205"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "numpy.ndarray"
            ]
          },
          "metadata": {},
          "execution_count": 5
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a.dtype"
      ],
      "metadata": {
        "id": "wP0YfmaAtaV3",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "3b8ad011-dcaf-46d8-c0d3-f911f3422a2a"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "dtype('int64')"
            ]
          },
          "metadata": {},
          "execution_count": 6
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "f = np.array([1.2, 1.3, 1.4])\n",
        "f.dtype"
      ],
      "metadata": {
        "id": "ROPrjzb7t2lh",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "6635c6ef-b140-4c0d-c959-af96c63c8f4c"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "dtype('float64')"
            ]
          },
          "metadata": {},
          "execution_count": 7
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "b = np.array([1, 'abc', True])\n",
        "b.dtype"
      ],
      "metadata": {
        "id": "QaR6eyrnvbSZ",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "ca0658cf-73bf-4386-b752-a3691859f0a8"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "dtype('<U21')"
            ]
          },
          "metadata": {},
          "execution_count": 8
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "b.dtype.type"
      ],
      "metadata": {
        "id": "1lvc2wZ0qgq-",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "886405d0-140e-48c3-9787-be95c6cf9cca"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "numpy.str_"
            ]
          },
          "metadata": {},
          "execution_count": 9
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a[0]"
      ],
      "metadata": {
        "id": "NHfTWsDVuDwx",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "63d724de-f7c2-42d2-cc16-11b286faed8e"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "1"
            ]
          },
          "metadata": {},
          "execution_count": 10
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a[0] = 10\n",
        "a"
      ],
      "metadata": {
        "id": "-OmDI-n8uHFR",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "335cde45-bc61-4202-85a7-3b5acad9f92f"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([10,  2,  3,  4])"
            ]
          },
          "metadata": {},
          "execution_count": 11
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a[0] = 11.5\n",
        "a"
      ],
      "metadata": {
        "id": "FTNwROHpuP2D",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "530d6646-5e7f-4a48-a818-90301327315c"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([11,  2,  3,  4])"
            ]
          },
          "metadata": {},
          "execution_count": 12
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a[0] = \"string\""
      ],
      "metadata": {
        "id": "8be160_NwcLY",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 141
        },
        "outputId": "9838d471-ce10-4aef-9f18-0ed2963c2f27"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "error",
          "ename": "ValueError",
          "evalue": "invalid literal for int() with base 10: 'string'",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-13-ad56bbf46c8a>\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"string\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
            "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: 'string'"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a.ndim"
      ],
      "metadata": {
        "id": "AV1A510MucD2",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "0c87f212-7aec-4907-8e84-5a168b8b7f22"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "1"
            ]
          },
          "metadata": {},
          "execution_count": 14
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a.shape"
      ],
      "metadata": {
        "id": "7KGvUITMuurK",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "f0e55388-be82-4a72-def5-613084ec3420"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(4,)"
            ]
          },
          "metadata": {},
          "execution_count": 15
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a.size"
      ],
      "metadata": {
        "id": "hWKVjayGvJ70",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "0e25a3cc-b2e6-4abd-86b5-43bb19d2e37a"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "4"
            ]
          },
          "metadata": {},
          "execution_count": 16
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a = np.array([[11, 12, 13], [21, 22, 23]])\n",
        "a"
      ],
      "metadata": {
        "id": "Hk_gLXT9vczi",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "1e077ef4-4d7b-4061-fb33-95173c94f51c"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[11, 12, 13],\n",
              "       [21, 22, 23]])"
            ]
          },
          "metadata": {},
          "execution_count": 17
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a.shape"
      ],
      "metadata": {
        "id": "PzKGblfixXkL",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "536fb543-028d-40cf-ee8f-7733b817efa1"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(2, 3)"
            ]
          },
          "metadata": {},
          "execution_count": 18
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a[0,0]"
      ],
      "metadata": {
        "id": "v5FfLh_jyJ6T",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "99a9f732-c9f1-4748-8c2a-ef4c0886651e"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "11"
            ]
          },
          "metadata": {},
          "execution_count": 19
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a[1,2]"
      ],
      "metadata": {
        "id": "Xn3ws7GTyMer",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "c98c8e34-793f-49f3-b481-b215fc2ca95c"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "23"
            ]
          },
          "metadata": {},
          "execution_count": 20
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "####Array indexing"
      ],
      "metadata": {
        "id": "ub-OhPsJ2Sto"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "#row\n",
        "a[1,]"
      ],
      "metadata": {
        "id": "qaSnWFYp2bPR",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "1a3329b3-f447-4bac-b60b-ee0da27e3929"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([21, 22, 23])"
            ]
          },
          "metadata": {},
          "execution_count": 21
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "#row\n",
        "a[1, :]"
      ],
      "metadata": {
        "id": "KNVn1QMe2qsR",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "aa51d9a0-9125-4b77-caed-d18a0cd6e121"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([21, 22, 23])"
            ]
          },
          "metadata": {},
          "execution_count": 22
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "#column\n",
        "a[:, 2]"
      ],
      "metadata": {
        "id": "9oMWDi5F2vN1",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "6808a588-fcc9-440f-f18a-fb6079d3281c"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([13, 23])"
            ]
          },
          "metadata": {},
          "execution_count": 23
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a[:, 0:2]"
      ],
      "metadata": {
        "id": "lkMLgYPc26zh",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "21b84d7d-f507-496c-d864-56e59eafc9f8"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[11, 12],\n",
              "       [21, 22]])"
            ]
          },
          "metadata": {},
          "execution_count": 24
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a[0, 1:]"
      ],
      "metadata": {
        "id": "nPkAFDMs3BOK",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "d6c8ed60-5c97-42ba-d0ed-cd2c418d268b"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([12, 13])"
            ]
          },
          "metadata": {},
          "execution_count": 25
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "####Alternative options to create array\n",
        "\n",
        "[documentation](https://numpy.org/doc/stable/reference/routines.array-creation.html)"
      ],
      "metadata": {
        "id": "VoWJhNhfyVeb"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "a = np.zeros((3, 5))\n",
        "a"
      ],
      "metadata": {
        "id": "SwxoJ93Uyyzj",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "3dfef913-8905-4678-eb78-bba2afb78155"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[0., 0., 0., 0., 0.],\n",
              "       [0., 0., 0., 0., 0.],\n",
              "       [0., 0., 0., 0., 0.]])"
            ]
          },
          "metadata": {},
          "execution_count": 26
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a = np.ones((2,3,4))\n",
        "a"
      ],
      "metadata": {
        "id": "donBpjLOzCbI",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "15b67619-f4f6-4dee-8f87-20bd1f40657e"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[[1., 1., 1., 1.],\n",
              "        [1., 1., 1., 1.],\n",
              "        [1., 1., 1., 1.]],\n",
              "\n",
              "       [[1., 1., 1., 1.],\n",
              "        [1., 1., 1., 1.],\n",
              "        [1., 1., 1., 1.]]])"
            ]
          },
          "metadata": {},
          "execution_count": 27
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a.shape"
      ],
      "metadata": {
        "id": "jHA2RB1EzLoT",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "5903797f-3537-422a-ebbc-4c315c8bb249"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(2, 3, 4)"
            ]
          },
          "metadata": {},
          "execution_count": 28
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a.size"
      ],
      "metadata": {
        "id": "6Y8PeyY0zOKm",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "d830966a-df2c-4b25-82c7-7bf3d68b1311"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "24"
            ]
          },
          "metadata": {},
          "execution_count": 29
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a = np.full((2, 3), 7)\n",
        "a"
      ],
      "metadata": {
        "id": "_TWvCUotzQDn",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "bdd90cf5-613d-430c-fd18-0a4bee8dca97"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[7, 7, 7],\n",
              "       [7, 7, 7]])"
            ]
          },
          "metadata": {},
          "execution_count": 30
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "np.full_like(a, 11)"
      ],
      "metadata": {
        "id": "fCXB9idLzkVq",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "25e80b46-293c-4550-c799-3a9b1d4ba554"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[11, 11, 11],\n",
              "       [11, 11, 11]])"
            ]
          },
          "metadata": {},
          "execution_count": 31
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a = np.arange(0, 10, 2)\n",
        "a"
      ],
      "metadata": {
        "id": "1CkhYvFNzsYk",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "6854b80d-c74b-41fa-fb13-2a0f1590c1b5"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([0, 2, 4, 6, 8])"
            ]
          },
          "metadata": {},
          "execution_count": 32
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "###Operations with arrays\n",
        "### Universal functions (ufunc)"
      ],
      "metadata": {
        "id": "azQ2OYYA0k5Y"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "a = np.ones((4))\n",
        "a"
      ],
      "metadata": {
        "id": "h0ouBiMh1GXK",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "7b0ea567-a20b-48fb-bf3c-8484a93fd390"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([1., 1., 1., 1.])"
            ]
          },
          "metadata": {},
          "execution_count": 33
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "b = np.full_like(a, 2)\n",
        "b"
      ],
      "metadata": {
        "id": "rq-3DikU1T9F",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "cd360371-b14c-46de-dd2d-bd863cc280d5"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([2., 2., 2., 2.])"
            ]
          },
          "metadata": {},
          "execution_count": 34
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "np.add(a, b)"
      ],
      "metadata": {
        "id": "ivngcufl1c-_",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "d95932f2-46ae-47a0-e949-372284cc6fa7"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([3., 3., 3., 3.])"
            ]
          },
          "metadata": {},
          "execution_count": 35
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a + b"
      ],
      "metadata": {
        "id": "gWnEimhc1gLz",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "17eefd45-c5a0-4efb-893e-8d235f949586"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([3., 3., 3., 3.])"
            ]
          },
          "metadata": {},
          "execution_count": 36
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a - b"
      ],
      "metadata": {
        "id": "hVA12P741hUr",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "8967c222-a552-4657-f566-228bf7ec8118"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([-1., -1., -1., -1.])"
            ]
          },
          "metadata": {},
          "execution_count": 37
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a * b"
      ],
      "metadata": {
        "id": "8Ei8BKym1kwo",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "de890710-35b1-4fad-b349-adb033e76247"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([2., 2., 2., 2.])"
            ]
          },
          "metadata": {},
          "execution_count": 38
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a / b"
      ],
      "metadata": {
        "id": "acksDi3T1mtn",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "51bc8af0-f9e7-4d1f-d936-ec732720d92d"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([0.5, 0.5, 0.5, 0.5])"
            ]
          },
          "metadata": {},
          "execution_count": 39
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "c = np.zeros((1,3))\n",
        "np.cos(c)"
      ],
      "metadata": {
        "id": "s_8bfnil3Ub4",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "968aaa8c-3235-47c2-8a14-91933f0cdbad"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[1., 1., 1.]])"
            ]
          },
          "metadata": {},
          "execution_count": 40
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Vectorization\n",
        "\n",
        "All previous operations are element-wise and were called without loops. This because all of them are vectorized operations."
      ],
      "metadata": {
        "id": "U8Wemfh13oDd"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "a_list = [i for i in range(100000)]\n",
        "type(a_list)"
      ],
      "metadata": {
        "id": "UbCi6aUp4Hlb",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "4c168007-3200-4e87-a384-c007f9ee7088"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "list"
            ]
          },
          "metadata": {},
          "execution_count": 41
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "from datetime import datetime\n",
        "start = datetime.now()\n",
        "a2_list = [a + 2 for a in a_list]\n",
        "end = datetime.now()\n",
        "loop_time = (end-start).total_seconds()\n",
        "print(loop_time)"
      ],
      "metadata": {
        "id": "RBRgxVvA4ovg",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "63ae22db-5c55-433b-f782-fef9979fef0e"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "0.012803\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "%%timeit\n",
        "a2_list = [a + 2 for a in a_list]"
      ],
      "metadata": {
        "id": "VmTZ10Zq6i5s",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "60063d82-2f94-4629-a4c8-71128337027c"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "6.27 ms ± 170 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a_arr = np.array(a_list)\n",
        "type(a_arr)"
      ],
      "metadata": {
        "id": "WRtDWCra5O1z",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "645961a3-1176-4aab-8f43-a5dcd75b1a7e"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "numpy.ndarray"
            ]
          },
          "metadata": {},
          "execution_count": 44
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "start = datetime.now()\n",
        "a2_arr = a_arr + 2\n",
        "end = datetime.now()\n",
        "vec_time = (end-start).total_seconds()\n",
        "print(vec_time)"
      ],
      "metadata": {
        "id": "BHZyjcpA60LJ",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "b2bcbf22-b420-4323-d296-de0d57a638f1"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "0.005663\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "%%timeit\n",
        "a2_arr = a_arr + 2"
      ],
      "metadata": {
        "id": "lmJUHuk25XQQ",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "78e5b324-ec15-4feb-a4f8-9f4f1ce15f97"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "57.6 µs ± 3.25 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "loop_time/vec_time"
      ],
      "metadata": {
        "id": "8DmB6q9U7H0X",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "a84a9802-42a9-4602-932f-44ff6ad06969"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "2.2608158220024723"
            ]
          },
          "metadata": {},
          "execution_count": 47
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Broadcasting\n",
        "\n",
        "[Documentations](https://numpy.org/doc/stable/user/basics.broadcasting.html)\n",
        "\n",
        "When operating on two arrays, NumPy compares their shapes element-wise. It starts with the trailing (i.e. rightmost) dimensions and works its way left. Two dimensions are compatible when\n",
        "\n",
        "* they are equal, or\n",
        "* one of them is 1"
      ],
      "metadata": {
        "id": "tyEA-akV7iMW"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "a = np.array([1, 1, 1])\n",
        "a + 1"
      ],
      "metadata": {
        "id": "C8ugwDok7ja3",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "512085d4-bf30-4482-8151-8fa3e28dfe13"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([2, 2, 2])"
            ]
          },
          "metadata": {},
          "execution_count": 48
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a = np.ones((3, 2))\n",
        "a.shape"
      ],
      "metadata": {
        "id": "BUSbPePZ_k18",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "754123ad-c9a9-431f-cdff-f51767dd5199"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(3, 2)"
            ]
          },
          "metadata": {},
          "execution_count": 49
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "b = np.array([5, 5])\n",
        "b.shape"
      ],
      "metadata": {
        "id": "jX3v6rP_AAKy",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "a90a3942-0eed-4b77-89c0-eb9656a45c0c"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(2,)"
            ]
          },
          "metadata": {},
          "execution_count": 50
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "b"
      ],
      "metadata": {
        "id": "bVMC3mtshUlT",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "b2ee929a-0648-4f91-ea97-5931e673f7ec"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([5, 5])"
            ]
          },
          "metadata": {},
          "execution_count": 51
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a+b"
      ],
      "metadata": {
        "id": "itB8iE0BAEGL",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "0419a159-8533-47cb-ef40-c46f9051b4ba"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[6., 6.],\n",
              "       [6., 6.],\n",
              "       [6., 6.]])"
            ]
          },
          "metadata": {},
          "execution_count": 52
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a"
      ],
      "metadata": {
        "id": "kd2YS-XtA2kY",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "e1f98008-e70b-4eb4-b3d8-685baaf82db1"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[1., 1.],\n",
              "       [1., 1.],\n",
              "       [1., 1.]])"
            ]
          },
          "metadata": {},
          "execution_count": 53
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "b = np.array([5, 5, 5])\n",
        "a + b"
      ],
      "metadata": {
        "id": "3z_WMyt9CcbT",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 159
        },
        "outputId": "7b0cf1c3-c37d-4409-a355-13099b8f1844"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "error",
          "ename": "ValueError",
          "evalue": "operands could not be broadcast together with shapes (3,2) (3,) ",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-54-7eef8bc70dda>\u001b[0m in \u001b[0;36m<cell line: 2>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0mb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0ma\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
            "\u001b[0;31mValueError\u001b[0m: operands could not be broadcast together with shapes (3,2) (3,) "
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "b+a"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 141
        },
        "id": "FqEZhZL9RkwC",
        "outputId": "ba67e736-9455-4692-cb3c-86ff1a68b373"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "error",
          "ename": "ValueError",
          "evalue": "operands could not be broadcast together with shapes (3,) (3,2) ",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-55-a3dc031780bc>\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mb\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
            "\u001b[0;31mValueError\u001b[0m: operands could not be broadcast together with shapes (3,) (3,2) "
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "c = np.ones((4,2))\n",
        "d = np.full((2,1),3)\n",
        "c+d"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 176
        },
        "id": "FT4iIodx7o0P",
        "outputId": "a8aacf01-7d5c-4fc2-8d08-863fee245cc6"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "error",
          "ename": "ValueError",
          "evalue": "operands could not be broadcast together with shapes (4,2) (2,1) ",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-58-e6dc24afeaee>\u001b[0m in \u001b[0;36m<cell line: 3>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0mc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mones\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0md\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfull\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mc\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0md\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
            "\u001b[0;31mValueError\u001b[0m: operands could not be broadcast together with shapes (4,2) (2,1) "
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a1 = np.zeros((2,3,5))\n",
        "a1"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "7J7HJ2vRR7T7",
        "outputId": "bfebf697-99f9-48d1-8f57-2b634033f400"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[[0., 0., 0., 0., 0.],\n",
              "        [0., 0., 0., 0., 0.],\n",
              "        [0., 0., 0., 0., 0.]],\n",
              "\n",
              "       [[0., 0., 0., 0., 0.],\n",
              "        [0., 0., 0., 0., 0.],\n",
              "        [0., 0., 0., 0., 0.]]])"
            ]
          },
          "metadata": {},
          "execution_count": 59
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a2 = np.ones((2,3))\n",
        "a2"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Qb_EMI-MSBO6",
        "outputId": "1aa25ff8-8703-4751-e674-e2ea0bd400d0"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[1., 1., 1.],\n",
              "       [1., 1., 1.]])"
            ]
          },
          "metadata": {},
          "execution_count": 60
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a1 + a2"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 141
        },
        "id": "M5vLeFKtSHUK",
        "outputId": "a091752d-15cb-49a7-87b5-3ccd719c9867"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "error",
          "ename": "ValueError",
          "evalue": "operands could not be broadcast together with shapes (2,3,5) (2,3) ",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-61-638078fd0cbc>\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma1\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0ma2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
            "\u001b[0;31mValueError\u001b[0m: operands could not be broadcast together with shapes (2,3,5) (2,3) "
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Arrays are mutable!!!"
      ],
      "metadata": {
        "id": "gwLrewQb-UbG"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "a = np.array([1, 2, 3])\n",
        "b = a\n",
        "b"
      ],
      "metadata": {
        "id": "Hyj-2dyM-bFE",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "f8731d48-4b01-46fc-93d9-eb01d52d2af3"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([1, 2, 3])"
            ]
          },
          "metadata": {},
          "execution_count": 62
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "b[1] = 11\n",
        "b"
      ],
      "metadata": {
        "id": "BmeaS5EZ-iTQ",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "4842e004-a7ca-48a0-a799-3ca1aa213749"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([ 1, 11,  3])"
            ]
          },
          "metadata": {},
          "execution_count": 63
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a"
      ],
      "metadata": {
        "id": "LB6-yPWq-nwc",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "f227b1de-f085-4c2d-a079-b931cf8aa83c"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([ 1, 11,  3])"
            ]
          },
          "metadata": {},
          "execution_count": 64
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "b = a.copy()\n",
        "b[1] = 111\n",
        "b"
      ],
      "metadata": {
        "id": "cnzrxKtg-pMq",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "a18602ff-c032-4f2a-a86a-39cd9fb76f51"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([  1, 111,   3])"
            ]
          },
          "metadata": {},
          "execution_count": 65
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a"
      ],
      "metadata": {
        "id": "3XZTfFwC_GlJ",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "43029e1a-7aeb-4238-98d8-8ac31256a837"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([ 1, 11,  3])"
            ]
          },
          "metadata": {},
          "execution_count": 66
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a+=2\n",
        "a"
      ],
      "metadata": {
        "id": "h0-AUyWsAxjt",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "d797732e-1b7e-4f90-d7c7-e2ad6a661165"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([ 3, 13,  5])"
            ]
          },
          "metadata": {},
          "execution_count": 67
        }
      ]
    }
  ]
}