Last active
June 9, 2020 04:26
-
-
Save raryo/616d5b8b1b4b0aeaf2ae07d71020c232 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# 2.Python入門の演習問題\n", | |
"\n", | |
"### 問2.1 組み込み関数 9:38~" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"len:5, max:8, min:1, sum:20, sort:[1, 3, 4, 4, 8]\n" | |
] | |
} | |
], | |
"source": [ | |
"def FUNC(a):\n", | |
" return f'len:{len(a)}, max:{max(a)}, min:{min(a)}, sum:{sum(a)}, sort:{sorted(a)}'\n", | |
"\n", | |
"a = [4, 8, 3, 4, 1]\n", | |
"res = FUNC(a)\n", | |
"print(res)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 問2.2 演算" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"5.0, type:<class 'float'> <- 1.2 + 3.8\n", | |
"0, type:<class 'int'> <- 10 // 100\n", | |
"True, type:<class 'bool'> <- 1 >= 0\n", | |
"True, type:<class 'bool'> <- 'Hello World' == 'Hello World'\n", | |
"False, type:<class 'bool'> <- not 'Chainer' != 'Tutorial'\n", | |
"False, type:<class 'bool'> <- all([True, True, False])\n", | |
"True, type:<class 'bool'> <- any([True, True, False])\n", | |
"3, type:<class 'int'> <- abs(-3)\n" | |
] | |
}, | |
{ | |
"ename": "ZeroDivisionError", | |
"evalue": "integer division or modulo by zero", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-41-5a77ac29cef3>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0mptype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"any([True, True, False])\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# True, bool\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0mptype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"abs(-3)\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# 3, int,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 13\u001b[0;31m \u001b[0mptype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"2 // 0\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# devided by zero\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;32m<ipython-input-41-5a77ac29cef3>\u001b[0m in \u001b[0;36mptype\u001b[0;34m(x)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mptype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mret\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0meval\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf'{ret}, type:{type(ret)} <- {x}'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mptype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'1.2 + 3.8'\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# 5.0, float\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;32m<string>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n", | |
"\u001b[0;31mZeroDivisionError\u001b[0m: integer division or modulo by zero" | |
] | |
} | |
], | |
"source": [ | |
"def ptype(x):\n", | |
" ret = eval(x)\n", | |
" print(f'{ret}, type:{type(ret)} <- {x}')\n", | |
" \n", | |
"ptype('1.2 + 3.8') # 5.0, float\n", | |
"ptype('10 // 100') # 0, int\n", | |
"ptype('1 >= 0') # True, bool\n", | |
"ptype(\"'Hello World' == 'Hello World'\") # True, bool\n", | |
"ptype(\"not 'Chainer' != 'Tutorial'\") # True, bool\n", | |
"ptype(\"all([True, True, False])\") # False, bool\n", | |
"ptype(\"any([True, True, False])\") # True, bool \n", | |
"ptype(\"abs(-3)\") # 3, int, \n", | |
"ptype(\"2 // 0\") # Zero devided err" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Q2.3 list " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 61, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"a=[4, 8, 3, 4, 1]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 66, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[8, 3, 4, 1]" | |
] | |
}, | |
"execution_count": 66, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#1 \n", | |
"a[1:]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 70, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[4, 8, 3]" | |
] | |
}, | |
"execution_count": 70, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#2\n", | |
"a[:len(a)]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 64, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[4, 8, 3, 4, 1, 100]" | |
] | |
}, | |
"execution_count": 64, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#3\n", | |
"a + [100]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 問2.4 リスト内包表記" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 72, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[0, 0, 1, 0, 1]\n", | |
"2\n", | |
"2\n", | |
"[3, 1]\n" | |
] | |
} | |
], | |
"source": [ | |
"#1\n", | |
"a=[4, 8, 3, 4, 1]\n", | |
"print([aa%2 for aa in a])\n", | |
"\n", | |
"#2\n", | |
"print(sum([aa%2 for aa in a]))\n", | |
"print(len([aa for aa in a if aa%2]))\n", | |
"\n", | |
"#3\n", | |
"print([aa for aa in a if aa%2])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Q2.5 Strings" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'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": 13, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#1 \n", | |
"\" \".join(map(str, range(0, 100)))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'0.142857143'" | |
] | |
}, | |
"execution_count": 16, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#2\n", | |
"\"{:.9f}\".format(1.0/7.0)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 74, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0.142857143\n" | |
] | |
} | |
], | |
"source": [ | |
"x = 1.0 / 7.0\n", | |
"print(f'{x:.9f}')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100\n" | |
] | |
} | |
], | |
"source": [ | |
"from decimal import *\n", | |
"import re\n", | |
"getcontext().prec = 298\n", | |
"\n", | |
"data = Decimal(1000) / Decimal(999**2) #計算部分\n", | |
"data = (str(data).split(\".\")[1])\n", | |
"\n", | |
"print(\" \".join(re.findall(r'...', data))) #分割+出力部分" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Q1.6 Class" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class DataManager():\n", | |
" def __init__(self, x, y, z):\n", | |
" self.x = x\n", | |
" self.y = y\n", | |
" self.z = z\n", | |
" \n", | |
" def add_x(self, v, delta):\n", | |
" self.x += delta\n", | |
" \n", | |
" def add_y(self, delta):\n", | |
" self.y += delta\n", | |
" \n", | |
" def add_z(self, delta):\n", | |
" self.z += delta \n", | |
" \n", | |
" def sum(self):\n", | |
" return self.x + self.y + self.z" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"10\n", | |
"14\n", | |
"14\n", | |
"5\n" | |
] | |
} | |
], | |
"source": [ | |
"data_manager = DataManager(2, 3, 5)\n", | |
"print(data_manager.sum()) # => 10\n", | |
"data_manager.add_x(4) # => data_manager.x の値が 2 から 6 に更新される\n", | |
"print(data_manager.sum()) # => 14\n", | |
"data_manager.add_y(0) # => data_manager.y の値が 3 から 3 に更新される\n", | |
"print(data_manager.sum()) # => 14\n", | |
"data_manager.add_z(-9) # => data_manager.z の値が 5 から -4 に更新される\n", | |
"print(data_manager.sum()) # => 5" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Q2.7" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[1, 2, 3]\n", | |
"[1, 2, 3, 1]\n" | |
] | |
} | |
], | |
"source": [ | |
"def f(a):\n", | |
" a = [6, 7, 8]\n", | |
"\n", | |
"def g(a):\n", | |
" a.append(1)\n", | |
" \n", | |
"def somefunction():\n", | |
" a0 = [1, 2, 3]\n", | |
" f(a0)\n", | |
" print(a0)\n", | |
"\n", | |
" a1 = [1, 2, 3]\n", | |
" g(a1)\n", | |
" print(a1)\n", | |
"\n", | |
"somefunction() # print(a0) -> [1,2,3];;; print(a1) -> [1,2,3,1]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"関数`f`内に定義されている`a`は関数内のローカル変数であるため、`a0`関数`sumfunction`には作用しない。\n", | |
"関数`g`引数`a`にリスト`a1`が渡され、`append`関数により要素が加えられているが、`append`関数は破壊的操作のため引数に渡した`a1`に作用する" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Q2.8 if-for" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 75, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 " | |
] | |
} | |
], | |
"source": [ | |
"for c in range(2, 101):\n", | |
" is_prime = sum([c % k==0 for k in range(2, c+1)])\n", | |
" if is_prime == 1:\n", | |
" print(c, end=' ')\n", | |
" " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# 100 numpy exercises" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 76, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#1\n", | |
"import numpy as np" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 85, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"('1.16.4',\n", | |
" <module 'numpy.__config__' from '/usr/local/lib/python3.7/site-packages/numpy/__config__.py'>)" | |
] | |
}, | |
"execution_count": 85, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#2\n", | |
"np.__version__, np.__config__" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 100, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])" | |
] | |
}, | |
"execution_count": 100, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#3\n", | |
"np.zeros(10)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 94, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"24" | |
] | |
}, | |
"execution_count": 94, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#4\n", | |
"arr = np.array([0, 1, 2])\n", | |
"arr.itemsize * arr.size" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 286, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#5\n", | |
"#help(np.add)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 101, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([0., 0., 0., 0., 1., 0., 0., 0., 0., 0.])" | |
] | |
}, | |
"execution_count": 101, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#6 Create a null vector of size 10 but the fifth value which is 1 (★☆☆)\n", | |
"arr = np.zeros(10)\n", | |
"arr[4] = 1\n", | |
"arr" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 102, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,\n", | |
" 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,\n", | |
" 44, 45, 46, 47, 48, 49])" | |
] | |
}, | |
"execution_count": 102, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#7 Create a vector with values ranging from 10 to 49\n", | |
"np.arange(10, 50)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 111, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([4, 3, 2, 1])" | |
] | |
}, | |
"execution_count": 111, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#8 Reverse a vector (first element becomes last) \n", | |
"arr = np.arange(1, 5)\n", | |
"arr[::-1]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 113, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[0, 1, 2],\n", | |
" [3, 4, 5],\n", | |
" [6, 7, 8]])" | |
] | |
}, | |
"execution_count": 113, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#9 Create a 3x3 matrix with values ranging from 0 to 8\n", | |
"arr = np.arange(0, 9)\n", | |
"np.reshape(arr, (3, 3))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 230, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"2" | |
] | |
}, | |
"execution_count": 230, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#10 Find indices of non-zero elements from [1,2,0,0,4,0]\n", | |
"arr = [1, 2, 0, 0, 4, 0]\n", | |
"arr[arr!=0]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 233, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(array([[1., 0., 0.],\n", | |
" [0., 1., 0.],\n", | |
" [0., 0., 1.]]), array([[1., 0., 0.],\n", | |
" [0., 1., 0.],\n", | |
" [0., 0., 1.]]))" | |
] | |
}, | |
"execution_count": 233, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#11 Create a 3x3 identity matrix\n", | |
"np.identity(3), np.eye(3)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 124, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[[0.82364558, 0.97971559, 0.19507914],\n", | |
" [0.23235363, 0.39651818, 0.28378136],\n", | |
" [0.35125597, 0.62737491, 0.79292767]],\n", | |
"\n", | |
" [[0.87225014, 0.59071296, 0.05076891],\n", | |
" [0.63307031, 0.93570602, 0.41033451],\n", | |
" [0.48213109, 0.05261458, 0.2482121 ]],\n", | |
"\n", | |
" [[0.05068198, 0.14415422, 0.12568984],\n", | |
" [0.55631122, 0.96098087, 0.29417321],\n", | |
" [0.82768959, 0.79003356, 0.48266527]]])" | |
] | |
}, | |
"execution_count": 124, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#12 Create a 3x3x3 array with random values\n", | |
"np.random.random((3,3,3))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 132, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[78 94 33 92 42 59 88 53 17 15]\n", | |
" [24 34 16 29 13 18 46 37 30 30]\n", | |
" [55 56 50 85 34 87 58 37 45 97]\n", | |
" [78 9 45 25 62 89 51 15 50 60]\n", | |
" [80 68 99 3 19 44 96 11 73 81]\n", | |
" [44 42 92 68 70 82 22 72 73 3]\n", | |
" [83 74 51 10 35 59 40 6 77 47]\n", | |
" [64 94 45 98 6 59 36 64 75 22]\n", | |
" [34 75 2 54 94 46 89 35 25 32]\n", | |
" [ 1 8 44 66 75 7 45 90 9 83]]\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"(1, 99)" | |
] | |
}, | |
"execution_count": 132, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#13 Create a 10x10 array with random values and find the minimum and maximum values \n", | |
"mat = np.random.randint(1, high=100, size=(10,10))\n", | |
"print(mat)\n", | |
"mat.min(), mat.max()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 234, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"1.0000016832209688" | |
] | |
}, | |
"execution_count": 234, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#14 Create a random vector of size 30 and find the mean value\n", | |
"np.random.normal(1, 0.00001, (30,)).mean()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 235, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[1., 1., 1., 1., 1.],\n", | |
" [1., 0., 0., 0., 1.],\n", | |
" [1., 0., 0., 0., 1.],\n", | |
" [1., 0., 0., 0., 1.],\n", | |
" [1., 1., 1., 1., 1.]])" | |
] | |
}, | |
"execution_count": 235, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#15 Create a 2d array with 1 on the border and 0 inside \n", | |
"mat = np.zeros((5, 5))\n", | |
"mat[0], mat[-1], mat[:, 0], mat[:, -1] = [1] * 4\n", | |
"mat" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 238, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[0., 0., 0., 0., 0., 0., 0.],\n", | |
" [0., 1., 1., 1., 1., 1., 0.],\n", | |
" [0., 1., 1., 1., 1., 1., 0.],\n", | |
" [0., 1., 1., 1., 1., 1., 0.],\n", | |
" [0., 1., 1., 1., 1., 1., 0.],\n", | |
" [0., 1., 1., 1., 1., 1., 0.],\n", | |
" [0., 0., 0., 0., 0., 0., 0.]])" | |
] | |
}, | |
"execution_count": 238, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#15 yamagichi\n", | |
"outer = np.ones((5, 5))\n", | |
"np.pad(outer, (1, 1), 'constant')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 154, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"16.0" | |
] | |
}, | |
"execution_count": 154, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#16 How to add a border (filled with 0's) around an existing array? \n", | |
"mat.sum()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 157, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(nan, False, False, nan, True, False)" | |
] | |
}, | |
"execution_count": 157, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#17 What is the result of the following expression?\n", | |
"0 * np.nan, np.nan == np.nan, np.inf > np.nan, np.nan - np.nan, np.nan in set([np.nan]), 0.3 == 3 * 0.1" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 160, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[0., 0., 0., 0., 0.],\n", | |
" [0., 1., 0., 0., 0.],\n", | |
" [0., 0., 2., 0., 0.],\n", | |
" [0., 0., 0., 3., 0.],\n", | |
" [0., 0., 0., 0., 4.]])" | |
] | |
}, | |
"execution_count": 160, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#18 Create a 5x5 matrix with values 1,2,3,4 just below the diagonal\n", | |
"np.identity(5) * np.arange(0, 5)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 241, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[0., 0., 0., 0., 0.],\n", | |
" [0., 1., 0., 0., 0.],\n", | |
" [0., 0., 2., 0., 0.],\n", | |
" [0., 0., 0., 3., 0.],\n", | |
" [0., 0., 0., 0., 4.]])" | |
] | |
}, | |
"execution_count": 241, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# 18 machii\n", | |
"np.diag(np.arange(0., 5.))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 242, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[1., 0., 1., 0., 1., 0., 1., 0.],\n", | |
" [0., 1., 0., 1., 0., 1., 0., 1.],\n", | |
" [1., 0., 1., 0., 1., 0., 1., 0.],\n", | |
" [0., 1., 0., 1., 0., 1., 0., 1.],\n", | |
" [1., 0., 1., 0., 1., 0., 1., 0.],\n", | |
" [0., 1., 0., 1., 0., 1., 0., 1.],\n", | |
" [1., 0., 1., 0., 1., 0., 1., 0.],\n", | |
" [0., 1., 0., 1., 0., 1., 0., 1.]])" | |
] | |
}, | |
"execution_count": 242, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#19 Create a 8x8 matrix and fill it with a checkerboard pattern \n", | |
"mat = np.zeros((8, 8))\n", | |
"mat[::2][:, ::2] = 1\n", | |
"mat[1::2][:, 1::2] = 1\n", | |
"mat" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 276, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[1., 0., 1., 0., 1., 0., 1., 0.],\n", | |
" [0., 1., 0., 1., 0., 1., 0., 1.],\n", | |
" [1., 0., 1., 0., 1., 0., 1., 0.],\n", | |
" [0., 1., 0., 1., 0., 1., 0., 1.],\n", | |
" [1., 0., 1., 0., 1., 0., 1., 0.],\n", | |
" [0., 1., 0., 1., 0., 1., 0., 1.],\n", | |
" [1., 0., 1., 0., 1., 0., 1., 0.],\n", | |
" [0., 1., 0., 1., 0., 1., 0., 1.]])" | |
] | |
}, | |
"execution_count": 276, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#19 \n", | |
"np.vstack(mat[[[i,j] for i in np.arange(0,8) for j in np.arange(0, 8) if (i+j)%2==1]][:4])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 228, | |
"metadata": { | |
"scrolled": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(1, 5, 4, 100)" | |
] | |
}, | |
"execution_count": 228, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#20\n", | |
"mat = np.arange(0, 6*7*8).reshape((6, 7, 8))\n", | |
"x = 100 // (8*7)\n", | |
"y = 100 // 8 - 7 *x\n", | |
"z = 100 % 8\n", | |
"x, y, z, mat[x, y, z]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 216, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[[ 0, 1, 2, 3, 4, 5, 6, 7],\n", | |
" [ 8, 9, 10, 11, 12, 13, 14, 15],\n", | |
" [ 16, 17, 18, 19, 20, 21, 22, 23],\n", | |
" [ 24, 25, 26, 27, 28, 29, 30, 31],\n", | |
" [ 32, 33, 34, 35, 36, 37, 38, 39],\n", | |
" [ 40, 41, 42, 43, 44, 45, 46, 47],\n", | |
" [ 48, 49, 50, 51, 52, 53, 54, 55]],\n", | |
"\n", | |
" [[ 56, 57, 58, 59, 60, 61, 62, 63],\n", | |
" [ 64, 65, 66, 67, 68, 69, 70, 71],\n", | |
" [ 72, 73, 74, 75, 76, 77, 78, 79],\n", | |
" [ 80, 81, 82, 83, 84, 85, 86, 87],\n", | |
" [ 88, 89, 90, 91, 92, 93, 94, 95],\n", | |
" [ 96, 97, 98, 99, 100, 101, 102, 103],\n", | |
" [104, 105, 106, 107, 108, 109, 110, 111]],\n", | |
"\n", | |
" [[112, 113, 114, 115, 116, 117, 118, 119],\n", | |
" [120, 121, 122, 123, 124, 125, 126, 127],\n", | |
" [128, 129, 130, 131, 132, 133, 134, 135],\n", | |
" [136, 137, 138, 139, 140, 141, 142, 143],\n", | |
" [144, 145, 146, 147, 148, 149, 150, 151],\n", | |
" [152, 153, 154, 155, 156, 157, 158, 159],\n", | |
" [160, 161, 162, 163, 164, 165, 166, 167]],\n", | |
"\n", | |
" [[168, 169, 170, 171, 172, 173, 174, 175],\n", | |
" [176, 177, 178, 179, 180, 181, 182, 183],\n", | |
" [184, 185, 186, 187, 188, 189, 190, 191],\n", | |
" [192, 193, 194, 195, 196, 197, 198, 199],\n", | |
" [200, 201, 202, 203, 204, 205, 206, 207],\n", | |
" [208, 209, 210, 211, 212, 213, 214, 215],\n", | |
" [216, 217, 218, 219, 220, 221, 222, 223]],\n", | |
"\n", | |
" [[224, 225, 226, 227, 228, 229, 230, 231],\n", | |
" [232, 233, 234, 235, 236, 237, 238, 239],\n", | |
" [240, 241, 242, 243, 244, 245, 246, 247],\n", | |
" [248, 249, 250, 251, 252, 253, 254, 255],\n", | |
" [256, 257, 258, 259, 260, 261, 262, 263],\n", | |
" [264, 265, 266, 267, 268, 269, 270, 271],\n", | |
" [272, 273, 274, 275, 276, 277, 278, 279]],\n", | |
"\n", | |
" [[280, 281, 282, 283, 284, 285, 286, 287],\n", | |
" [288, 289, 290, 291, 292, 293, 294, 295],\n", | |
" [296, 297, 298, 299, 300, 301, 302, 303],\n", | |
" [304, 305, 306, 307, 308, 309, 310, 311],\n", | |
" [312, 313, 314, 315, 316, 317, 318, 319],\n", | |
" [320, 321, 322, 323, 324, 325, 326, 327],\n", | |
" [328, 329, 330, 331, 332, 333, 334, 335]]])" | |
] | |
}, | |
"execution_count": 216, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"mat" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3.7.4 64-bit", | |
"language": "python", | |
"name": "python37464bita035101807a046a0bf85be8625188e90" | |
}, | |
"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.7.4" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment