Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
382 changes: 382 additions & 0 deletions homework/m1_numpy.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,382 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "dedf1249",
"metadata": {},
"source": [
"\"\"\"\n",
"M1 NumPy 向量化思維 — 課後作業\n",
"================================\n",
"請完成以下每個函式,用 NumPy 向量化寫法(不要 for-loop)。\n",
"完成後 git push,GitHub Actions 會自動批改並顯示成績與解答。\n",
"\n",
"提示:\n",
"- np.array, np.where, np.argsort\n",
"- 布林遮罩: arr[arr > 10]\n",
"- 統計: .sum(), .mean(), .max(), .min()\n",
"\"\"\""
]
},
{
"cell_type": "markdown",
"id": "60c0496d",
"metadata": {},
"source": [
"# ============================================================\n",
"# 🟢 送分題(每題 10 分,共 30 分)\n",
"# ============================================================"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "5567ab7e",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "de47a63f",
"metadata": {},
"outputs": [],
"source": [
"\n",
"import numpy as np\n",
"\n",
"\n",
"\n",
"def green_mean():\n",
" \"\"\"建立 [10, 20, 30, 40, 50],回傳所有元素的平均值 (float)\"\"\"\n",
" # TODO: 你的程式碼\n",
" pass\n",
"\n",
"\n",
"def green_double():\n",
" \"\"\"建立 [10, 20, 30, 40, 50],回傳所有元素乘以 2 的 ndarray\"\"\"\n",
" # TODO: 你的程式碼\n",
" pass\n",
"\n",
"\n",
"def green_filter():\n",
" \"\"\"建立 [10, 20, 30, 40, 50],回傳大於 25 的元素 (ndarray)\"\"\"\n",
" # TODO: 你的程式碼\n",
" pass\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "378063f7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"30.0\n"
]
}
],
"source": [
"import numpy as np\n",
"\n",
"#def green_mean():\n",
" #\"\"\"建立 [10, 20, 30, 40, 50],回傳所有元素的平均值 (float)\"\"\"\n",
" # TODO: 你的程式碼\n",
" \n",
"arr = np.array([10, 20, 30, 40, 50])\n",
"avg_arr = np.mean(arr)\n",
"print(avg_arr)\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "d62c7972",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 20 40 60 80 100]\n"
]
}
],
"source": [
"#def green_double():\n",
"#\"\"\"建立 [10, 20, 30, 40, 50],回傳所有元素乘以 2 的 ndarray\"\"\"\n",
"# TODO: 你的程式碼\n",
"\n",
"arr = np.array([10, 20, 30, 40, 50])\n",
"twotimes_arr = np.multiply(arr, 2)\n",
"print(twotimes_arr)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "d70bddec",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[30 40 50]\n"
]
}
],
"source": [
"#def green_filter():\n",
" #\"\"\"建立 [10, 20, 30, 40, 50],回傳大於 25 的元素 (ndarray)\"\"\"\n",
" # TODO: 你的程式碼\n",
"arr = np.array([10, 20, 30, 40, 50])\n",
"biggerthan25_arr = arr[arr > 25]\n",
"print(biggerthan25_arr)"
]
},
{
"cell_type": "markdown",
"id": "df18d551",
"metadata": {},
"source": [
"# ============================================================\n",
"# 🟡 核心題(每題 15 分,共 45 分)\n",
"# 以下函式會接收從 products.csv 讀出的 prices, stocks 陣列\n",
"# ============================================================"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "02a97124",
"metadata": {},
"outputs": [],
"source": [
"\n",
"def yellow_expensive_count(prices):\n",
" \"\"\"回傳單價 > 1000 的商品數量 (int)\"\"\"\n",
" # TODO: 你的程式碼\n",
" pass\n",
"\n",
"\n",
"def yellow_top3_stock_indices(stocks):\n",
" \"\"\"\n",
" 回傳庫存最多的前 3 個商品的索引位置 (ndarray, 由大到小排)\n",
" 提示:np.argsort\n",
" \"\"\"\n",
" # TODO: 你的程式碼\n",
" pass\n",
"\n",
"\n",
"def yellow_restock_cost(prices, stocks):\n",
" \"\"\"\n",
" 單價 < 500 的商品,每種各進貨 50 個,回傳總花費 (float/int)\n",
" 提示:布林遮罩 + .sum()\n",
" \"\"\"\n",
" # TODO: 你的程式碼\n",
" pass\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "24269aa8",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"df = pd.read_csv(r\"D:\\python_workstation\\iSpan_python-DA-cookbooks\\python-da-homework-2026-main\\python-da-homework-2026-main\\datasets\\ecommerce\\products.csv\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "e328bf6b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"16"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"\n",
"DATA = pd.read_csv(r\"D:\\python_workstation\\iSpan_python-DA-cookbooks\\python-da-homework-2026-main\\python-da-homework-2026-main\\datasets\\ecommerce\\products.csv\")\n",
"prices = DATA.iloc[:, 3].to_numpy()\n",
"stocks = DATA.iloc[:, 4].to_numpy()\n",
"#def yellow_expensive_count(prices):\n",
"#\"\"\"回傳單價 > 1000 的商品數量 (int)\"\"\"\n",
"# TODO: 你的程式碼\n",
"\n",
"prices \n",
"prices[prices > 1000]\n",
"len(prices[prices > 1000])"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "8f9d602b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([10, 0, 17, 22, 24, 5, 25, 2, 11, 13, 23, 1, 29, 20, 14, 12, 16,\n",
" 28, 21, 27, 19, 15, 4, 9, 26, 6, 18, 3, 8, 7])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"\n",
"DATA = pd.read_csv(r\"D:\\python_workstation\\iSpan_python-DA-cookbooks\\python-da-homework-2026-main\\python-da-homework-2026-main\\datasets\\ecommerce\\products.csv\")\n",
"prices = DATA.iloc[:, 3].to_numpy()\n",
"stocks = DATA.iloc[:, 4].to_numpy()\n",
"\n",
"#def yellow_top3_stock_indices(stocks):\n",
" #\"\"\"\n",
" #回傳庫存最多的前 3 個商品的索引位置 (ndarray, 由大到小排)\n",
" #提示:np.argsort\n",
" #\"\"\"\n",
"\n",
"\n",
"stocks\n",
"np.argsort(stocks)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "62a610a4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"np.int64(120300)"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#def yellow_restock_cost(prices, stocks):\n",
"# \"\"\"\n",
"#單價 < 500 的商品,每種各進貨 50 個,回傳總花費 (float/int)\n",
"#提示:布林遮罩 + .sum()\n",
"#\"\"\"\n",
"# TODO: 你的程式碼\n",
" \n",
"prices[prices<500] #找出單價小於500的價格\n",
"prices[prices<500] * 50 #每個商品進貨50個\n",
"sum(prices[prices<500] * 50) #總價格"
]
},
{
"cell_type": "markdown",
"id": "3fa5a64a",
"metadata": {},
"source": [
"# ============================================================\n",
"# 🔴 挑戰題(25 分)\n",
"# ============================================================"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b62c2097",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1312.5, 1202.6, 203. , 426.3, 1075.9, 392.7, 766.5, 1238.3,\n",
" 526.4, 1677.2, 228.9, 219.8, 1292.2, 267.4, 189.7, 1460.9,\n",
" 133.7, 700. , 1132.6, 331.8, 410.9, 558. , 141.3, 1724.4,\n",
" 1386.9, 1329.3, 1755.9, 1935. , 1627. , 1506. ])"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"#def red_double11_prices(prices, stocks):\n",
"#\"\"\"\n",
"#雙 11 定價規則(必須向量化,不能用 for-loop):\n",
"#- 庫存 >= 100:打 7 折\n",
"#- 庫存 20~99:打 9 折\n",
"#- 庫存 < 20:原價\n",
"#回傳每個商品的雙 11 售價 (ndarray)\n",
"#提示:np.where 可以巢狀使用\n",
"#\"\"\"\n",
"\n",
"\n",
"prices_07 = prices[stocks >= 100] *0.7\n",
"prices_09 = prices[(stocks >= 20) & (stocks <= 99)] *0.9\n",
"prices_original = prices[stocks < 20]\n",
"\n",
"# np.concatenate 沿指定軸連接多個陣列\n",
"total_price = np.concatenate([prices_07, prices_09, prices_original])\n",
"total_price\n",
"\n",
" \n",
" "
]
}
],
"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.12.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading
Loading