ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်
ဒီ project မှာ student တွေရဲ့ subject အလိုက် score တွေကို NumPy 2D array တစ်ခုထဲမှာ သိမ်းပြီး analyze လုပ်မယ့် mini dashboard တစ်ခု တည်ဆောက်ပါမယ်။ Python list of lists နဲ့လည်း ရေးလို့ရပေမယ့် NumPy array က shape, dtype attribute တွေကနေတစ်ဆင့် data structure ကို instant စစ်နိုင်ပြီး၊ နောက်ပိုင်း aggregation, broadcasting တွေမှာ loop မရေးဘဲတွက်နိုင်တဲ့ အားသာချက်ရှိပါတယ်။ Part 1 မှာ core data structure ကိုပဲ တည်ဆောက်ပြီး၊ attributes (shape, ndim, dtype) စစ်ဆေးတာနဲ့ indexing/slicing ကို student list, subject list တွေနဲ့ ချိတ်ပြီး ကျင့်ကြည့်ပါမယ်။ ဒီအဆင့်က နောက် Part 2 rowk analysis, Part 3 final report အတွက် base data ဖြစ်ပါတယ်။
လက်တွေ့ ဆောက်ကြည့်မယ်
Student 5 ယောက်၊ subject 4 ခု (Math, Myanmar, English, Science) အတွက် score array ကို np.array နဲ့ 2D shape (5, 4) အနေနဲ့ manual ထည့်ပါ။ students list နဲ့ subjects list ကို Python list အနေနဲ့ ခွဲသိမ်းပြီး index ကို name နဲ့ map ဖြစ်အောင် ပြင်ဆင်ပါ။ scores.shape, scores.ndim, scores.dtype ကို print ထုတ်ပြီး data structure ကို confirm လုပ်ပါ။ scores[0] နဲ့ student ပထမဦးဆုံးရဲ့ subject score အားလုံး၊ scores[:, 1] နဲ့ Myanmar subject column ကို slicing သုံးပြီး ထုတ်ကြည့်ပါ။
Code နမူနာ
import numpy as np
students = ["Aye", "Bo", "Cho", "Dan", "Eaint"]
subjects = ["Math", "Myanmar", "English", "Science"]
# rows = students, columns = subjects
scores = np.array([
[78, 85, 90, 72],
[64, 70, 58, 80],
[95, 88, 92, 91],
[50, 60, 55, 48],
[82, 79, 84, 88]
])
print("shape:", scores.shape)
print("ndim:", scores.ndim)
print("dtype:", scores.dtype)
# first student's all subject scores
print(students[0], "scores:", scores[0])
# every student's Myanmar (column index 1) score
print("Myanmar scores:", scores[:, 1])
shape (5, 4)၊ ndim 2၊ dtype int64 (or int32) ဆိုပြီး print ထွက်ပြီး၊ student list နဲ့ subject slicing result တွေက correct student/subject scores ကို ပြပါလိမ့်မယ်။၅ မိနစ် စမ်းကြည့်
student နောက်ထပ်တစ်ယောက်နဲ့ subject တစ်ခု (History) ထပ်ထည့်ပြီး scores array ကို (6, 5) shape ဖြစ်အောင် ပြန်ရေးကြည့်ပါ (5 minutes)။
သတိလေးတစ်ချက်
np.array() ကို nested list နဲ့ ဖန်တီးတဲ့အခါ row အားလုံးက length တူဖို့ လိုပါတယ်။ မတူရင် shape က (5,) object array ဖြစ်ပြီး နောက်ပိုင်း aggregation, broadcasting တွေမှာ error တက်ပါလိမ့်မယ်။