{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "ggFUZOmm3V8H" }, "source": [ "## $\\Large{Numpy\\; 練習題}$" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "6KNEKLNu3V8H" }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": { "id": "0ByhvaxjVhrH" }, "source": [ "---\n", "# 1-Dimension Array" ] }, { "cell_type": "markdown", "source": [ "## Exercise 1\n", "使用np.arrange 函數創建一個數值從10到49的一維陣列" ], "metadata": { "id": "086J8NWDX4SI" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "nB7ScIE23V8M" }, "outputs": [], "source": [ "np.arange(10, 50)" ] }, { "cell_type": "markdown", "metadata": { "id": "JEJqL9pdVhrJ" }, "source": [ "## Exercise 2\n", "請建立一個陣列,裡頭包含0~101中所有完全平方數(ex. 1, 4, 9...)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "AosO2-L0VhrJ" }, "outputs": [], "source": [ "np.array([i**2 for i in range(11)])" ] }, { "cell_type": "markdown", "source": [ "## Exercise 3\n", "反轉陣列x使得陣列內的元素從尾排到頭" ], "metadata": { "id": "lEnCW1RiWhka" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "9468qVTX3V8R" }, "outputs": [], "source": [ "x = np.array([2, 0, 3, 1, 4, 7])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "-mnCPhJLVhrK" }, "outputs": [], "source": [ "x[::-1]" ] }, { "cell_type": "markdown", "source": [ "## Exercise 4\n", "將x陣列內數值介於3到8(包含3和8)的元素取負號" ], "metadata": { "id": "4myjnFgOWk3n" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "D9v1tbiZVhrL" }, "outputs": [], "source": [ "X = np.array([6, 2, 8, 3, 0, 1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "4CH3eqeyVhrL" }, "outputs": [], "source": [ "X[np.where((X >= 3) & (X <= 8))] = X[np.where((X >= 3) & (X <= 8))]*-1\n", "X" ] }, { "cell_type": "markdown", "source": [ "## Exercise 5\n", "創建一個長度為10的隨機數字陣列,並且將最大值取代為0" ], "metadata": { "id": "ClTRv4KGWoB5" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "kI3-7BgGVhrM" }, "outputs": [], "source": [ "X = np.random.randn(10)\n", "print(X)\n", "X[np.argmax(X)] = 0\n", "print(X)" ] }, { "cell_type": "markdown", "source": [ "## Exercise 6\n", "從陣列x中取出符合以下條件的子集\n", "\n", "(1) 不能被 3 整除\n", "\n", "(2) 可以被 3 整除\n", "\n", "(3) 可以被 3 或 5 整除" ], "metadata": { "id": "7ISF97dEWqAM" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "Wjr6WONwVhrM" }, "outputs": [], "source": [ "x = np.array([3, 4, 6, 10, 24, 89, 45, 43, 46, 99, 100])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "r8fj6jExVhrN" }, "outputs": [], "source": [ "x[x % 3 != 0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "wNuNxzc3VhrN" }, "outputs": [], "source": [ "x[x % 3 == 0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "aXLPS6rwVhrN" }, "outputs": [], "source": [ "x[(x % 3 == 0) | (x % 5 == 0)]" ] }, { "cell_type": "markdown", "metadata": { "id": "utrw8C92VhrN" }, "source": [ "## Exercise 7\n", "使用np.arange建立從2019年1月到12月共12個月的陣列資料" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "xfyj4hHAVhrN" }, "outputs": [], "source": [ "np.arange('2019-01', '2020-01', dtype='datetime64[M]')" ] }, { "cell_type": "markdown", "metadata": { "id": "DSAOYSWzVhrO" }, "source": [ "---\n", "# n-Dimension Array" ] }, { "cell_type": "markdown", "metadata": { "id": "jUVuEO0mVhrO" }, "source": [ "## Exercise 8\n", "請以ndim與shape兩個用法印出陣列x的維度數目與各維度的長度" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "rU71Nt_eVhrO" }, "outputs": [], "source": [ "x = np.array([[[[[[[[[[[[[[[[[5, 4, 3, 2, 1]]]], [[[[10, 9, 8, 7, 6]]]]]]]]]]]]]]]]])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "CWq9XOLbVhrO" }, "outputs": [], "source": [ "print(x.ndim)\n", "print(x.shape)" ] }, { "cell_type": "markdown", "source": [ "## Exercise 9\n", "使用np.random.random 函數創建一個3x3x3的陣列" ], "metadata": { "id": "qF06hgaBWtKa" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "WdVy_Z683V8V" }, "outputs": [], "source": [ "np.random.random((3, 3, 3))" ] }, { "cell_type": "markdown", "source": [ "## Exercise 10\n", "創建一個5x5且數值介於0到4之間的整數陣列" ], "metadata": { "id": "CZvVkfD_Wvjb" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "sAAJVPeWVhrP" }, "outputs": [], "source": [ "np.random.randint(0, 4, size=(5, 5))" ] }, { "cell_type": "markdown", "source": [ "## Exercise 11\n", "創建一個擁有隨機數值且形狀為10x10的陣列,並且使用np.min 和 np.max兩函數找出陣列中的最大值/最小值" ], "metadata": { "id": "BtiA2NduW0s2" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "t2j9bQw33V8Z" }, "outputs": [], "source": [ "x = np.random.random(size=(10, 10))\n", "print(np.min(x))\n", "print(np.max(x))" ] }, { "cell_type": "markdown", "metadata": { "id": "Vo77LjZBVhrP" }, "source": [ "## Exercise 12\n", "使用reshape函數將陣列x轉成最後一個維度的長度為1且第一個維度為3的3維陣列" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "YZhiG0DmVhrP" }, "outputs": [], "source": [ "x = np.arange(9)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "drY0thn2VhrP" }, "outputs": [], "source": [ "x.reshape(3, -1, 1)" ] }, { "cell_type": "markdown", "metadata": { "id": "Mw3Zf-91VhrQ" }, "source": [ "## Exercise 13\n", "請針對x陣列中的每欄資料做累積加總\n", "\n", "註:加總後的陣列數值將為 [[2, 6, 3], [7, 15, 4]]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "euFkrzz9VhrQ" }, "outputs": [], "source": [ "x = np.array([2, 6, 3, 5, 9, 1]).reshape(2, -1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "vjqIJCQyVhrQ" }, "outputs": [], "source": [ "np.cumsum(x, axis=0)" ] }, { "cell_type": "markdown", "source": [ "## Exercise 14\n", "陣列標準化: 將x陣列內所有的元素除以x陣列內元素的最大值" ], "metadata": { "id": "HWEjxajAW5IR" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "EjXbfUeEVhrQ" }, "outputs": [], "source": [ "x = np.random.randn(6).reshape(2, 3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "uRIzRKOwVhrQ" }, "outputs": [], "source": [ "x = x/x.max()\n", "print(x)" ] }, { "cell_type": "markdown", "metadata": { "id": "cWnuFM_TVhrR" }, "source": [ "## Exercise 15\n", "請計算出x陣列中相鄰數字的差異並同樣存成np.array型態\n", "\n", "註:由於x陣列有6個數字,因此相鄰數字的差異共會有5個數值,依序為 [-151, 224, -158, -88, 572]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "2J2PDtM_VhrR" }, "outputs": [], "source": [ "x = np.array([183, 32, 256, 98, 10, 582])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "J0EoNohkVhrR" }, "outputs": [], "source": [ "new_x = x[1:] - x[:-1]\n", "print(new_x)" ] }, { "cell_type": "markdown", "metadata": { "id": "7dQWwsWPVhrR" }, "source": [ "## Exercise 16\n", "請將x陣列中所有數值可被3整除的元素索引列出\n", "\n", "註:由於x為二維陣列,單一個元素的索引會有兩個數字(ex.[1, 0])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "_jJ6AVKAVhrR" }, "outputs": [], "source": [ "x = np.arange(20).reshape(4, 5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "mUuoQefNVhrR" }, "outputs": [], "source": [ "np.argwhere(x % 3 == 0)" ] }, { "cell_type": "markdown", "source": [ "## Exercise 17\n", "使用np.pad 函數在陣列x外圍加一層0,使得原本3x3的陣列變為5x5" ], "metadata": { "id": "H3qVPaW2aYnE" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "1hp3gjzn3V8e" }, "outputs": [], "source": [ "x = np.ones(shape=(3, 3))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "VVIu1v5AVhrS" }, "outputs": [], "source": [ "np.pad(x, pad_width=1, mode='constant')" ] }, { "cell_type": "markdown", "metadata": { "id": "B3TnICAIVhrS" }, "source": [ "## Exercise 18\n", "計算x與y兩兩元素的乘積並做加總(答案應為32)\n", "\n", "註:上述的做法其實就是矩陣內積,因此有超過兩種以上的作法可以計算出此答案,大家可以試試看自己會幾種" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "1VtWuU7HVhrS" }, "outputs": [], "source": [ "x = np.array([[1, 2, 3]])\n", "y = np.array([[4, 5, 6]])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "y9gm8CbzVhrS" }, "outputs": [], "source": [ "np.matmul(x, y.T)" ] }, { "cell_type": "markdown", "metadata": { "id": "cv8G5wMjVhrS" }, "source": [ "## Exercise 19\n", "使用np.concatenate函數將陣列x與陣列y合併在一起,使其與```np.append(x, y, axis = 1)```結果相同" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "T-1OjJXcVhrS" }, "outputs": [], "source": [ "x = np.array([[1, 2, 3]])\n", "y = np.array([[4, 5, 6]])\n", "\n", "np.append(x, y, axis=1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "p5GBd6MwVhrT" }, "outputs": [], "source": [ "np.concatenate((x, y), axis=1)" ] }, { "cell_type": "markdown", "metadata": { "id": "QaKEuybI3V9E" }, "source": [ "### 請參考 [100-numpy-exercises](https://github.com/rougier/numpy-100/blob/master/100_Numpy_exercises.ipynb) 做更多 numpy 的操作練習" ] } ], "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.7.3" }, "colab": { "provenance": [] } }, "nbformat": 4, "nbformat_minor": 0 }