{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "cd0bceb7", "metadata": { "id": "cd0bceb7" }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "id": "68b0b0db", "metadata": { "id": "68b0b0db" }, "source": [ "# Numpy Excersise" ] }, { "cell_type": "markdown", "id": "c489958c", "metadata": { "id": "c489958c" }, "source": [ "本次測驗分為兩個部分:\n", "\n", "1. Python 基礎\n", "2. Numpy 基礎\n", "\n", "兩個部分各 3 ~ 4 題,請善用工具及提示。**google 是你的一個好幫手**,如果有任何問題不妨參考前人的智慧,若有任何問題,也可隨時向助教尋求幫助。" ] }, { "cell_type": "markdown", "id": "7af2a713", "metadata": { "id": "7af2a713" }, "source": [ "# Python 基礎" ] }, { "cell_type": "markdown", "id": "411096a6", "metadata": { "id": "411096a6" }, "source": [ "## 1. 質數判斷" ] }, { "cell_type": "markdown", "id": "47f48f1c", "metadata": { "id": "47f48f1c" }, "source": [ "**請定義一函式 prime ,輸入為一整數,該函式檢查數字是否為質數。**\n", "\n", "(若 \"是\",回傳 **True**;反之,則回傳 **False**。)" ] }, { "cell_type": "code", "execution_count": null, "id": "b7129256", "metadata": { "id": "b7129256" }, "outputs": [], "source": [ "def prime(number):\n", " # writting your answer here\n", " '''\n", " input parameter:\n", " 1. number:一整數,用於判斷是否為整數\n", " '''\n", " pass" ] }, { "cell_type": "code", "execution_count": null, "id": "b74f40ac", "metadata": { "id": "b74f40ac" }, "outputs": [], "source": [ "# validation cell, do not modifiy this cell\n", "assert prime(-17) == False\n", "assert prime(0) == False\n", "assert prime(2) == True\n", "assert prime(6) == False\n", "assert prime(17) == True" ] }, { "cell_type": "markdown", "id": "4b2466bf", "metadata": { "id": "4b2466bf" }, "source": [ "## 2. 購物清單" ] }, { "cell_type": "markdown", "id": "885bf4e0", "metadata": { "id": "885bf4e0" }, "source": [ "給定一購物清單 shopping_list,當中包含多個 list。\n", "\n", "每個 list 都存有一日期、購物項目;每個購物項目皆為三元組,依序為品項、數量、價格。\n", "\n", "**請幫忙將購物清單整理成 dictionary 的格式,並用以下格式輸出:**\n", "\n", "* **dictionary 格式:(注意:該字典格式為 \"三層\" 結構)**\n", "\n", "\n", "\n", "* **輸出格式**\n", "\n", "\n", "\n", "\n" ] }, { "cell_type": "code", "source": [ "# define variable cell, do not modifiy this cell\n", "shopping_list = [['2020.01/24', ('milk', 15, 129.0), ('noodle', 1, 70.0), ('candy', 10, 2.5)],\n", " ['2020.02/24', ('milk', 15, 129.0), ('noodle', 3, 70.0), ('candy', 10, 2.5)],\n", " ['2020.02/24', ('milk', 4, 129.0), ('meat', 2, 499.5), ('cookie', 10, 3)],\n", " ['2022/12-25', ('milk', 15, 129.0), ('coffee', 5, 25), ('cookie', 15, 3), ('meat', 10, 499.5)],\n", " ['2023-12/26', ('coffee', 7, 25), ('noodle', 2, 70.0), ('candy', 15, 2.5)]]" ], "metadata": { "id": "YVX2cU6odEzg" }, "id": "YVX2cU6odEzg", "execution_count": null, "outputs": [] }, { "cell_type": "code", "execution_count": null, "id": "2d4159ec", "metadata": { "id": "2d4159ec" }, "outputs": [], "source": [ "# writting your answer here\n" ] }, { "cell_type": "markdown", "id": "b9c14b9f", "metadata": { "id": "b9c14b9f" }, "source": [ "## 3. 捲積運算" ] }, { "cell_type": "markdown", "id": "6c86ae90", "metadata": { "id": "6c86ae90" }, "source": [ "捲積運算是透過一個較小的矩陣 (Filters,或稱卷積核 kernel),平移滑動數個位置 (stride) 在另一矩陣做 element-wise 的相乘並計算其總和得計算方式。 \n", "此方法常用在提取圖像的特徵 (稱作 Feature Map)。如下圖所示:\n", "\n", "\n", "\n", "**請試著定義一函式 convolution,輸入為一代表圖像的矩陣 (img) 和卷積核 (kernel),並回傳計算後的矩陣。平移數 (stride) 皆為 1。**" ] }, { "cell_type": "code", "source": [ "# define variable cell, do not modifiy this cell\n", "img = np.arange(1, 26, 1).reshape(5, 5)\n", "kernel = np.ones((3,3))" ], "metadata": { "id": "1ZFcykTLp3dj" }, "id": "1ZFcykTLp3dj", "execution_count": null, "outputs": [] }, { "cell_type": "code", "execution_count": null, "id": "e82a0ed1", "metadata": { "id": "e82a0ed1" }, "outputs": [], "source": [ "def convolution(img, kernel):\n", " # writting your answer here\n", " '''\n", " input parameter:\n", " 1. img:一個 np.array 的矩陣。\n", " 2. kernel:一個 np.array 的矩陣,用來遍歷 img 並計算結果。\n", " '''\n", " pass" ] }, { "cell_type": "code", "execution_count": null, "id": "0e3d6c66", "metadata": { "id": "0e3d6c66" }, "outputs": [], "source": [ "# validation cell, do not modifiy this cell\n", "img = np.arange(1, 26, 1).reshape(5, 5)\n", "kernel = np.ones((3,3))\n", "assert (convolution(img, kernel) == [[ 63., 72., 81.],\n", " [108., 117., 126.],\n", " [153., 162., 171.]]).all()" ] }, { "cell_type": "markdown", "id": "ab935c6e", "metadata": { "id": "ab935c6e" }, "source": [ "## 4. 凱薩加密" ] }, { "cell_type": "markdown", "id": "2ac5fa6d", "metadata": { "id": "2ac5fa6d" }, "source": [ "凱薩加密是一種簡單且廣為人知的替換加密技術。\n", "明文 (原始文本) 中的所有字母都在字母表上向後(或向前)按照一個固定數目進行偏移後被替換成密文 (加密後的文本)。\n", "這偏移量便是加密技術中所謂的金鑰 (key),若沒有金鑰便難以進行解密的動作。\n", "\n", "**請試著定義一個函式 caesar 符合凱薩加密的方法,輸入參數分別為密文(ciphertext)以及金鑰(key),並返回明文。**\n", "\n", "\n", "\n", "**hint: 該題只考慮小寫字母 (a ~ z),不做特殊符號的處理**" ] }, { "cell_type": "code", "execution_count": null, "id": "3fb60c49", "metadata": { "id": "3fb60c49" }, "outputs": [], "source": [ "def caesar(ciphertext, key):\n", " # writting your answer here\n", " '''\n", " input parameter:\n", " 1. ciphertext:密文\n", " 2. key:金鑰\n", " '''\n", "\n", " pass" ] }, { "cell_type": "code", "execution_count": null, "id": "265e1f36", "metadata": { "id": "265e1f36" }, "outputs": [], "source": [ "# validation cell, do not modifiy this cell\n", "assert caesar('tqxxa iadxp', 12) == 'hello world'\n", "assert caesar('em egehiqc', 56) == 'ai academy'\n", "assert caesar('fytc y lgac byw.', 24) == 'have a nice day.'" ] }, { "cell_type": "markdown", "id": "b2fb9c4a", "metadata": { "id": "b2fb9c4a" }, "source": [ "# Numpy 基礎" ] }, { "cell_type": "markdown", "id": "8cf804e1", "metadata": { "id": "8cf804e1" }, "source": [ "## 5. 維度" ] }, { "cell_type": "markdown", "id": "3daf2736", "metadata": { "id": "3daf2736" }, "source": [ "有兩陣列 $a$、$b$。$a$ 為一存放兩筆資料的陣列;$b$ 表示 $a$ 的資料分別需要乘上多少倍率。\n", "\n", "**請透過兩種方式:**\n", "\n", "**1. 函式 method_1 使用 reshape。**\n", "\n", "**2. 函式 method_2 使用 squeeze 、 expand_dims。**\n", "\n", "**使得 $a$ x $b$ 符合題目要求。**" ] }, { "cell_type": "code", "execution_count": null, "id": "5a35de6b", "metadata": { "id": "5a35de6b" }, "outputs": [], "source": [ "# define variable cell, do not modifiy this cell\n", "a = np.array([[[[1, 2, 3, 4],\n", " [1, 2, 3, 4],\n", " [1, 2, 3, 4]]],\n", " [[[1, 2, 3, 4],\n", " [1, 2, 3, 4],\n", " [1, 2, 3, 4]]]])\n", "b = np.array([[1], [2]])" ] }, { "cell_type": "code", "execution_count": null, "id": "568d6f60", "metadata": { "id": "568d6f60" }, "outputs": [], "source": [ "def method_1(a, b):\n", " # writting your answer here\n", " '''\n", " input parameter:\n", " 1. a、b:皆一個 np.array 的矩陣,試著完成題目要求。\n", " '''\n", " pass" ] }, { "cell_type": "code", "execution_count": null, "id": "8c283c55", "metadata": { "id": "8c283c55" }, "outputs": [], "source": [ "def method_2(a, b):\n", " # writting your answer here\n", " '''\n", " input parameter:\n", " 1. a、b:皆一個 np.array 的矩陣,試著完成題目要求。\n", " '''\n", " pass" ] }, { "cell_type": "code", "execution_count": null, "id": "784f3025", "metadata": { "id": "784f3025" }, "outputs": [], "source": [ "# validation cell, do not modifiy this cell\n", "assert (method_1(a, b) == [[[1, 2, 3, 4],[1, 2, 3, 4],[1, 2, 3, 4]],\n", " [[2, 4, 6, 8],[2, 4, 6, 8],[2, 4, 6, 8]]]).all()\n", "\n", "assert (method_2(a, b) == [[[1, 2, 3, 4],[1, 2, 3, 4],[1, 2, 3, 4]],\n", " [[2, 4, 6, 8],[2, 4, 6, 8],[2, 4, 6, 8]]]).all()" ] }, { "cell_type": "markdown", "id": "1d9cf9c1", "metadata": { "id": "1d9cf9c1" }, "source": [ "## 6. 維度擴充並重複填充" ] }, { "cell_type": "markdown", "id": "eb253ce8", "metadata": { "id": "eb253ce8" }, "source": [ "在後續的深度學習課程,我們可能在處理數據的時候碰到某些情況\n", "\n", "**透過一個 $n$ 維數組來與另一個 $n+1$ 維數組做一對一運算**\n", "\n", "這時候該怎麼辦呢?我們會需要先將 $n$ 維數組填充成 $n+1$ 維數組,並重複 $k$ 次。\n", "\n", "如: \n", "一個 $n$ 維數組,填充第 1 維度成 $n+1$ 維,並重複 4 次\n", "\n", "\n", "\n", "一個 $n$ 維數組,填充第 2 維度成 $n+1$ 維,並重複 4 次\n", "\n", "\n", "\n", "**請試著定義一函式 expand,輸入為一陣列 (array) 、維度 (axis)、和次數 (k),可以透過參數改變需要填充的維度以及重複的次數,並回傳結果。**" ] }, { "cell_type": "code", "source": [ "# define variable cell, do not modifiy this cell\n", "a = np.array([[1, 2, 3], [2, 4, 6]])" ], "metadata": { "id": "tS8_K3Vid2OV" }, "id": "tS8_K3Vid2OV", "execution_count": null, "outputs": [] }, { "cell_type": "code", "execution_count": null, "id": "2debf1e2", "metadata": { "id": "2debf1e2" }, "outputs": [], "source": [ "def expand(array, axis, k):\n", " # writting your answer here\n", " '''\n", " input parameter:\n", " 1. array:一個 np.array 的矩陣。\n", " 2. axis:需要填充的維度。\n", " 3. k:需要重複的次數。\n", " '''\n", " pass" ] }, { "cell_type": "code", "source": [ "assert (expand(a, 0, 4)==np.array([[[1, 2, 3], [2, 4, 6]], [[1, 2, 3], [2, 4, 6]],\n", " [[1, 2, 3], [2, 4, 6]], [[1, 2, 3], [2, 4, 6]]])).all() # 注意,填充第幾個維度是從零開始算\n", "assert (expand(a, 1, 4)==[[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]],\n", " [[2, 4, 6], [2, 4, 6], [2, 4, 6], [2, 4, 6]],]).all() # 注意,填充第幾個維度是從零開始算" ], "metadata": { "id": "tsrh0bjugyIm" }, "id": "tsrh0bjugyIm", "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "id": "0cfd414d", "metadata": { "id": "0cfd414d" }, "source": [ "## 7. 矩陣總和" ] }, { "cell_type": "markdown", "id": "50e5678f", "metadata": { "id": "50e5678f" }, "source": [ "**請試著定義一函式 matrixElementsSum,輸入為一矩陣 (X),計算矩陣中所有數字的總和,若該值的上方 \"出現過\" 0,則不予計算**\n", "\n", "例如:一矩陣如圖\n", "\n", "\n", "\n", "由於紅色數字上方皆沒有 0 出現,故總數為 1 + 2 + 3 + 5 = 11 \n", "因此輸出應為:matrixElementsSum(matrix) = 11\n", "\n", "**hint:np.where、.item、.itemset**" ] }, { "cell_type": "code", "source": [ "# define variable cell, do not modifiy this cell\n", "matrix_X = [[0, 1, 2, 3],\n", " [4, 5, 0, 0],\n", " [6, 0, 7, 8]]" ], "metadata": { "id": "oywrnV8ediK-" }, "id": "oywrnV8ediK-", "execution_count": null, "outputs": [] }, { "cell_type": "code", "execution_count": null, "id": "613324a1", "metadata": { "id": "613324a1" }, "outputs": [], "source": [ "def matrixElementsSum(M):\n", " # writting your answer here\n", " '''\n", " input parameter:\n", " 1. M:一個 np.array 的矩陣。是求出題目要求。\n", " '''\n", " pass" ] }, { "cell_type": "code", "execution_count": null, "id": "9625b12a", "metadata": { "id": "9625b12a" }, "outputs": [], "source": [ "assert matrixElementsSum(matrix_X) == 11" ] } ], "metadata": { "colab": { "provenance": [], "toc_visible": true }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.12" } }, "nbformat": 4, "nbformat_minor": 5 }