{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 12. 넘파이 패키지" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 12.1\tarray, numpy, matplotlib 패키지 개요" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 12.1.1 array 패키지" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 12.1.2 numpy 패키지" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 12.1.3 numpy.random 패키지" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 12.1.4 matplotlib 패키지" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 12.2\t“array” 패키지 사용하기\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](./images_skill_up/12-1.PNG)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- array 패키지\n", " - 개념적으로 numpy 작동 원리의 기반을 제공\n", " - 1차원 배열만 지원\n", " - python을 설치할 때 기본적으로 존재하는 패키지\n", " \n", " \n", "- 일반적인 array(배열)의 특징\n", " - 인덱스(색인 번호)로 각 항목을 참조\n", " - 고정 길이 데이터 구조\n", " - 데이터의 모든 항목이 메모리상에 연속적으로 서로 옆에 존재" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](./images_skill_up/12-2.PNG)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "

형 코드

C 형

파이썬 형

사이즈(바이트)

'b'

signed char

int

1

'B'

unsigned char

int

1

'u'

wchar_t

유니코드 문자

2

'h'

signed short

int

2

'H'

unsigned short

int

2

'i'

signed int

int

2

'I'

unsigned int

int

2

'l'

signed long

int

4

'L'

unsigned long

int

4

'q'

signed long long

int

8

'Q'

unsigned long long

int

8

'f'

float

float

4

'd'

double

float

8

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- array vs. list" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import array" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array('h', [1, 2, 3])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = array.array('h', [1, 2, 3])\n", "a" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array('h', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = array.array('h', range(100))\n", "a" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "ename": "OverflowError", "evalue": "signed short integer is greater than maximum", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mOverflowError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0marray\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'h'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1_000_000\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[0m", "\u001b[0;31mOverflowError\u001b[0m: signed short integer is greater than maximum" ] } ], "source": [ "a = array.array('h', range(1_000_000))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "a = array.array('l', range(1_000_000))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 12.3\tnumpy 패키지를 내려받고 탑재하기\n", "``` pip install numpy ```\n", "\n", "or\n", "\n", "``` pip download numpy ```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 12.4 numpy 소개하기: 1부터 100만까지 더하기" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- numpy에 의하여 생성되는 모든 데이터들의 타입은 ndarray 라고 함\n", " - ndarray: n-dimensional array" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "500000500000\n" ] } ], "source": [ "a_list = list(range(1, 1_000_001))\n", "print(sum(a_list))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "500000500000\n" ] } ], "source": [ "a = np.arange(1, 1_000_001)\n", "print(sum(a))" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from time import time\n", "\n", "def benchmarks(n):\n", " t1 = time()\n", " a_list = list(range(1, n + 1)) \t# Old style!\n", " tot = sum(a_list)\n", " t2 = time()\n", " elapsed_time_1 = t2 - t1\n", " print('Time taken by Python is {0:6.4f}'.format(elapsed_time_1))\n", "\n", " t1 = time()\n", " a = np.arange(1, n + 1) \t\t# Numpy!\n", " tot = np.sum(a)\n", " t2 = time()\n", " elapsed_time_2 = t2 - t1 \n", " print('Time taken by numpy is {0:6.4f}'.format(t2 - t1))\n", " \n", " print('{0:5.3f}배의 시간 차이 발생!!!'.format(elapsed_time_1 / elapsed_time_2)) " ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Time taken by Python is 0.3095\n", "Time taken by numpy is 0.0445\n", "6.949배의 시간 차이 발생!!!\n" ] } ], "source": [ "benchmarks(10_000_000)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 12.5\tnumpy 배열 만들기" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](./tables_skill_up/t1201.PNG)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](./tables_skill_up/t1202.PNG)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 12.5.1 array 함수 (array로 변환)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- ```numpy.array``` is a function that returns a ```numpy.ndarray```. \n", " - There is no object type numpy.array.\n", "\n" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1차원 ndarray 생성" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3]\n", "\n" ] } ], "source": [ "a = np.array([1, 2, 3], dtype=np.int64)\n", "print(a)\n", "print(type(a))" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--------------------------------------------------------------------------------\n", " a.T: [1 2 3]\n", " a.data: \n", " a.dtype: int64\n", " a.flat: \n" ] } ], "source": [ "print(\"-\" * 80)\n", "\n", "print(\"{0:>10}: {1}\".format(\"a.T\", a.T))\n", "print(\"{0:>10}: {1}\".format(\"a.data\", a.data))\n", "print(\"{0:>10}: {1}\".format(\"a.dtype\", a.dtype))\n", "print(\"{0:>10}: {1}\".format(\"a.flat\", a.flat)) # A 1-D iterator over the array" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " a.imag: [0 0 0]\n", " a.real: [1 2 3]\n" ] } ], "source": [ "print(\"{0:>10}: {1}\".format(\"a.imag\", a.imag))\n", "print(\"{0:>10}: {1}\".format(\"a.real\", a.real))" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " a.size: 3\n", "a.itemsize: 8\n", " a.nbytes: 24\n" ] } ], "source": [ "print(\"{0:>10}: {1}\".format(\"a.size\", a.size))\n", "print(\"{0:>10}: {1}\".format(\"a.itemsize\", a.itemsize)) # Length of one array element in bytes\n", "print(\"{0:>10}: {1}\".format(\"a.nbytes\", a.nbytes)) # Number of elements in the array" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 중요!!! (암기해야할 속성: ndim, shape)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " a.ndim: 1\n", " a.shape: (3,)\n" ] } ], "source": [ "print(\"{0:>10}: {1}\".format(\"a.ndim\", a.ndim)) # Number of array dimensions.\n", "print(\"{0:>10}: {1}\".format(\"a.shape\", a.shape)) # Tuple of array dimensions." ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " a.strides: (8,)\n", " a.ctypes: \n", " a.base: None\n", " a.flags:==>\n", " C_CONTIGUOUS : True\n", " F_CONTIGUOUS : True\n", " OWNDATA : True\n", " WRITEABLE : True\n", " ALIGNED : True\n", " WRITEBACKIFCOPY : False\n", " UPDATEIFCOPY : False\n" ] } ], "source": [ "print(\"{0:>10}: {1}\".format(\"a.strides\", a.strides)) # Tuple of bytes to step in each dimension when traversing an array.\n", "print(\"{0:>10}: {1}\".format(\"a.ctypes\", a.ctypes)) # An object to simplify the interaction of the array with the ctypes module\n", "print(\"{0:>10}: {1}\".format(\"a.base\", a.base)) # Base object if memory is from some other object\n", "print(\"{0:>10}:==>\\n{1}\".format(\"a.flags\", a.flags))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2차원 ndarray 생성" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1 2 3]\n", " [10 20 30]\n", " [ 0 0 -1]]\n", "\n" ] } ], "source": [ "a = np.array([[1, 2, 3], [10, 20, 30], [0, 0, -1]])\n", "print(a)\n", "print(type(a))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--------------------------------------------------------------------------------\n", " a.T: [[ 1 10 0]\n", " [ 2 20 0]\n", " [ 3 30 -1]]\n", " a.data: \n", " a.dtype: int64\n", " a.flat: \n" ] } ], "source": [ "print(\"-\" * 80)\n", "print(\"{0:>10}: {1}\".format(\"a.T\", a.T))\n", "print(\"{0:>10}: {1}\".format(\"a.data\", a.data))\n", "print(\"{0:>10}: {1}\".format(\"a.dtype\", a.dtype))\n", "print(\"{0:>10}: {1}\".format(\"a.flat\", a.flat)) # A 1-D iterator over the array" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " a.imag: [[0 0 0]\n", " [0 0 0]\n", " [0 0 0]]\n", " a.real: [[ 1 2 3]\n", " [10 20 30]\n", " [ 0 0 -1]]\n" ] } ], "source": [ "print(\"{0:>10}: {1}\".format(\"a.imag\", a.imag))\n", "print(\"{0:>10}: {1}\".format(\"a.real\", a.real))" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " a.size: 9\n", "a.itemsize: 8\n", " a.nbytes: 72\n" ] } ], "source": [ "print(\"{0:>10}: {1}\".format(\"a.size\", a.size))\n", "print(\"{0:>10}: {1}\".format(\"a.itemsize\", a.itemsize)) # Length of one array element in bytes\n", "print(\"{0:>10}: {1}\".format(\"a.nbytes\", a.nbytes)) # Number of elements in the array" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 중요!!! (암기해야할 속성: ndim, shape)" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " a.ndim: 2\n", " a.shape: (3, 3)\n" ] } ], "source": [ "print(\"{0:>10}: {1}\".format(\"a.ndim\", a.ndim)) # Number of array dimensions.\n", "print(\"{0:>10}: {1}\".format(\"a.shape\", a.shape)) # Tuple of array dimensions." ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " a.strides: (24, 8)\n", " a.ctypes: \n", " a.base: None\n", " a.flags:==>\n", " C_CONTIGUOUS : True\n", " F_CONTIGUOUS : False\n", " OWNDATA : True\n", " WRITEABLE : True\n", " ALIGNED : True\n", " WRITEBACKIFCOPY : False\n", " UPDATEIFCOPY : False\n" ] } ], "source": [ "print(\"{0:>10}: {1}\".format(\"a.strides\", a.strides)) # Tuple of bytes to step in each dimension when traversing an array.\n", "print(\"{0:>10}: {1}\".format(\"a.ctypes\", a.ctypes)) # An object to simplify the interaction of the array with the ctypes module\n", "print(\"{0:>10}: {1}\".format(\"a.base\", a.base)) # Base object if memory is from some other object\n", "print(\"{0:>10}:==>\\n{1}\".format(\"a.flags\", a.flags))" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 2, 3],\n", " [ 10, 20, 300]])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array([[1, 2, 3], [10, 20, 300]])\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 가지런하지 않은 배열 형태로 생성될 때 각 원소는 object 타입 (실제로는 list)이 된다." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\gilbut\\anaconda3\\lib\\site-packages\\ipykernel_launcher.py:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray\n", " \"\"\"Entry point for launching an IPython kernel.\n" ] }, { "data": { "text/plain": [ "array([list([1, 2, 3]), list([10, 20, 300, 4])], dtype=object)" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array([[1, 2, 3], [10, 20, 300, 4]])\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 12.5.2 arange 함수" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- https://numpy.org/doc/stable/reference/generated/numpy.arange.html?highlight=arange#numpy.arange\n", "- range 내장함수와 거의 동일" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "a = np.arange(1, 1000001) \t# 100만개 항목으로 구성된 배열 생성" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 12.5.3\tlinspace 함수" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- https://numpy.org/doc/stable/reference/generated/numpy.linspace.html#numpy.linspace\n", " - num 디폴트값: 50" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0. , 0.25, 0.5 , 0.75, 1. ])" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "a = np.linspace(0, 1.0, num=5)\n", "a" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0. , 0.2, 0.4, 0.6, 0.8])" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "a = np.linspace(0, 1.0, num=5, endpoint=False)\n", "a" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0. , 0.2, 0.4, 0.6, 0.8, 1. ])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.linspace(0, 1.0, num=6)\n", "a" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4, 5], dtype=int16)" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.linspace(1, 5, num=5, dtype=np.int16)\n", "a" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 0, 0, 0, 0, 1], dtype=int16)" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.linspace(0, 1.0, num=6, dtype=np.int16)\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 12.5.4 empty 함수" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- https://numpy.org/doc/stable/reference/generated/numpy.empty.html\n", "- 초기화되지 않은 (결국 쓰레기값을 지닌) ndarray 생성" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 0],\n", " [ 0, 16368]], dtype=int16)" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "a = np.empty((2, 2), dtype='int16')\n", "a" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[3.3454865e+31, 8.6880505e-43],\n", " [0.0000000e+00, 0.0000000e+00],\n", " [1.8367379e-40, nan]], dtype=float32)" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.empty((3, 2), dtype='float32')\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 12.5.5\teye 함수\n", "- https://numpy.org/doc/stable/reference/generated/numpy.eye.html" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 0, 0, 0],\n", " [0, 1, 0, 0],\n", " [0, 0, 1, 0],\n", " [0, 0, 0, 1]])" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.eye(N=4, dtype='int')\n", "a" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1., 0., 0., 0., 0., 0.],\n", " [0., 1., 0., 0., 0., 0.],\n", " [0., 0., 1., 0., 0., 0.],\n", " [0., 0., 0., 1., 0., 0.],\n", " [0., 0., 0., 0., 1., 0.],\n", " [0., 0., 0., 0., 0., 1.]])" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.eye(N=6)\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 12.5.6 ones 함수\n", "- https://numpy.org/doc/stable/reference/generated/numpy.ones.html" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1., 1., 1.],\n", " [1., 1., 1.],\n", " [1., 1., 1.]])" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "a = np.ones(shape=(3, 3))\n", "a" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[[1, 1, 1],\n", " [1, 1, 1]],\n", "\n", " [[1, 1, 1],\n", " [1, 1, 1]]], dtype=int16)" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.ones(shape=(2, 2, 3), dtype=np.int16)\n", "a" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ True, True, True, True, True, True])" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.ones(shape=6, dtype=np.bool)\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 12.5.7\tzeros 함수\n", "- https://numpy.org/doc/stable/reference/generated/numpy.zeros.html" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0., 0., 0.],\n", " [0., 0., 0.],\n", " [0., 0., 0.]])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "a = np.zeros((3,3))\n", "a" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[[0, 0, 0],\n", " [0, 0, 0]],\n", "\n", " [[0, 0, 0],\n", " [0, 0, 0]]], dtype=int16)" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.zeros((2, 2, 3), dtype=np.int16)\n", "a" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([False, False, False, False, False])" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.zeros(5, dtype=np.bool)\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 12.5.8 full 함수\n", "- https://numpy.org/doc/stable/reference/generated/numpy.full.html" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[3.14, 3.14],\n", " [3.14, 3.14]])" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "a = np.full((2, 2), 3.14)\n", "a" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([100, 100, 100, 100, 100, 100, 100, 100])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.full(8, 100)\n", "a" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['ken', 'ken', 'ken', 'ken', 'ken'], dtype='" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 3D ndarray을 생성할 때, shape=(i, j, k)의 의미는 다음과 같은 x축, y축, z축에 대하여 **(z축, y축, x축)**으로 대응된다. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 12.6\t예시: 곱셈표 만들기" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1 2 3 4 5 6 7 8 9 10]\n", " [ 2 4 6 8 10 12 14 16 18 20]\n", " [ 3 6 9 12 15 18 21 24 27 30]\n", " [ 4 8 12 16 20 24 28 32 36 40]\n", " [ 5 10 15 20 25 30 35 40 45 50]\n", " [ 6 12 18 24 30 36 42 48 54 60]\n", " [ 7 14 21 28 35 42 49 56 63 70]\n", " [ 8 16 24 32 40 48 56 64 72 80]\n", " [ 9 18 27 36 45 54 63 72 81 90]\n", " [ 10 20 30 40 50 60 70 80 90 100]]\n" ] } ], "source": [ "import numpy as np\n", "\n", "def multy(r, c):\n", " return (r + 1) * (c + 1)\n", "\n", "a = np.fromfunction(function=multy, shape=(10, 10), dtype=np.int16)\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1 2 3 4 5 6 7 8 9 10]\n", " [ 2 4 6 8 10 12 14 16 18 20]\n", " [ 3 6 9 12 15 18 21 24 27 30]\n", " [ 4 8 12 16 20 24 28 32 36 40]\n", " [ 5 10 15 20 25 30 35 40 45 50]\n", " [ 6 12 18 24 30 36 42 48 54 60]\n", " [ 7 14 21 28 35 42 49 56 63 70]\n", " [ 8 16 24 32 40 48 56 64 72 80]\n", " [ 9 18 27 36 45 54 63 72 81 90]\n", " [ 10 20 30 40 50 60 70 80 90 100]]\n" ] } ], "source": [ "a = np.fromfunction(function=lambda r, c: (r + 1) * (c + 1), shape=(10, 10), dtype=np.int16)\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "s = str(a)\n", "s = s.replace('[', '')\n", "s = s.replace(']', '')\n", "s = ' ' + s" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 1 2 3 4 5 6 7 8 9 10\n", " 2 4 6 8 10 12 14 16 18 20\n", " 3 6 9 12 15 18 21 24 27 30\n", " 4 8 12 16 20 24 28 32 36 40\n", " 5 10 15 20 25 30 35 40 45 50\n", " 6 12 18 24 30 36 42 48 54 60\n", " 7 14 21 28 35 42 49 56 63 70\n", " 8 16 24 32 40 48 56 64 72 80\n", " 9 18 27 36 45 54 63 72 81 90\n", " 10 20 30 40 50 60 70 80 90 100\n" ] } ], "source": [ "print(s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 12.7 numpy 배열의 배치 연산" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](./tables_skill_up/t1203.PNG)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](./tables_skill_up/t1204.PNG)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 3],\n", " [ 4, 5, 6, 7],\n", " [ 8, 9, 10, 11],\n", " [12, 13, 14, 15]])" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "\n", "A = np.array([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]])\n", "A" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 3],\n", " [ 4, 5, 6, 7],\n", " [ 8, 9, 10, 11],\n", " [12, 13, 14, 15]])" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = np.arange(16).reshape((4, 4))\n", "A" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0., 1., 2., 3.],\n", " [ 4., 5., 6., 7.],\n", " [ 8., 9., 10., 11.],\n", " [12., 13., 14., 15.]])" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = np.fromfunction(function=lambda r, c: r * 4 + c, shape=(4, 4))\n", "A" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 0 0 0]\n", " [0 1 0 0]\n", " [0 0 1 0]\n", " [0 0 0 1]]\n" ] } ], "source": [ "B = np.eye(4, dtype='int16')\n", "print(B)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0. 10. 20. 30.]\n", " [ 40. 50. 60. 70.]\n", " [ 80. 90. 100. 110.]\n", " [120. 130. 140. 150.]]\n" ] } ], "source": [ "C = A * 10\n", "print(C)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0. 1. 4. 9.]\n", " [ 16. 25. 36. 49.]\n", " [ 64. 81. 100. 121.]\n", " [144. 169. 196. 225.]]\n" ] } ], "source": [ "C = A * A\n", "print(C)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0. 1. 4. 9. 16. 25. 36. 49.]\n", " [ 64. 81. 100. 121. 144. 169. 196. 225.]]\n" ] } ], "source": [ "print(C.reshape((2, 8)))" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0. 1. 4. 9.]\n", " [ 16. 25. 36. 49.]\n", " [ 64. 81. 100. 121.]\n", " [144. 169. 196. 225.]]\n" ] } ], "source": [ "print(C)" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0., 1., 2., 3.],\n", " [ 4., 5., 6., 7.],\n", " [ 8., 9., 10., 11.],\n", " [12., 13., 14., 15.]])" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0. 0. 0. 0.]\n", " [ 0. 5. 0. 0.]\n", " [ 0. 0. 10. 0.]\n", " [ 0. 0. 0. 15.]]\n" ] } ], "source": [ "C = A * B\n", "print(C)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 12.8 numpy의 조각을 정렬하기" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 1 2 3 4 5 6 7 8 9 10]\n" ] } ], "source": [ "A = np.arange(1, 11)\n", "print(A)" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[3 4 5]\n" ] } ], "source": [ "print(A[2:5])" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 1 2 0 0 0 6 7 8 9 10]\n" ] } ], "source": [ "A[2:5] = 0\n", "print(A)" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 1 2 100 100 100 6 7 8 9 10]\n" ] } ], "source": [ "A[2:5] += 100\n", "print(A)" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1, 2, 300, 800, 1500, 6, 7, 8, 9, 10])" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = np.arange(1, 11)\n", "A[2:5] *= [100, 200, 300]\n", "A" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23\n", " 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47\n", " 48 49 50]\n" ] } ], "source": [ "A = np.arange(51)\n", "print(A)" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 0 0 2 3 0 5 0 7 0 9 0 11 0 13 0 15 0 17 0 19 0 21 0 23\n", " 0 25 0 27 0 29 0 31 0 33 0 35 0 37 0 39 0 41 0 43 0 45 0 47\n", " 0 49 0]\n" ] } ], "source": [ "A[1] = 0\n", "A[2 * 2::2] = 0\n", "print(A)" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 0 0 2 3 0 5 0 7 0 0 0 11 0 13 0 0 0 17 0 19 0 0 0 23\n", " 0 25 0 0 0 29 0 31 0 0 0 35 0 37 0 0 0 41 0 43 0 0 0 47\n", " 0 49 0]\n" ] } ], "source": [ "A[3 * 3::3] = 0\n", "print(A)" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 0 0 2 3 0 5 0 7 0 0 0 11 0 13 0 0 0 17 0 19 0 0 0 23\n", " 0 0 0 0 0 29 0 31 0 0 0 0 0 37 0 0 0 41 0 43 0 0 0 47\n", " 0 0 0]\n" ] } ], "source": [ "A[5 * 5::5] = 0\n", "A[7 * 7::7] = 0\n", "print(A)" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_prime_list = [i for i in A if i > 0]\n", "my_prime_list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Boolean Numpy Array\n", " - 임의의 Numpy 배열 A의 인덱싱 연산에 활용되어 A 자체에 마스크(mask)로 적용" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([False, False, True, True, False, True, False, True, False,\n", " False, False, True, False, True, False, False, False, True,\n", " False, True, False, False, False, True, False, False, False,\n", " False, False, True, False, True, False, False, False, False,\n", " False, True, False, False, False, True, False, True, False,\n", " False, False, True, False, False, False])" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A > 0" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47]\n" ] } ], "source": [ "P = A[A > 0]\n", "print(P)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 12.9 다차원 슬라이싱" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](./tables_skill_up/t1205.PNG)" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1 2 3 4]\n", " [ 5 6 7 8]\n", " [ 9 10 11 12]\n", " [13 14 15 16]]\n" ] } ], "source": [ "A = np.arange(1, 17).reshape((4, 4))\n", "print(A)" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 5 6 7 8]\n", " [ 9 10 11 12]]\n" ] } ], "source": [ "print(A[1:3])" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 2, 3],\n", " [ 6, 7],\n", " [10, 11],\n", " [14, 15]])" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[:, 1:3]" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0. 0. 0. 0. 0. 0.]\n", " [0. 0. 1. 0. 0. 0.]\n", " [0. 0. 1. 0. 0. 0.]\n", " [0. 0. 1. 0. 0. 0.]\n", " [0. 0. 0. 0. 0. 0.]\n", " [0. 0. 0. 0. 0. 0.]]\n" ] } ], "source": [ "G = np.zeros((6, 6))\n", "G[1:4, 2] = 1\n", "print(G)" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0. 1. 0.]\n", " [0. 1. 0.]\n", " [0. 1. 0.]]\n" ] } ], "source": [ "print(G[1:4, 1:4])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 12.10\t불리언 배열: 넘파이에 마스킹하기!" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2 3]\n", " [4 5 6]\n", " [7 8 9]]\n" ] } ], "source": [ "B = np.arange(1,10).reshape(3,3)\n", "print(B)" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[False, False, False],\n", " [False, True, True],\n", " [ True, True, True]])" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B1 = B > 4\n", "B1" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0 0 0]\n", " [0 5 6]\n", " [7 8 9]]\n" ] } ], "source": [ "print(B * (B > 4))" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[8 9]\n" ] } ], "source": [ "print(B[B > 7])" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 4 7]\n" ] } ], "source": [ "print(B[B % 3 == 1])" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[False False True]\n", " [ True True True]\n", " [False False False]]\n" ] } ], "source": [ "B2 = (B > 2) & (B < 7) \t\t# \"AND\" 연산\n", "print(B2)" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2 3]\n", " [4 5 6]\n", " [7 8 9]]\n" ] } ], "source": [ "print(B)" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[3 4 5 6]\n" ] } ], "source": [ "print(B[B2])" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[False False False]\n", " [ True True True]\n", " [ True True True]]\n" ] } ], "source": [ "B3 = B > 3\n", "print(B3)" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[4 5 6 7 8 9]\n" ] } ], "source": [ "print(B[B3])" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 7 8 9]\n" ] } ], "source": [ "print(B[ (B == 1) | (B > 6)]) \t# ‘OR’ 연산자" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 12.11 numpy와 에라토스테네스의 체\n", "" ] }, { "cell_type": "code", "execution_count": 114, "metadata": {}, "outputs": [], "source": [ "def sieve(n):\n", " b_list = [True] * (n + 1)\n", " b_list[0:2] = [False, False] # 일반 파이썬 리스트에서 슬라이싱 할당은 동일 사이즈로만 가능\n", " for i in range(2, n+1):\n", " if b_list[i]:\n", " for j in range(i*i, n+1, i):\n", " b_list[j] = False\n", " primes = [i for i in range(2, n+1) if b_list[i]]\n", " return primes" ] }, { "cell_type": "code", "execution_count": 115, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]" ] }, "execution_count": 115, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sieve(50)" ] }, { "cell_type": "code", "execution_count": 116, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "def np_sieve(n):\n", " # B 생성, 모든 항목을 True로 설정한다.\n", " B = np.ones(n + 1, dtype=np.bool)\n", " B[0:2] = False\n", " for i in range(2, n + 1):\n", " if B[i]:\n", " B[i*i: n+1: i] = False\n", " return np.arange(n + 1)[B]" ] }, { "cell_type": "code", "execution_count": 117, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47])" ] }, "execution_count": 117, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np_sieve(50)" ] }, { "cell_type": "code", "execution_count": 118, "metadata": {}, "outputs": [], "source": [ "import time" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 1000까지 소수 검출 속도 비교" ] }, { "cell_type": "code", "execution_count": 119, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "sieve took 0.22509765625 milliseconds.\n" ] } ], "source": [ "t1 = time.time() * 1000\n", "sieve(1_000)\n", "t2 = time.time() * 1000\n", "print('sieve took', t2-t1, 'milliseconds.')" ] }, { "cell_type": "code", "execution_count": 120, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "np_sieve took 0.264892578125 milliseconds.\n" ] } ], "source": [ "t1 = time.time() * 1000\n", "np_sieve(1_000)\n", "t2 = time.time() * 1000\n", "print('np_sieve took', t2-t1, 'milliseconds.')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 10000000 까지 소수 검출 속도 비교" ] }, { "cell_type": "code", "execution_count": 121, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "sieve took 2546.77978515625 milliseconds.\n" ] } ], "source": [ "t1 = time.time() * 1000\n", "sieve(10_000_000)\n", "t2 = time.time() * 1000\n", "print('sieve took', t2-t1, 'milliseconds.')" ] }, { "cell_type": "code", "execution_count": 122, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "np_sieve took 896.49658203125 milliseconds.\n" ] } ], "source": [ "t1 = time.time() * 1000\n", "np_sieve(10_000_000)\n", "t2 = time.time() * 1000\n", "print('np_sieve took', t2-t1, 'milliseconds.')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 12.12 numpy 통계 구하기 – 표준 편차" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](./tables_skill_up/t1206.PNG)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- https://numpy.org/doc/stable/reference/random/generated/numpy.random.rand.html" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0.33973737 0.16864527 0.39474274 0.42710631 0.86438432 0.67152607\n", " 0.00672403 0.1244622 0.13069404 0.80049644]\n", "(10,)\n" ] } ], "source": [ "import numpy as np\n", "import numpy.random as ran\n", "A = ran.rand(10)\n", "print(A)\n", "print(A.shape)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.90879311 0.61971292 0.16838488]\n", " [0.10881478 0.69499699 0.07213863]\n", " [0.57432973 0.93496149 0.17358513]]\n", "(3, 3)\n" ] } ], "source": [ "A = ran.rand(3, 3)\n", "print(A)\n", "print(A.shape)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[[0.47952324 0.50679153 0.38142121]\n", " [0.18419655 0.98121221 0.35026395]\n", " [0.66970099 0.80612869 0.46596528]]\n", "\n", " [[0.41454809 0.39191325 0.52940041]\n", " [0.23523159 0.26554211 0.40562865]\n", " [0.14930301 0.94267647 0.96713257]]\n", "\n", " [[0.42923939 0.36459349 0.31501216]\n", " [0.36935727 0.25893938 0.42838482]\n", " [0.23286462 0.98129982 0.1953949 ]]]\n", "(3, 3, 3)\n" ] } ], "source": [ "A = ran.rand(3, 3, 3)\n", "print(A)\n", "print(A.shape)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.5018404494269068" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "import numpy.random as ran\n", "A = ran.random(100_000)\n", "np.mean(A)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "50184.04494269068" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sum(A)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.5033012753071466" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.median(A)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.28889654605996534" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.std(A)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[[0.37179968 0.04861112 0.75513961]\n", " [0.87322301 0.3392253 0.51569713]\n", " [0.17456649 0.37514481 0.04819507]]\n", "\n", " [[0.35481209 0.3042816 0.71001006]\n", " [0.58083898 0.88110883 0.56928942]\n", " [0.41816889 0.92267971 0.56071293]]\n", "\n", " [[0.22542938 0.57968216 0.08112255]\n", " [0.77211739 0.54911936 0.86319039]\n", " [0.86337074 0.45191001 0.4027625 ]]]\n", "(3, 3, 3)\n", "0.5034151561065233\n", "13.59220921487613\n", "0.5156971294015305\n", "0.2606589044496662\n" ] } ], "source": [ "A = ran.rand(3, 3, 3)\n", "print(A)\n", "print(A.shape)\n", "print(np.mean(A))\n", "print(np.sum(A))\n", "print(np.median(A))\n", "print(np.std(A))" ] }, { "cell_type": "code", "execution_count": 113, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python took 0.42522501945495605\n", "Numpy took 0.002130746841430664\n", "np.std took 0.005688905715942383\n" ] } ], "source": [ "import numpy as np\n", "import time\n", "import numpy.random as ran\n", "\n", "def get_std1(ls):\n", " t1 = time.time()\n", " m = sum(ls)/len(ls)\n", " ls2 = [(i - m) ** 2 for i in ls]\n", " sd = (sum(ls2)/len(ls2)) ** .5\n", " t2 = time.time()\n", " print('Python took', t2-t1)\n", "\n", "def get_std2(A):\n", " t1 = time.time()\n", " A2 = (A - np.mean(A)) ** 2\n", " result = (np.mean(A2)) ** .5\n", " t2 = time.time()\n", " print('Numpy took', t2-t1)\n", "\n", "def get_std3(A):\n", " t1 = time.time()\n", " result = np.std(A)\n", " t2 = time.time()\n", " print('np.std took', t2-t1)\n", "\n", "A = ran.rand(1_000_000)\n", "get_std1(A)\n", "get_std2(A)\n", "get_std3(A)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 12.13 numpy 행과 열 가져오기" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### numpy.random.randint\n", "- https://numpy.org/doc/stable/reference/random/generated/numpy.random.randint.html\n", " - Return random integers from low (inclusive) to high (exclusive)." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[16 9 16 19]\n", " [18 18 3 11]\n", " [14 18 17 3]]\n" ] } ], "source": [ "import numpy as np\n", "import numpy.random as ran\n", "A = ran.randint(low=1, high=20, size=(3, 4))\n", "print(A)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "13.5" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.mean(A)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "162" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sum(A)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5.5" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.std(A) \t\t# 표준 편차" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0 1 2 3 4]\n", " [0 1 2 3 4]\n", " [0 1 2 3 4]\n", " [0 1 2 3 4]]\n" ] } ], "source": [ "B = np.fromfunction(lambda r,c: c, (4, 5), dtype=np.int32)\n", "print(B)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- sum() & axis\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](./images_skill_up/12-3.PNG)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](./tables_skill_up/t1207.PNG)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Axis or axes along which a sum is performed." ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0, 4, 8, 12, 16])" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sum(B, axis=0) \t\t# 행 기준 합계 --> 각 열의 합계 --> 연산 결과 벡터 내 원소 개수는 열의 개수와 동일" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([10, 10, 10, 10])" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sum(B, axis=1) \t\t# 열 기준 합계 --> 각 행의 합계 --> 연산 결과 벡터 내 원소 개수는 행의 개수와 동일" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 2차원 배열에 1차원 배열을 새로운 열(column)로 붙이기\n", " - np.c_" ] }, { "cell_type": "code", "execution_count": 141, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 1, 2, 3, 4],\n", " [0, 1, 2, 3, 4],\n", " [0, 1, 2, 3, 4],\n", " [0, 1, 2, 3, 4]], dtype=int32)" ] }, "execution_count": 141, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([10, 10, 10, 10])" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B_sum_col = np.sum(B, axis=1)\n", "B_sum_col" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(4, 5) (4,)\n" ] } ], "source": [ "print(B.shape, B_sum_col.shape)" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0 1 2 3 4 10]\n", " [ 0 1 2 3 4 10]\n", " [ 0 1 2 3 4 10]\n", " [ 0 1 2 3 4 10]]\n" ] } ], "source": [ "B1 = np.c_[B, B_sum_col]\n", "print(B1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 2차원 배열에 1차원 배열을 새로운 행(row)으로 붙이기\n", " - np.r_" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0, 4, 8, 12, 16, 40])" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B_sum_row = np.sum(B1, axis=0)\n", "B_sum_row" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(4, 6) (6,)\n" ] } ], "source": [ "print(B1.shape, B_sum_row.shape)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0 1 2 3 4 10]\n", " [ 0 1 2 3 4 10]\n", " [ 0 1 2 3 4 10]\n", " [ 0 1 2 3 4 10]\n", " [ 0 4 8 12 16 40]]\n" ] } ], "source": [ "B2 = np.r_[B1, [B_sum_row]]\n", "print(B2)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [], "source": [ "def spreadsheet(A):\n", " AC = np.sum(A, axis=1)\n", " A2 = np.c_[A, AC]\n", " AR = np.sum(A2, axis=0)\n", " return np.r_[A2, [AR]]" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0 1 2 3 4]\n", " [ 5 6 7 8 9]\n", " [10 11 12 13 14]]\n" ] } ], "source": [ "arr = np.arange(15).reshape(3, 5)\n", "print(arr)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0 1 2 3 4 10]\n", " [ 5 6 7 8 9 35]\n", " [ 10 11 12 13 14 60]\n", " [ 15 18 21 24 27 105]]\n" ] } ], "source": [ "print(spreadsheet(arr))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- hstack, vstack" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(array([[0, 1, 2, 3, 4],\n", " [0, 1, 2, 3, 4],\n", " [0, 1, 2, 3, 4],\n", " [0, 1, 2, 3, 4]], dtype=int32),\n", " (4, 5))" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B, B.shape" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([10, 10, 10, 10])" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B_sum_col = np.sum(B, axis=1)\n", "B_sum_col" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "((4, 5), (4,))" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B.shape, B_sum_col.shape" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [], "source": [ "B_sum_col = np.expand_dims(B_sum_col, axis=1)" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "((4, 5), (4, 1))" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B.shape, B_sum_col.shape" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(array([[ 0, 1, 2, 3, 4, 10],\n", " [ 0, 1, 2, 3, 4, 10],\n", " [ 0, 1, 2, 3, 4, 10],\n", " [ 0, 1, 2, 3, 4, 10]]),\n", " (4, 6))" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B1 = np.hstack([B, B_sum_col])\n", "B1, B1.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0, 4, 8, 12, 16, 40])" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B_sum_row = np.sum(B1, axis=0)\n", "B_sum_row" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "((4, 6), (6,))" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B1.shape, B_sum_row.shape" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [], "source": [ "B_sum_row = np.expand_dims(B_sum_row, axis=0)" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "((4, 6), (1, 6))" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B1.shape, B_sum_row.shape" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(array([[ 0, 1, 2, 3, 4, 10],\n", " [ 0, 1, 2, 3, 4, 10],\n", " [ 0, 1, 2, 3, 4, 10],\n", " [ 0, 1, 2, 3, 4, 10],\n", " [ 0, 4, 8, 12, 16, 40]]),\n", " (5, 6))" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B2 = np.vstack([B1, B_sum_row])\n", "B2, B2.shape" ] } ], "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.8" } }, "nbformat": 4, "nbformat_minor": 4 }