{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# NumPy" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np # numpyのインポート" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## NumPy配列の生成" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1. 2. 3.]\n" ] } ], "source": [ "x = np.array([1.0, 2.0, 3.0])\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "numpy.ndarray" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## NumPyの算術計算" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "x = np.array([1.0, 2.0, 3.0])\n", "y = np.array([2.0, 4.0, 6.0])" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([3., 6., 9.])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x + y" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([-1., -2., -3.])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x - y" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 2., 8., 18.])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x * y # element-wise product" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0.5, 0.5, 0.5])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x / y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* x, yの要素数が同じ (要素数3の1次元配列)\n", "* 要素数が同じ場合、算術計算は各要素に対して行われる\n", "* 要素ごとは、英語で「element-wise」と呼ばれる" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0.5, 1. , 1.5])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = np.array([1.0, 2.0, 3.0])\n", "x / 2.0 # スカラ値を組み合わせた計算は、" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 🤔NumPyを使わないで配列を計算するとどうなるか" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "a = [0, 1, 2]\n", "b = [3, 4, 5]" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a + b # 行列の結合" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for -: 'list' and 'list'", "output_type": "error", "traceback": [ "\u001b[1;31m----------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m \u001b[1;33m-\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for -: 'list' and 'list'" ] } ], "source": [ "a - b" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "can't multiply sequence by non-int of type 'list'", "output_type": "error", "traceback": [ "\u001b[1;31m----------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m \u001b[1;33m*\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: can't multiply sequence by non-int of type 'list'" ] } ], "source": [ "a * b" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for /: 'list' and 'list'", "output_type": "error", "traceback": [ "\u001b[1;31m----------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m \u001b[1;33m/\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for /: 'list' and 'list'" ] } ], "source": [ "a / b" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a * 5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## NumPyのN次元配列" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2]\n", " [3 4]]\n" ] } ], "source": [ "A = np.array([[1, 2], [3, 4]])\n", "print(A)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2, 2)" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A.shape # 行列Aの形状。つまりこれは2次元配列であることを示す" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dtype('int32')" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A.dtype # 要素のデータ型" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 4, 2],\n", " [ 3, 10]])" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B = np.array([[3, 0], [0, 6]])\n", "A + B" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[10, 20],\n", " [30, 40]])" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A * 10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ブロードキャスト" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* 2次元配列とスカラ値の掛け算では、スカラ値が2次元配列に変換された後に計算される" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[10, 40],\n", " [30, 80]])" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = np.array([[1, 2], [3, 4]]) # ((A1, A2), (A3, A4))\n", "B = np.array([10, 20]) # (B1, B2)\n", "A * B # = ((A1 * B1, A2 * B2), (A3 * B1, A4 * B2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 要素へのアクセス" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[51 55]\n", " [14 19]\n", " [ 0 4]]\n" ] } ], "source": [ "X = np.array([[51, 55], [14, 19], [0, 4]])\n", "print(X)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([51, 55])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X[0]" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "55" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X[0][1]" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[51 55]\n", "[14 19]\n", "[0 4]\n" ] } ], "source": [ "for row in X:\n", " print(row)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[51 55 14 19 0 4]\n" ] } ], "source": [ "X = X.flatten() # Xを1次元配列へ変換\n", "print(X)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([51, 14, 0])" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X[np.array([0, 2, 4])] # インデックスが0, 2, 4番目の要素を取得" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ True, True, False, True, False, False])" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X > 15 # Xが15以上である要素の結果" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([51, 55, 19])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X[X > 15] # Xが15以上の要素のみを抽出" ] } ], "metadata": { "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.8.3" } }, "nbformat": 4, "nbformat_minor": 4 }