Thuta Learning
Numpy
BasicData & Databasesintermediate

Creating Arrays

NumPy မှာ data တွေကိုတွက်ချင်ရင် အရင်ဆုံး array တည်ဆောက်ရပါတယ်။ Array တည်ဆောက်နည်းများစွာရှိပြီး အသုံးအများဆုံးတွေက np.array(), np.zeros(), np.ones(), np.arange(), np.linspace() တို့ပါ။ ဘယ် function ကိုသုံးမလဲဆိုတာက data ကို ဘယ်လိုစတင်ချင်လဲပေါ်မူတည်ပါတယ်။

python
import numpy as np

# Create an array from an existing list
prices = np.array([1200, 1500, 1800, 2100])
print("Prices:", prices)

# Create a 2 rows x 3 columns array filled with zeros
empty_table = np.zeros((2, 3))
print("
Empty table:
", empty_table)

# Create values from 0 to 8, step by 2
steps = np.arange(0, 10, 2)
print("
Steps:", steps)

# Create 5 evenly spaced values between 0 and 1
percent = np.linspace(0, 1, 5)
print("
Percent points:", percent)

np.array() က ရှိပြီးသား list ကို array ပြောင်းပေးပါတယ်။ np.zeros((2, 3)) က row 2 ခု၊ column 3 ခုပါတဲ့ zero table တစ်ခုလုပ်ပေးပါတယ်။ np.arange(0, 10, 2) မှာ stop value ဖြစ်တဲ့ 10 မပါဝင်တာကို သတိထားပါ။ np.linspace(0, 1, 5) က 0 နဲ့ 1 ကြားကို ညီမျှတဲ့ point 5 ခုခွဲပေးတာပါ။

You should see
Prices: [1200 1500 1800 2100] Empty table: [[0. 0. 0.] [0. 0. 0.]] Steps: [0 2 4 6 8] Percent points: [0. 0.25 0.5 0.75 1. ]

Info

shape ကိုရေးတဲ့အခါ (rows, columns) ပုံစံ tuple နဲ့ရေးရပါတယ်။ np.zeros(2, 3) လို့ရေးရင် မျှော်လင့်သလိုမဟုတ်နိုင်ပါ။

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

  • np.arange() မှာ stop number မပါဝင်ပါ။ 0 ကနေ 10 အထိဆိုပြီး 10 ပါစေချင်ရင် np.arange(0, 11) လိုရေးရပါတယ်။

လေ့ကျင့်ခန်း

Data analysis project တွေမှာ CSV/database ထဲက number တွေကို array ပြောင်းပြီး calculation လုပ်တာများပါတယ်။ အလွတ် result array ကို ကြိုတင်ထားတာက နောက်ပိုင်း calculation results သိမ်းဖို့အသုံးဝင်ပါတယ်။

python
import numpy as np

# Monthly sales for 6 months
sales = np.array([120, 135, 150, 160, 155, 180])

# Prepare an empty result array for future calculations
bonus_points = np.zeros(sales.shape)

print("Sales:", sales)
print("Bonus placeholder:", bonus_points)

You'll know it worked when: Sales: [120 135 150 160 155 180] Bonus placeholder: [0. 0. 0. 0. 0. 0.]

Creating Arrays | Thuta Learning