Skip to content

Instantly share code, notes, and snippets.

@raryo
Last active June 9, 2020 04:26
Show Gist options
  • Save raryo/616d5b8b1b4b0aeaf2ae07d71020c232 to your computer and use it in GitHub Desktop.
Save raryo/616d5b8b1b4b0aeaf2ae07d71020c232 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 5. 線形代数の基礎 の演習問題"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Q5.1 vector/matrix operation"
]
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[9, 3, 5]\n",
"[9 3 5]\n"
]
}
],
"source": [
"#2つのベクトル x, y を受け取り、その和 x+y を返す関数を書いて下さい。コードは例えば以下のようになります。\n",
"def vector_sum(x, y):\n",
" return [x[i] + y[i] for i in range(len(x))]\n",
"\n",
"x = [1, 2, 3]\n",
"y = [8, 1, 2]\n",
"answer = vector_sum(x, y)\n",
"print(answer) # => [9, 3, 5]\n",
"na = np.array\n",
"print(np.array(x) + np.array(y))"
]
},
{
"cell_type": "code",
"execution_count": 96,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[9, 3, 5], [3, 5, 4]]\n",
"[[9 3 5]\n",
" [3 5 4]]\n"
]
}
],
"source": [
"#2つの行列 X, Y を受け取り、その和 X+Y を返す関数を書いて下さい。コードは例えば以下のようになります。\n",
"def matrix_sum(X, Y):\n",
" def vs(x, y):\n",
" return [x[i] + y[i] for i in range(len(x))]\n",
" return [vs(X[i], Y[i]) for i in range(len(X))]\n",
"\n",
"X = [[1, 2, 3],\n",
" [4, 5, 6]]\n",
"Y = [[8, 1, 2],\n",
" [-1, 0, -2]]\n",
"answer = matrix_sum(X, Y)\n",
"print(answer) # => [[9, 3, 5], [3, 5, 4]]\n",
"print(na(X) + na(Y))"
]
},
{
"cell_type": "code",
"execution_count": 98,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[16, 49]\n",
"[16 49]\n"
]
}
],
"source": [
"# 行列 X とベクトル y を受け取り、その積 Xy を返す関数を書いて下さい。\n",
"def matrix_vector_product(X, y):\n",
" def dot(x, y):\n",
" return sum([x[i] * y[i] for i in range(len(x))])\n",
" return [dot(X[i], y) for i in range(len(X))]\n",
"\n",
"X = [[1, 2, 3],\n",
" [4, 5, 6]]\n",
"y = [8, 1, 2]\n",
"answer = matrix_vector_product(X, y)\n",
"print(answer) # => [16, 49]\n",
"\n",
"def dot(x, y):\n",
" return sum([x[i] * y[i] for i in range(len(x))])\n",
"\n",
"print(np.matmul(na(X), na(y)))"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[8, -1, 0], [1, 0, 1]]"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def tp(X):\n",
" return [[row[i] for row in X] for i in range(len(X[0]))]\n",
"Y = [[8, 1],\n",
" [-1, 0],\n",
" [0, 1]]\n",
"tp(Y)"
]
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[6, 4], [27, 10]]\n",
"[[ 6 4]\n",
" [27 10]]\n"
]
}
],
"source": [
"#2つの行列 X, Y を受け取り、その積 XY を返す関数を書いて下さい。\n",
"def matrix_product(X, Y):\n",
" tY = tp(Y)\n",
"# for i in range(len(X)):\n",
" return [[dot(X[j], tY[i]) for i in range(len(X))] for j in range(len(tY))]\n",
" \n",
" \n",
"\n",
"X = [[1, 2, 3],\n",
" [4, 5, 6]]\n",
"Y = [[8, 1],\n",
" [-1, 0],\n",
" [0, 1]]\n",
"answer = matrix_product(X, Y)\n",
"print(answer) # => [[6, 4], [27, 10]]\n",
"print(np.matmul(na(X), na(Y)))"
]
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1, 4], [2, 5], [3, 6]]\n",
"[[1 4]\n",
" [2 5]\n",
" [3 6]]\n"
]
}
],
"source": [
"def matrix_transpose(X):\n",
" return tp(X)\n",
"\n",
"X = [[1, 2, 3],\n",
" [4, 5, 6]]\n",
"answer = matrix_transpose(X)\n",
"print(answer) # => [[1, 4], [2, 5], [3, 6]]\n",
"print(na(X).T)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## \"6. 確率・統計の基礎\" の演習問題"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 問6.1 (同時分布・周辺確率)\n",
"偏りのあるコイン $A$ と $B$ があります。 コイン $A$ を振ったときに表が出る確率は1/4、裏が出る確率は3/4です。コイン $B$ を振ったときに表が出る確率は2/3、裏が出る確率は1/3です。\n",
"\n",
"この二つのコインのうちどちらか一つを等しい確率でランダムに選び、選んだ方のコインを振るという試行を考えます。 選んだコインが $A$ と $B$ どちらだったかを表す確率変数を $X$、コインを振った結果(表裏)を表す確率変数を $Y$ とします。\n",
"\n",
"同時確率 $p(X=A,Y=表), p(X=A,Y=裏), p(X=B,Y=表), p(X=B,Y=裏)$ をそれぞれ計算して下さい。 \n",
"同時確率を周辺化して、確率 $p(Y=表)$ と $p(Y=裏)$ を計算して下さい。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"コインA:$p(O|A)=\\frac{1}{4}, p(U|A)=\\frac{3}{4}$\n",
"コインB:$p(O|B)=\\frac{2}{3}, p(U|B)=\\frac{1}{3}$\n",
"\n",
"より, \n",
"$p(X=A, Y=O) = p(Y=O|X=A)p(X=A) = \\frac{1}{4} * \\frac{1}{2} = \\frac{1}{8}$ \n",
"同様に、$p(X=A, Y=U)=\\frac{3}{8}, p(X=B, Y=O)=\\frac{1}{3}, p(X=B, Y=U)=\\frac{1}{6}$\n",
"また、$p(Y=O) = p(X=A, Y=O) + p(X=B, Y=O) = \\frac{11}{24}, p(Y=U) = p(X=A, Y=U) + p(X=B, Y=U) = \\frac{13}{24}$ "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 問6.2 (条件付き確率)\n",
"問6.1 の試行で条件付き確率 p(Y=表∣X=A) を計算して下さい。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 問6.3 (ベイズの定理)\n",
"\n",
"問6.1 の試行で事後確率 $p(X=A∣Y=表)$, $p(X=B∣Y=表)$, $p(X=A∣Y=裏)$, $p(X=B∣Y=裏)$ をそれぞれ計算して下さい。 \n",
"問6.1 の試行を行った後、コインは表が出たとします。この後さらに同じコインを振ったときに再び表が出る確率を計算して下さい。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$p(X=A|Y=O) = p(Y=O|X=A)p(X=A) / p(Y=O) = 1/4 * 1/2 * 24/11 = \\frac{3}{11}$ \n",
"$p(X=B|Y=O) = p(Y=O|X=B)p(X=B) / p(Y=O) = 2/3 * 1/2 * 24/11 = \\frac{8}{11}$ \n",
"$p(X=A|Y=U) = p(Y=U|X=A)p(X=A) / p(Y=U) = 3/4 * 1/2 * 24/13 = \\frac{9}{13}$ \n",
"$p(X=A|Y=U) = p(Y=U|X=B)p(X=B) / p(Y=U) = 1/3 * 1/2 * 24/13 = \\frac{4}{13}$ "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$p(O|A)p(A)p(O|A) + p(O|B)p(B)p(O|B) = 1/32 + 2/9 = 9+64/288$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 問6.4 (ベイズの定理)\n",
"一様な確率で1から6までの数が出るサイコロがあります。 サイコロを何回か連続で振る試行を考えます。サイコロを振った後、直前に出た数より大きい数が出たときに「ラッキーである」と呼ぶことにします。 例えばサイコロを5回振って 2,5,3,4,4 と順に 出た場合、2回目と4回目はラッキーですが、それ以外はラッキーではありません。\n",
"\n",
"いま、サイコロを 10 回振って一度もラッキーではなかったとします。このとき、次にサイコロを振ってラッキーになる確率はいくらになるでしょうか。プログラムを使って分析してみて下さい。"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from numba import njit"
]
},
{
"cell_type": "code",
"execution_count": 103,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"prior:10, post:1, ans:0.1\n"
]
}
],
"source": [
"def isNoLucky(A):\n",
" return not any([(A[i+1]-A[i])>0 for i in range(len(A)-1)])\n",
" return any(res)\n",
"\n",
"N = 100_000\n",
"cnt_prior = 0\n",
"cnt_post = 0\n",
"\n",
"\n",
"for s in range(N):\n",
" X = np.random.randint(1, 6, 10)\n",
" if isNoLucky(X) == True:\n",
" cnt_prior += 1\n",
" X = np.append(X, np.random.randint(1, 6, 1))\n",
" if isNoLucky(X) == True:\n",
" cnt_post += 1\n",
"print(f'prior:{cnt_prior}, post:{cnt_post}, ans:{cnt_post/cnt_prior}')"
]
},
{
"cell_type": "code",
"execution_count": 111,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.03984545489748833\n"
]
}
],
"source": [
"@njit\n",
"def q64(N):\n",
" cnt_prior = 0\n",
" cnt_post = 0\n",
"\n",
"\n",
" for s in range(N):\n",
" X = np.random.randint(1, 6, 4)\n",
"\n",
" # ラッキー判定処理\n",
" sum_ = 0\n",
" for i in range(len(X)-1):\n",
" sum_ += (X[i+1] - X[i] <= 0)\n",
"\n",
" # もしNoラッキーなら\n",
" if sum_ == 0:\n",
" cnt_prior += 1 # カウントアップ(分母)\n",
" # 次のサイコロをふる\n",
" X = np.append(X, np.random.randint(1, 6, 1))\n",
"\n",
" # is NO luckey?\n",
" sum_ = 0\n",
" for i in range(len(X)-1):\n",
" sum_ += (X[i+1] - X[i] <= 0)\n",
" # もしnoラッキーならカウントアップ(分子)\n",
" if sum_ == 0:\n",
" cnt_post += 1\n",
" print(cnt_post / cnt_prior)\n",
" \n",
"q64(1_000_000_00)"
]
},
{
"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": [
"### Numpy training"
]
},
{
"cell_type": "code",
"execution_count": 254,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[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],\n",
" [1, 0, 1, 0, 1, 0, 1, 0]])"
]
},
"execution_count": 254,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.tile([[0, 1], [1, 0]],(4, 4))"
]
},
{
"cell_type": "code",
"execution_count": 140,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0, 8, 0, 8, 0, 8, 0, 8],\n",
" [8, 0, 8, 0, 8, 0, 8, 0],\n",
" [0, 8, 0, 8, 0, 8, 0, 8],\n",
" [8, 0, 8, 0, 8, 0, 8, 0],\n",
" [0, 8, 0, 8, 0, 8, 0, 8],\n",
" [8, 0, 8, 0, 8, 0, 8, 0],\n",
" [0, 8, 0, 8, 0, 8, 0, 8],\n",
" [8, 0, 8, 0, 8, 0, 8, 0]])"
]
},
"execution_count": 140,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 21. Create a checkerboard 8x8 matrix using the tile function (★☆☆)\n",
"# np.tile(np.vstack([np.tile([0, 8], 4), np.tile([8, 0], 4)]), 4)\n",
"\n",
"\n",
"np.tile(np.reshape([0,8,8,0]*4, (8,2)), 4)"
]
},
{
"cell_type": "code",
"execution_count": 153,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5.329070518200751e-17 1.0000000000000002\n"
]
}
],
"source": [
"#22. Normalize a 5x5 random matrix (★☆☆)\n",
"X = np.random.rand(5, 5)\n",
"nX = (X - X.mean()) / X.std()\n",
"print(nX.mean(), nX.var())"
]
},
{
"cell_type": "code",
"execution_count": 160,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([(205, 204, 204, 189)],\n",
" dtype=[('R', 'u1'), ('G', 'u1'), ('B', 'u1'), ('A', 'u1')])"
]
},
"execution_count": 160,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#23. Create a custom dtype that describes a color as four unsigned bytes (RGBA) (★☆☆)\n",
"dcolor = np.dtype([('R', 'B'), ('G', 'B'), ('B', 'B'), ('A', 'B')])\n",
"rgba = np.empty(1, dtype=dcolor)\n",
"rgba"
]
},
{
"cell_type": "code",
"execution_count": 167,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0.94790871 1.21922152]\n",
" [0.14545987 0.18860681]\n",
" [1.54817236 1.71401757]\n",
" [1.05955372 1.38995919]\n",
" [1.01560237 1.21580748]] (5, 2)\n"
]
}
],
"source": [
"# 24. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) (★☆☆)\n",
"A = np.random.rand(5, 3)\n",
"B = np.random.rand(3, 2)\n",
"C = np.matmul(A, B)\n",
"print(C, C.shape)"
]
},
{
"cell_type": "code",
"execution_count": 265,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[6.51954931 6.72052486 6.47075707 6.05421612 1.78626917 8.98068183\n",
" 6.44862839 5.25414015 7.99887041 2.2456201 ]\n",
"[-6.51954931 -6.72052486 -6.47075707 -6.05421612 1.78626917 8.98068183\n",
" -6.44862839 -5.25414015 -7.99887041 2.2456201 ]\n"
]
}
],
"source": [
"# 25. Given a 1D array, negate all elements which are between 3 and 8, in place. (★☆☆)\n",
"A = np.random.rand(10) * 10\n",
"print(A)\n",
"A[(A>=3)&(A<=8)] *= -1\n",
"print(A)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#26. What is the output of the following script? (★☆☆)\n",
"# Author: Jake VanderPlas\n",
"\n",
"print(sum(range(5),-1))\n",
"from numpy import *\n",
"print(sum(range(5),-1))"
]
},
{
"cell_type": "code",
"execution_count": 186,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"o Z**Z \n",
"o 2 << Z >> 2 \n",
"o Z <- Z \n",
"o 1j*Z \n",
"o Z/1/1 \n",
"x Z<Z>Z The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()\n"
]
}
],
"source": [
"#27. Consider an integer vector Z, which of these expressions are legal? (★☆☆)\n",
"def isLegal(s):\n",
" res = ''\n",
" err = ''\n",
" try:\n",
" eval(s)\n",
" res = 'o '\n",
" except Exception as e:\n",
" res = 'x '\n",
" err = e\n",
" print(f'{res} {s} {err}')\n",
"Z = np.arange(1, 10)\n",
"isLegal('Z**Z'),\n",
"isLegal('2 << Z >> 2')\n",
"isLegal('Z <- Z')\n",
"isLegal('1j*Z')\n",
"isLegal('Z/1/1')\n",
"isLegal('Z<Z>Z')"
]
},
{
"cell_type": "code",
"execution_count": 187,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/usr/local/lib/python3.7/site-packages/ipykernel_launcher.py:2: RuntimeWarning: invalid value encountered in true_divide\n",
" \n",
"/usr/local/lib/python3.7/site-packages/ipykernel_launcher.py:3: RuntimeWarning: divide by zero encountered in floor_divide\n",
" This is separate from the ipykernel package so we can avoid doing imports until\n"
]
},
{
"data": {
"text/plain": [
"array([-9.22337204e+18])"
]
},
"execution_count": 187,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#28. What are the result of the following expressions?\n",
"np.array(0) / np.array(0),\n",
"np.array(0) // np.array(0),\n",
"np.array([np.nan]).astype(int).astype(float)"
]
},
{
"cell_type": "code",
"execution_count": 269,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[8.97677074 6.06020386 6.29557674 2.20392256 3.95490212 5.67459379\n",
" 4.15242691 6.15932259 5.08540991 8.72458591]\n",
"[9. 7. 7. 3. 4. 6. 5. 7. 6. 9.]\n",
"[9. 7. 7. 3. 4. 6. 5. 7. 6. 9.]\n",
"[8. 6. 6. 2. 3. 5. 4. 6. 5. 8.]\n",
"[8. 6. 6. 2. 3. 5. 4. 6. 5. 8.]\n"
]
}
],
"source": [
"# 29. How to round away from zero a float array ? (★☆☆)\n",
"A = np.random.rand(10) * 10\n",
"print(A)\n",
"print(np.round(A+0.5, 0))\n",
"print(np.ceil(A))\n",
"print(np.round(A-0.5, 0))\n",
"print(np.floor(A))"
]
},
{
"cell_type": "code",
"execution_count": 315,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[7 7 5 2 7] [5 5 9 7 5]\n",
"[5 7]\n"
]
}
],
"source": [
"# 30. How to find common values between two arrays? (★☆☆)\n",
"A = np.random.randint(0, 10, 5)\n",
"B = np.random.randint(0, 10, 5)\n",
"print(A, B)\n",
"print(np.intersect1d(A, B))"
]
},
{
"cell_type": "code",
"execution_count": 211,
"metadata": {},
"outputs": [],
"source": [
"# 31. How to ignore all numpy warnings (not recommended)? (★☆☆)\n",
"# np.seterr"
]
},
{
"cell_type": "code",
"execution_count": 212,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/usr/local/lib/python3.7/site-packages/ipykernel_launcher.py:2: RuntimeWarning: invalid value encountered in sqrt\n",
" \n"
]
},
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 212,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 32. Is the following expressions true? (★☆☆)\n",
"np.sqrt(-1) == np.emath.sqrt(-1)"
]
},
{
"cell_type": "code",
"execution_count": 318,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2020-06-01\n",
"2020-06-02\n",
"2020-06-03\n"
]
}
],
"source": [
"# 33. How to get the dates of yesterday, today and tomorrow? (★☆☆)\n",
"today = np.datetime64('today')\n",
"print(today - 1)\n",
"print(today)\n",
"print(today + 1)"
]
},
{
"cell_type": "code",
"execution_count": 227,
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "module 'numpy' has no attribute 'date'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-227-24be11506c0f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# 34. How to get all the dates corresponding to the month of July 2016? (★★☆)\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[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdate\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m: module 'numpy' has no attribute 'date'"
]
}
],
"source": [
"# 34. How to get all the dates corresponding to the month of July 2016? (★★☆)\n",
"np.date"
]
},
{
"cell_type": "code",
"execution_count": 228,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([-16. , -24.5, -30. , -12. , -15. ])"
]
},
"execution_count": 228,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 35. How to compute ((A+B)*(-A/2)) in place (without copy)? (★★☆)\n",
"((A+B)*(-A/2))"
]
},
{
"cell_type": "code",
"execution_count": 326,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[18.10517198 81.08710388 50.92511687 40.1436419 11.61321423 62.30267348\n",
" 11.29760591 51.48817591 13.4247326 20.46938934]\n",
"[18. 81. 50. 40. 11. 62. 11. 51. 13. 20.]\n",
"[18. 81. 50. 40. 11. 62. 11. 51. 13. 20.]\n",
"[18. 81. 50. 40. 11. 62. 11. 51. 13. 20.]\n",
"[18, 81, 50, 40, 11, 62, 11, 51, 13, 20]\n",
"[18, 81, 50, 40, 11, 62, 11, 51, 13, 20]\n"
]
}
],
"source": [
"import re\n",
"# 36. Extract the integer part of a random array using 5 different methods (★★☆)\n",
"A = np.random.rand(10) * 100\n",
"print(A)\n",
"# ans 1\n",
"print(np.round(A - 0.5, 0))\n",
"# ans 2\n",
"print(np.ceil(A)-1)\n",
"# ans 3\n",
"print(np.floor(A))\n",
"# ans 4\n",
"print(list(map(np.int, A)))\n",
"# ans 5\n",
"print([int(re.sub(r'([0-9]+)\\.[0-9]+', r'\\1', str(a))) for a in A])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#37. Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆)\n",
"\n",
"#38. Consider a generator function that generates 10 integers and use it to build an array (★☆☆)\n",
"#39. Create a vector of size 10 with values ranging from 0 to 1, both excluded (★★☆)\n",
"#40. Create a random vector of size 10 and sort it (★★☆)"
]
}
],
"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
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment