ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်
ဒီ lesson က Practice: Arrays & Indexing ထက် တစ်ဆင့်တက်ပြီး Intermediate/Advanced chapter တွေမှာ သင်ခဲ့တဲ့ broadcasting, axis aggregation, reshape, linear algebra, random module တွေကို ပေါင်းစပ်ကျင့်သုံးရတဲ့ task 4 ခု ပါဝင်ပါတယ်။ Task တစ်ခုစီက Part 1 ရဲ့ basic skill တွေကို base ယူပြီး real-world data pattern (price + tax, normalization, matrix multiplication) တွေနဲ့ ချိတ်ဆက်ထားပါတယ်။ Task တွေကို order အလိုက် လုပ်ရင် NumPy project တစ်ခုလုံးကို ကိုယ်တိုင် ပြန်တည်ဆောက်နိုင်တဲ့ confidence ရလာပါလိမ့်မယ်။
လေ့ကျင့်ခန်းများ
Task 1: product 4 ခု၊ store 3 ခုအတွက် price matrix (3, 4) ဖန်တီးပြီး, column အလိုက် tax rate မတူညီတဲ့ 1D array ([0.05, 0.08, 0.05, 0.10]) ကို broadcasting နဲ့ ပေါင်းထည့်ပါ။ Task 2: price matrix ပေါ်မှာ axis=0 (store-wise sum) နဲ့ axis=1 (product-wise sum) နှစ်မျိုးလုံးကို တွက်ပြီး, np.argmax() နဲ့ price အမြင့်ဆုံး product index ကို ရှာပါ။ Task 3: np.random.seed() ချပြီး np.random.rand(4, 4) ဖြင့် random 2D array တစ်ခု generate လုပ်ပြီး, (array - array.mean()) / array.std() formula နဲ့ normalize လုပ်ပါ။ Task 4: shape (2, 3) matrix တစ်ခုနဲ့ shape (3, 2) matrix တစ်ခုကို @ operator (သို့) np.dot() ဖြင့် matrix multiply လုပ်ပြီး result shape ကို confirm ပါ။
Code နမူနာ
import numpy as np
# Task 1: broadcasting tax onto price matrix
prices = np.array([
[1000, 2000, 1500, 3000],
[1100, 1900, 1600, 2800],
[950, 2100, 1550, 3200]
])
tax_rate = np.array([0.05, 0.08, 0.05, 0.10])
prices_with_tax = prices + (prices * tax_rate)
print("prices with tax:\n", prices_with_tax)
# Task 2: axis aggregation + argmax
store_totals = prices.sum(axis=1)
product_totals = prices.sum(axis=0)
top_product_index = np.argmax(product_totals)
print("store totals:", store_totals)
print("product totals:", product_totals, "top index:", top_product_index)
# Task 3: random + normalization
np.random.seed(1)
random_grid = np.random.rand(4, 4)
normalized = (random_grid - random_grid.mean()) / random_grid.std()
print("normalized:\n", normalized)
# Task 4: matrix multiplication
a = np.arange(6).reshape(2, 3)
b = np.arange(6).reshape(3, 2)
result = a @ b
print("result shape:", result.shape)
print(result)
Task 4 ခုစလုံးက tax ထည့်ပြီးသား price matrix, store/product totals နဲ့ top index, mean 0 အနီးကပ် normalized array, နဲ့ shape (2, 2) matrix multiplication result တွေကို print ထုတ်ပါလိမ့်မယ်။၅ မိနစ် စမ်းကြည့်
Task 4 ရဲ့ a, b matrix shape တွေကို (2, 3) နဲ့ (2, 3) အတူတူ ပြောင်းကြည့်ပြီး a @ b ဘာကြောင့် error တက်ရတယ်ဆိုတာ error message ကို ဖတ်ပြီး ရှင်းကြည့်ပါ (5 minutes)။
သတိလေးတစ်ချက်
a @ b (matrix multiplication) နဲ့ a * b (element-wise multiplication) က လုံးဝ operation မတူပါဘူး - matrix multiplication အတွက် a.shape[1] == b.shape[0] ဖြစ်ဖို့ လိုအပ်ပါတယ်။