{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "zOyOW4RvNkN6"
      },
      "source": [
        "# numpy入門"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "tEVLu5LDf59P"
      },
      "source": [
        "## 達成目標\n",
        "基本的な行列作成、演算、列参照できるようになろう。\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "3-90Fe2lgFmJ"
      },
      "source": [
        "## 参考サイト\n",
        "- [Numpy公式チュートリアル](https://numpy.org/doc/stable/user/quickstart.html)\n",
        "- [NumPy Cheat Sheet: Data Analysis in Python](https://www.datacamp.com/blog/numpy-cheat-sheet-data-analysis-in-python)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "4DIvJWJXjL20"
      },
      "source": [
        "## チュートリアル"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "aGXTMMs8NgxS",
        "outputId": "312bef39-5a9e-4132-e142-cccf50e0f1be"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "[[1 2 3]\n",
            " [4 5 6]]\n",
            "<class 'numpy.ndarray'>\n",
            "(2, 3)\n"
          ]
        }
      ],
      "source": [
        "import numpy as np\n",
        "\n",
        "# 行列の作成\n",
        "a = np.array([[1,2,3], [4,5,6]])\n",
        "print(a)\n",
        "print(type(a))\n",
        "print(a.shape)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "b4AnjxZVPkW9",
        "outputId": "ed38443d-4738-4696-9757-7204c0b82961"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([1, 2, 3])"
            ]
          },
          "execution_count": 2,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "#行の参照\n",
        "a[0]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "f5WTu5vsPoaM",
        "outputId": "b0854cb4-c1cd-4589-ade5-28310bc321c0"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([1, 4])"
            ]
          },
          "execution_count": 3,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "#列の参照\n",
        "a[:,0]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ioebfqDtPron",
        "outputId": "d6898ce7-0876-42d3-f008-521c2eb813d9"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([[1, 2, 3],\n",
              "       [4, 5, 6]])"
            ]
          },
          "execution_count": 4,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "#スライス指定も可能\n",
        "a[0:2]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "OejQdfapPuTs",
        "outputId": "3a0d67c5-5340-45c1-d944-c3519fb6f380"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([[1, 2],\n",
              "       [4, 5]])"
            ]
          },
          "execution_count": 5,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "a[:,0:2]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "lo7xrxAXN2vU",
        "outputId": "62da90c1-6fa7-4e44-f789-fadbb18368d7"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([[2, 3, 4],\n",
              "       [5, 6, 7]])"
            ]
          },
          "execution_count": 6,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# 「行列 + 1」は全要素に対する和を実行\n",
        "a + 1"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "-j8BnkHYN-Md",
        "outputId": "b07c75b3-4142-4d4e-f12b-2f24738dac34"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([[ 2,  4,  6],\n",
              "       [ 8, 10, 12]])"
            ]
          },
          "execution_count": 7,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# *演算子も同様。\n",
        "a * 2"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "FgY-VZMMOD8a",
        "outputId": "7b1bb3b0-2d64-472b-eae1-a5c0567e95d9"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([[ 1,  4,  9],\n",
              "       [16, 25, 36]])"
            ]
          },
          "execution_count": 8,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "#行列演算ではない！\n",
        "a * a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "CcPzjIcMOB8I",
        "outputId": "bba71330-4fc2-4b29-b49b-ce18bc721ecd"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([[1, 4],\n",
              "       [2, 5],\n",
              "       [3, 6]])"
            ]
          },
          "execution_count": 9,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "#転置行列\n",
        "a.T"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "JtsJhxWdOIeY",
        "outputId": "3aaa6e99-1ef1-4862-cc5b-5ad4cb1e60a0"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([[14, 32],\n",
              "       [32, 77]])"
            ]
          },
          "execution_count": 10,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "#内積を求めるにはdot関数を使う\n",
        "np.dot(a, a.T)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "PJOJFvUBOKui",
        "outputId": "b8908cf3-afa9-43a2-a72b-993c26791500"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([[ 1.42592593, -0.59259259],\n",
              "       [-0.59259259,  0.25925926]])"
            ]
          },
          "execution_count": 11,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "#逆行列\n",
        "np.linalg.inv(np.dot(a, a.T))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "by0xPzqUOVMz",
        "outputId": "cff330eb-9a67-4702-acf9-db1560fbfaf3"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "[[0. 0. 0.]\n",
            " [0. 0. 0.]]\n",
            "[[1. 1. 1.]\n",
            " [1. 1. 1.]]\n",
            "[[1. 0. 0.]\n",
            " [0. 1. 0.]\n",
            " [0. 0. 1.]]\n"
          ]
        }
      ],
      "source": [
        "#ゼロ行列、1行列、対角行列\n",
        "print(np.zeros((2,3)))\n",
        "\n",
        "print(np.ones((2,3)))\n",
        "\n",
        "print(np.eye(3))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "vSzfhXAdOc6C",
        "outputId": "5414f32c-7071-4217-f178-5f0cf00293fe"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([0. , 0.3, 0.6, 0.9])"
            ]
          },
          "execution_count": 13,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "#特定範囲内で幅を指定してサンプル点を用意。\n",
        "#例えば、\n",
        "# 「y=x**2」のグラフを描画したいとき、\n",
        "#　定義域「-10〜10の範囲で0.1刻みでサンプル点を用意」みたいなときに便利。\n",
        "np.arange(0, 1, 0.3)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "cZEqbhjQPAZ0",
        "outputId": "d0377724-eb0f-4e17-b94d-790f74cac25d"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([0, 1, 2, 3, 4, 5, 6, 7])"
            ]
          },
          "execution_count": 14,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "#np.arangeで始点、刻み幅を省略すると0から指定個数の整数を用意。\n",
        "np.arange(8)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "_UFbdjmUPO8k",
        "outputId": "fd0f3610-86ab-4939-cad7-911d0b9fefd4"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([[0, 1, 2],\n",
              "       [3, 4, 5]])"
            ]
          },
          "execution_count": 15,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "#行列の形を変形できる。\n",
        "np.reshape(np.arange(6),(2,3))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Yi3pLN6SPXTF",
        "outputId": "7d586a76-fff0-49ac-ea75-09a38bc12c84"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([0., 1., 2.])"
            ]
          },
          "execution_count": 16,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "#刻み幅はどうでも良いからサンプル数を指定したい場合に便利。\n",
        "np.linspace(0,2,3)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "3toSvv6VPeRv",
        "outputId": "67cbe055-5f1f-4b86-fb7e-4edd1d4f2760"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([0.        , 0.66666667, 1.33333333, 2.        ])"
            ]
          },
          "execution_count": 17,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "np.linspace(0,2,4)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "K1-31QgpPy7W",
        "outputId": "1c7b5dea-9ce0-4af3-b3c8-4f09d0638b8c"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([[ 1,  2,  3],\n",
              "       [ 4,  5,  6],\n",
              "       [ 7,  8,  9],\n",
              "       [10, 11, 12]])"
            ]
          },
          "execution_count": 18,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "#行列を結合できる。\n",
        "#縦方向に結合\n",
        "a = np.array([[1,2,3], [4,5,6]])\n",
        "b = np.array([[7,8,9], [10,11,12]])\n",
        "np.r_[a, b]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Yi-_Je6SP4Jx",
        "outputId": "44368112-3d06-4661-bb1e-76e17bc620ba"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([[ 1,  2,  3,  7,  8,  9],\n",
              "       [ 4,  5,  6, 10, 11, 12]])"
            ]
          },
          "execution_count": 19,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "#横方向に結合\n",
        "np.c_[a, b]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "tf6jJke3XrOG"
      },
      "source": [
        "## 演習演習\n",
        "- ベクトル演算の演習\n",
        "  - (1) 5個の要素を持つ列ベクトルを作成せよ。値は全て1とする。\n",
        "  - (2) 1で作成した列ベクトルのうち、2番目の要素を3.14に更新せよ。なおインデックスは0から数える（0番目、1番目、2番目、、）ものとする。\n",
        "  - (3) 2で作成した列ベクトルを複製し、転置により行ベクトルに変換せよ。\n",
        "  - (4) 用意した列ベクトルと行ベクトルの内積を求めよ。\n",
        "  - (5) [np.random.rand](https://numpy.org/doc/stable/reference/random/generated/numpy.random.rand.html)を用いて、10個の要素を持つ列ベクトルを作成せよ。\n",
        "- 行列演算の演習行列演算の演習\n",
        "  - (6) [np.random.normal](https://numpy.org/doc/stable/reference/random/generated/numpy.random.normal.html)を用いて、平均値10、標準偏差2の正規分布に基づく、2行5列の行列を作成せよ。\n",
        "  - (7) 6で作成した行列から、2列目の要素を抜き出だせ。\n",
        "  - (8) 6で作成した行列から、3列目と4列目の要素を抜き出せ。\n",
        "  - (9) np.random.randで5行2列の行列を用意し、6で用意した行列との積を求めよ。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "AHZXG7gpgh6M"
      },
      "outputs": [],
      "source": []
    }
  ],
  "metadata": {
    "colab": {
      "collapsed_sections": [],
      "name": "intro_numpy.ipynb",
      "provenance": [],
      "toc_visible": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
