Thuta Learning
Numpy
ProjectsData & Databasesအလယ်အလတ်

Student Score Project - Part 1: Setup

Numpyသင်ခန်းစာ 17

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Student Score Project - Part 1: Setup ကို လက်တွေ့ project ထဲမှာ အသုံးချတတ်မယ်
  • ကိုယ်တိုင် code ရေးပြီး run ကြည့်တတ်မယ်
  • Project တစ်ခုလုံးကို အဆင့်လိုက် ဆောက်တတ်မယ်

ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်

ဒီ 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 နမူနာ

python
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])
You should see
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 တက်ပါလိမ့်မယ်။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • row/column count မညီအောင် nested list ရေးမိလို့ np.array() က object dtype ဖြစ်သွားတာ (inconsistent shape)
  • scores[:, 1] နဲ့ scores[1] ကို မှားနားလည်ပြီး row/column ဆင်တူထင်တာ

အခု ကိုယ်တိုင် စမ်းကြည့်

student နောက်ထပ်တစ်ယောက်နဲ့ subject တစ်ခု (History) ထပ်ထည့်ပြီး scores array ကို (6, 5) shape ဖြစ်အောင် ပြန်ရေးကြည့်ပါ (5 minutes)။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: shape (5, 4)၊ ndim 2၊ dtype int64 (or int32) ဆိုပြီး print ထွက်ပြီး၊ student list နဲ့ subject slicing result တွေက correct student/subject scores ကို ပြပါလိမ့်မယ်။

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Student Score Project - Part 1: Setup | Thuta Learning