From 9111e232f11dc9e48129b8284d317c0aeb5a5dcf Mon Sep 17 00:00:00 2001 From: Young Woo Kim <skysaver00@ajou.ac.kr> Date: Sun, 20 Dec 2020 16:14:40 +0900 Subject: [PATCH] Upload New File --- .../Numpy_\354\230\210\354\240\234.ipynb" | 893 ++++++++++++++++++ 1 file changed, 893 insertions(+) create mode 100644 "Python Numpy, Pandas \354\230\210\354\240\234\355\214\214\354\235\274/Numpy_\354\230\210\354\240\234.ipynb" diff --git "a/Python Numpy, Pandas \354\230\210\354\240\234\355\214\214\354\235\274/Numpy_\354\230\210\354\240\234.ipynb" "b/Python Numpy, Pandas \354\230\210\354\240\234\355\214\214\354\235\274/Numpy_\354\230\210\354\240\234.ipynb" new file mode 100644 index 0000000..92b7197 --- /dev/null +++ "b/Python Numpy, Pandas \354\230\210\354\240\234\355\214\214\354\235\274/Numpy_\354\230\210\354\240\234.ipynb" @@ -0,0 +1,893 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.19.3\n", + "1.1.5\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import pandas as pd\n", + "print (np.__version__)\n", + "print (pd.__version__)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#np array 만들기\n", + "#여기서부터는 array = 배열로 취급" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5]\n", + "[10, 20, 30, 40, 50]\n" + ] + }, + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "one = [1,2,3,4,5]\n", + "print (one)\n", + "ten = [10,20,30,40,50]\n", + "print (ten)\n", + "\n", + "type (one)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1 2 3 4 5]\n", + "[10 20 30 40 50]\n" + ] + }, + { + "data": { + "text/plain": [ + "numpy.ndarray" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "onearr = np.array(one)\n", + "tenarr = np.array(ten)\n", + "print (onearr)\n", + "print (tenarr)\n", + "\n", + "type (onearr)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dtype('int32')" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "onearr.dtype" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(5,)" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "onearr.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(4, 3)" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr2nd = np.array([[1,0,0],[0,1,0],[0,0,1],[1,1,1]])\n", + "arr2nd.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "ename": "AttributeError", + "evalue": "'list' object has no attribute 'dtype'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m<ipython-input-7-81347d222ef3>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mone\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdtype\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mAttributeError\u001b[0m: 'list' object has no attribute 'dtype'" + ] + } + ], + "source": [ + "one.dtype" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[11 22 33 44 55]\n", + "[1, 2, 3, 4, 5, 10, 20, 30, 40, 50]\n" + ] + } + ], + "source": [ + "sumarr = onearr + tenarr\n", + "sumlist = one + ten\n", + "print (sumarr)\n", + "print (sumlist)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#기본 배열 만드는 함수 -> 빠르게 배열 만들기" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0. 0. 0.]\n", + "[[0. 0. 0.]\n", + " [0. 0. 0.]\n", + " [0. 0. 0.]\n", + " [0. 0. 0.]\n", + " [0. 0. 0.]]\n" + ] + } + ], + "source": [ + "arr1 = np.zeros(3)\n", + "arr2 = np.zeros((5, 3))\n", + "\n", + "print (arr1)\n", + "print (arr2)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1. 1. 1.]\n", + "[[1. 1. 1.]\n", + " [1. 1. 1.]\n", + " [1. 1. 1.]\n", + " [1. 1. 1.]\n", + " [1. 1. 1.]]\n" + ] + } + ], + "source": [ + "arr1 = np.ones(3)\n", + "arr2 = np.ones((5, 3))\n", + "\n", + "print (arr1)\n", + "print (arr2)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1.]]\n", + "[[1. 0.]\n", + " [0. 1.]]\n" + ] + } + ], + "source": [ + "arr1 = np.identity(1)\n", + "arr2 = np.identity(2)\n", + "\n", + "print (arr1)\n", + "print (arr2)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "scrolled": true + }, + "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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71\n", + " 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95\n", + " 96 97 98 99]\n", + "[96 97 98 99]\n" + ] + } + ], + "source": [ + "arr1 = np.arange(100)\n", + "arr2 = np.arange(96,100)\n", + "\n", + "print (arr1)\n", + "print (arr2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#기본적인 배열의 연산" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1 2 3 4 5]\n", + "[10 20 30 40 50]\n" + ] + } + ], + "source": [ + "print (onearr)\n", + "print (tenarr)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[11 22 33 44 55]\n", + "[ -9 -18 -27 -36 -45]\n", + "[ 10 40 90 160 250]\n", + "[0.1 0.1 0.1 0.1 0.1]\n" + ] + } + ], + "source": [ + "plus = onearr + tenarr\n", + "minus = onearr - tenarr\n", + "mul = onearr * tenarr\n", + "div = onearr / tenarr\n", + "\n", + "print (plus)\n", + "print (minus)\n", + "print (mul)\n", + "print (div)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[ 15 26 37]\n", + " [105 206 307]]\n", + "[[ 5 14 23]\n", + " [ 95 194 293]]\n" + ] + } + ], + "source": [ + "arr1 = np.array([[10, 20, 30], [100, 200, 300]])\n", + "arr2 = np.array(([5, 6, 7], [5, 6, 7]))\n", + "\n", + "print (arr1 + arr2)\n", + "print (arr1 - arr2)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[10. 0.]\n", + " [ 0. 40.]]\n" + ] + } + ], + "source": [ + "arr1 = np.array([[10, 20], [30, 40]])\n", + "arr2 = np.identity(2)\n", + "\n", + "print (arr1 * arr2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#numpy에서의 브로드캐스트\n", + "#numpy는 브로드 캐스트를 지원합니다. 몰론 두 array가 행이나 열의 길이가 같아야 합니다." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[10 20]\n", + " [30 40]\n", + " [50 60]]\n", + "(3, 2)\n" + ] + } + ], + "source": [ + "arr1 = np.array([[10, 20],[30, 40],[50,60]])\n", + "\n", + "print (arr1)\n", + "print (arr1.shape)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(2,)\n", + "(3, 1)\n" + ] + } + ], + "source": [ + "arr2 = np.array([100,200])\n", + "arr3 = np.array([[100], [200], [300]])\n", + "\n", + "print (arr2.shape)\n", + "print (arr3.shape)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[110 220]\n", + " [130 240]\n", + " [150 260]]\n", + "[[110 120]\n", + " [230 240]\n", + " [350 360]]\n" + ] + } + ], + "source": [ + "horizontal = arr1 + arr2\n", + "print (horizontal)\n", + "\n", + "vertical = arr1 + arr3\n", + "print (vertical)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "operands could not be broadcast together with shapes (5,) (4,) ", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m<ipython-input-24-3603de6b54e0>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mdifarr\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0marray\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m100\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m200\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m300\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m400\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0msumarr2\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0monearr\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mdifarr\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0msumarr2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mValueError\u001b[0m: operands could not be broadcast together with shapes (5,) (4,) " + ] + } + ], + "source": [ + "difarr = np.array([100,200,300,400])\n", + "sumarr2 = onearr + difarr\n", + "print (sumarr2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#배열의 값 찾기\n", + "#파이썬, C언어에서 하는 배열 값 찾기와 비슷하다.\n", + "\n", + "#1차원에서는 기본적으로 1개의 값만 입력하면 되지만, 2차원은 2개의 값을 입력한다." + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0 1 2 3 4 5 6 7 8 9]\n", + "6\n", + "0\n", + "[3 4 5 6]\n", + "[0 1 2 3]\n", + "[6 7 8 9]\n", + "[0 1 2 3 4 5 6 7 8 9]\n" + ] + } + ], + "source": [ + "arr1 = np.arange(10)\n", + "\n", + "print (arr1)\n", + "print (arr1[6])\n", + "print (arr1[0])\n", + "print (arr1[3:7])\n", + "print (arr1[:4])\n", + "print (arr1[6:])\n", + "print (arr1[:])" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1 2 3]\n", + " [4 5 6]\n", + " [7 8 9]]\n", + "9\n", + "1\n", + "[1 2 3]\n", + "[[2 3]\n", + " [5 6]\n", + " [8 9]]\n", + "[[1]\n", + " [4]\n", + " [7]]\n" + ] + } + ], + "source": [ + "arr2 = np.array([[1,2,3],[4,5,6],[7,8,9]])\n", + "\n", + "print (arr2)\n", + "print (arr2[2,2])\n", + "print (arr2[0,0])\n", + "print (arr2[0,:])\n", + "print (arr2[0:,1:])\n", + "print (arr2[:3,:1])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#numpy의 함수들\n", + "#진짜 많다 여기 있는게 다가 아니다" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0.9446711 0.83497248 0.83201387 0.13935773 0.17415421 0.49383566\n", + " 0.62534832 0.95301777 0.775304 0.42802872]\n", + "[ 0.49760198 0.42642281 -0.17887658 0.97600137 -1.09890443 1.14944642\n", + " -1.61367583 0.68313963 -0.69266217 -0.20675362]\n", + "[0.45892907 0.79511622 0.23510859 0.83475971 0.61042409 0.98928205\n", + " 0.04701599 0.47026134 0.10542542 0.6393744 ]\n", + "3\n" + ] + } + ], + "source": [ + "arr1 = np.random.random(10)\n", + "arr2 = np.random.randn(10)\n", + "arr3 = np.random.rand(10)\n", + "arr4 = np.random.randint(10)\n", + "\n", + "print (arr1)\n", + "print (arr2)\n", + "print (arr3)\n", + "print (arr4)" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[0.69049139, 0.33961382, 0.4720473 , 0.05657901, 0.44823818,\n", + " 0.62798771, 0.18857805, 0.58646854, 0.78296022, 0.23064811],\n", + " [0.89095585, 0.56933846, 0.66368549, 0.36591346, 0.78302718,\n", + " 0.04110933, 0.23313533, 0.3403511 , 0.68102568, 0.99330884],\n", + " [0.18974105, 0.01787721, 0.24513353, 0.61273492, 0.82943479,\n", + " 0.88117367, 0.39032357, 0.85654216, 0.57124692, 0.9269831 ]])" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.random.rand(3, 10)" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[9.44671104 8.34972483 8.32013866 1.39357733 1.74154213 4.9383566\n", + " 6.2534832 9.53017769 7.75304001 4.28028724]\n", + "[10. 9. 9. 2. 2. 5. 7. 10. 8. 5.]\n", + "[9. 8. 8. 1. 1. 4. 6. 9. 7. 4.]\n" + ] + } + ], + "source": [ + "arr1 = arr1 * 10\n", + "\n", + "print (arr1)\n", + "print (np.ceil(arr1))\n", + "print (np.floor(arr1))" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1. 1.41421356 1.73205081]\n", + " [2. 2.23606798 2.44948974]\n", + " [2.64575131 2.82842712 3. ]]\n" + ] + } + ], + "source": [ + "arr1 = np.array([[1,2,3],[4,5,6],[7,8,9]])\n", + "\n", + "print (np.sqrt(arr1))" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[ 10 -100]\n", + " [ 10 -1000]]\n", + "[[ 10 100]\n", + " [ 10 1000]]\n" + ] + } + ], + "source": [ + "arr2 = np.array([[10, -100],[10, -1000]])\n", + "\n", + "print (arr2)\n", + "print (np.abs(arr2))" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[2.71828183e+00 7.38905610e+00 2.00855369e+01]\n", + " [5.45981500e+01 1.48413159e+02 4.03428793e+02]\n", + " [1.09663316e+03 2.98095799e+03 8.10308393e+03]]\n", + "[[0. 0.30103 0.47712125]\n", + " [0.60205999 0.69897 0.77815125]\n", + " [0.84509804 0.90308999 0.95424251]]\n" + ] + } + ], + "source": [ + "print (np.exp(arr1))\n", + "print (np.log10(arr1))" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[ 0.54030231 -0.41614684 -0.9899925 ]\n", + " [-0.65364362 0.28366219 0.96017029]\n", + " [ 0.75390225 -0.14550003 -0.91113026]]\n" + ] + } + ], + "source": [ + "print (np.cos(arr1))" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[9 8 7]\n", + " [6 5 6]\n", + " [7 8 9]]\n", + "[[1 2 3]\n", + " [4 5 4]\n", + " [3 2 1]]\n" + ] + } + ], + "source": [ + "arr3 = np.array([[9,8,7],[6,5,4],[3,2,1]])\n", + "\n", + "print (np.maximum(arr1, arr3))\n", + "print (np.minimum(arr1, arr3))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#행렬의 정렬" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[31. 75. 13. 88. 97. 83. 97. 52. 42. 40. 27. 39. 95. 78. 86. 9. 72. 85.\n", + " 55. 75. 8. 17. 77. 78. 55. 1. 81. 89. 62. 68.]\n", + "[31 75 13 88 97 83 97 52 42 40 27 39 95 78 86 9 72 85 55 75 8 17 77 78\n", + " 55 1 81 89 62 68]\n", + "int32\n" + ] + } + ], + "source": [ + "arr1 = np.random.rand(30)\n", + "\n", + "arr1 = arr1 * 100\n", + "arr1 = np.floor(arr1)\n", + "\n", + "print (arr1)\n", + "\n", + "arr1 = arr1.astype('int32')\n", + "\n", + "print (arr1)\n", + "print (arr1.dtype)" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[ 1 8 9 13 17 27 31 39 40 42 52 55 55 62 68 72 75 75 77 78 78 81 83 85\n", + " 86 88 89 95 97 97]\n", + "[97 97 95 89 88 86 85 83 81 78 78 77 75 75 72 68 62 55 55 52 42 40 39 31\n", + " 27 17 13 9 8 1]\n" + ] + } + ], + "source": [ + "print (np.sort(arr1))\n", + "print (np.sort(arr1)[::-1])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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.6" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} -- GitLab