{ "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": "E7y13BffZFY6" }, "source": [ "---\n", "# 1-Dimension Array" ] }, { "cell_type": "markdown", "source": [ "## Exercise 1\n", "使用np.arrange 函數創建一個數值從10到49的一維陣列" ], "metadata": { "id": "rUF_-tnCZJ6s" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "nB7ScIE23V8M" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "id": "6FR8HFzLZFY8" }, "source": [ "## Exercise 2\n", "請建立一個陣列,裡頭包含0~101中所有完全平方數(ex. 1, 4, 9...)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "2HLC2YBtZFY8" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "source": [ "## Exercise 3\n", "反轉陣列x使得陣列內的元素從尾排到頭" ], "metadata": { "id": "xl9t5OhPZM9D" } }, { "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": "M3enqNUsZFY9" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "source": [ "## Exercise 4\n", "將x陣列內數值介於3到8(包含3和8)的元素取負號" ], "metadata": { "id": "LpSJ8JYLZPSp" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "9iQpH5dZZFY9" }, "outputs": [], "source": [ "X = np.array([6, 2, 8, 3, 0, 1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "if9bGNLLZFY-" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "source": [ "## Exercise 5\n", "創建一個長度為10的隨機數字陣列,並且將最大值取代為0" ], "metadata": { "id": "P1FKNG31ZRdx" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "7Of-nk4aZFY-" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "source": [ "## Exercise 6\n", "從陣列x中取出符合以下條件的子集\n", "\n", "(1) 不能被 3 整除\n", "\n", "(2) 可以被 3 整除\n", "\n", "(3) 可以被 3 或 5 整除" ], "metadata": { "id": "Ktkv0c0NZT63" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "iXB4gnd1ZFY-" }, "outputs": [], "source": [ "x = np.array([3, 4, 6, 10, 24, 89, 45, 43, 46, 99, 100])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "b3zi1ajEZFY_" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "id": "dWol65hbZFY_" }, "source": [ "## Exercise 7\n", "使用np.arange建立從2019年1月到12月共12個月的陣列資料\n", "\n", "範例結果如下\n", "" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "hCasCdc1ZFY_" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "id": "EOC-PxmZZFY_" }, "source": [ "---\n", "# n-Dimension Array" ] }, { "cell_type": "markdown", "metadata": { "id": "PPpaEeZwZFY_" }, "source": [ "## Exercise 8\n", "請以ndim與shape兩個用法印出陣列x的維度數目與各維度的長度" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "Qseed7RJZFY_" }, "outputs": [], "source": [ "x = np.array([[[[[[[[[[[[[[[[[5, 4, 3, 2, 1]]]], [[[[10, 9, 8, 7, 6]]]]]]]]]]]]]]]]])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "Fz2HW15wZFZA" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "source": [ "## Exercise 9\n", "使用np.random.random 函數創建一個3x3x3的陣列" ], "metadata": { "id": "Ktmkc3uUZWyz" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "WdVy_Z683V8V" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "source": [ "## Exercise 10\n", "創建一個5x5且數值介於0到4之間的整數陣列" ], "metadata": { "id": "iwJPoGp0ZZZR" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "llG_MeMCZFZA" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "source": [ "## Exercise 11\n", "創建一個擁有隨機數值且形狀為10x10的陣列,並且使用np.min 和 np.max兩函數找出陣列中的最大值/最小值" ], "metadata": { "id": "APrvxc_8ZbYv" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "t2j9bQw33V8Z" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "id": "vUu_289XZFZC" }, "source": [ "## Exercise 12\n", "使用reshape函數將陣列x轉成最後一個維度的長度為1且第一個維度為3的3維陣列" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "zEQAIjKLZFZD" }, "outputs": [], "source": [ "x = np.arange(9)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "GChybyZhZFZD" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "id": "JbMAkxo6ZFZD" }, "source": [ "## Exercise 13\n", "請針對x陣列中的每欄資料做累積加總\n", "\n", "註:加總後的陣列數值將為 [[2, 6, 3], [7, 15, 4]]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "Rq8-7AhyZFZD" }, "outputs": [], "source": [ "x = np.array([2, 6, 3, 5, 9, 1]).reshape(2, -1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "YN6iQtBjZFZD" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "source": [ "## Exercise 14\n", "陣列標準化: 將x陣列內所有的元素除以x陣列內元素的最大值" ], "metadata": { "id": "UR31XV0lZdqA" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "vN4LagRkZFZE" }, "outputs": [], "source": [ "x = np.random.randn(6).reshape(2, 3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "qEpjr7MvZFZE" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "id": "AlPX8QKgZFZE" }, "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": "p871cBmoZFZE" }, "outputs": [], "source": [ "x = np.array([183, 32, 256, 98, 10, 582])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "oXcDnFXSZFZE" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "id": "KpmVRtkPZFZE" }, "source": [ "## Exercise 16\n", "請將x陣列中所有數值可被3整除的元素索引列出\n", "\n", "註:由於x為二維陣列,單一個元素的索引會有兩個數字(ex.[1, 0])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "IWVYXt9AZFZF" }, "outputs": [], "source": [ "x = np.arange(20).reshape(4, 5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "41X3ZjjSZFZF" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "source": [ "## Exercise 17\n", "使用np.pad 函數在陣列x外圍加一層0,使得原本3x3的陣列變為5x5" ], "metadata": { "id": "jh125XR_ZgAa" } }, { "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": "CTBxsdI3ZFZF" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "id": "NHkNTexpZFZF" }, "source": [ "## Exercise 18\n", "計算x與y兩兩元素的乘積並做加總(答案應為32)\n", "\n", "註:上述的做法其實就是矩陣內積,因此有超過兩種以上的作法可以計算出此答案,大家可以試試看自己會幾種" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "fk5aRUNnZFZF" }, "outputs": [], "source": [ "x = np.array([[1, 2, 3]])\n", "y = np.array([[4, 5, 6]])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "zcLJFYWuZFZF" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "id": "4EhpQz4QZFZG" }, "source": [ "## Exercise 19\n", "使用np.concatenate函數將陣列x與陣列y合併在一起,使其與```np.append(x, y, axis = 1)```結果相同" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "cLfEEa2WZFZG", "outputId": "27ca8cd7-d45b-47d3-9b90-d8723d48c0d6" }, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3, 4, 5, 6]])" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "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": "urUFsQMqZFZG" }, "outputs": [], "source": [] }, { "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 }