ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်
Part 1 မှာ တည်ဆောက်ထားတဲ့ scores array ကို ဒီအဆင့်မှာ real analysis feature တွေ ထည့်ပါမယ်။ np.mean(), np.max(), np.min() စတဲ့ aggregation function တွေကို axis parameter နဲ့ ချိတ်ပြီး student တစ်ယောက်ချင်းစီရဲ့ average (axis=1) နဲ့ subject တစ်ခုချင်းစီရဲ့ class average (axis=0) ကို တွက်ပါမယ်။ ပြီးရင် broadcasting သုံးပြီး subject တိုင်းကို bonus point မတူညီစွာပေးတဲ့ curve (grade adjustment) ကို loop မရေးဘဲ တစ်ကြိမ်တည်း apply လုပ်ကြည့်ပါမယ်။ ဒီ pattern က real data analysis code မှာ အရမ်းအသုံးများပြီး Part 3 final report အတွက် direct-ကူညီပေးပါလိမ့်မယ်။
လက်တွေ့ ဆောက်ကြည့်မယ်
scores.mean(axis=1) သုံးပြီး student တစ်ယောက်ချင်းစီရဲ့ average score array ကို တွက်ပါ။ scores.mean(axis=0) သုံးပြီး subject တစ်ခုချင်းစီရဲ့ class average ကို တွက်ပါ။ np.argmax(student_avg) နဲ့ average အမြင့်ဆုံး student index ကို ရှာပါ။ subject တစ်ခုချင်းစီ ကွာခြားတဲ့ bonus array (curve = [2, 5, 0, 3]) တစ်ခုကို broadcasting နဲ့ scores ပေါ်ထပ်ပေါင်း (curved_scores = scores + curve) ပြီး result ကို original scores နဲ့ compare ကြည့်ပါ။
Code နမူနာ
import numpy as np
students = ["Aye", "Bo", "Cho", "Dan", "Eaint"]
subjects = ["Math", "Myanmar", "English", "Science"]
scores = np.array([
[78, 85, 90, 72],
[64, 70, 58, 80],
[95, 88, 92, 91],
[50, 60, 55, 48],
[82, 79, 84, 88]
])
student_avg = scores.mean(axis=1)
subject_avg = scores.mean(axis=0)
print("student averages:", student_avg)
print("subject averages:", subject_avg)
top_index = np.argmax(student_avg)
print("top student:", students[top_index], student_avg[top_index])
# curve/bonus per subject: Math+2, Myanmar+5, English+0, Science+3
curve = np.array([2, 5, 0, 3])
curved_scores = scores + curve
print("before curve:\n", scores)
print("after curve:\n", curved_scores)
student_avg နဲ့ subject_avg array နှစ်ခု print ထွက်ပြီး၊ top student name ကို ဖော်ပြပါလိမ့်မယ်၊ curved_scores ကို subject column အလိုက် curve value ထပ်ပေါင်းထားတဲ့ scores အသစ်အနေနဲ့ ရပါလိမ့်မယ်။၅ မိနစ် စမ်းကြည့်
curve array ကို subjects list ထဲက အခု element 4 ခုအစား 3 ခုပဲ ပေးပြီး broadcasting error တက်လာတာကို ကိုယ်တိုင် စမ်းကြည့်ပါ (5 minutes)။
သတိလေးတစ်ချက်
axis=1 ဆိုတာ row အတွင်းက value တွေ (subjects) ကို collapse လုပ်တာမို့ student-wise result ရပါတယ်၊ axis=0 ကတော့ column အတွင်းက value (students) ကို collapse လုပ်ပြီး subject-wise result ရပါတယ် - direction ကို confuse မဖြစ်ဖို့ သတိထားပါ။