Thuta Learning
ရှာဖွေရန်
IntermediateData & Databasesintermediate

Universal Functions

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

Universal Functions သို့မဟုတ် ufuncs ဆိုတာ array ထဲက element တိုင်းကို function တစ်ခုတည်းနဲ့မြန်မြန်တွက်ပေးတဲ့ NumPy functions တွေပါ။ ဥပမာ np.sqrt(), np.round(), np.sin(), np.exp(), np.log() စတာတွေဖြစ်ပါတယ်။

python
import numpy as np

values = np.array([1, 4, 9, 16])

print("Square root:", np.sqrt(values))
print("Power 2:", np.power(values, 2))
print("Rounded average:", np.round(values.mean(), 2))

np.sqrt(values) က element တိုင်းရဲ့ square root ကိုတွက်ပါတယ်။ np.power(values, 2) က element တိုင်းကို power 2 တင်ပါတယ်။ values.mean() က average ကိုတွက်ပြီး np.round(..., 2) က decimal 2 နေရာအထိ round လုပ်ပါတယ်။

You should see
Square root: [1. 2. 3. 4.] Power 2: [ 1 16 81 256] Rounded average: 7.5

Info

ufuncs တွေကိုသုံးရင် loop ရေးတာထက် code ပိုတိုပြီး performance ပိုကောင်းလေ့ရှိပါတယ်။ Data များလာတဲ့အခါ ဒီကွာခြားချက်က သိသာလာပါတယ်။

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

  • np.log() ကို 0 သို့မဟုတ် negative values တွေပေါ်မှာသုံးရင် warning သို့မဟုတ် invalid result ရနိုင်ပါတယ်။ Function ရဲ့ mathematical rule ကိုပါစဉ်းစားပါ။

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

Machine learning preprocessing မှာ normalize, scale, transform လုပ်တဲ့အခါ ufuncs တွေကိုအများကြီးသုံးပါတယ်။ ဥပမာ sensor value တွေကို square root transform လုပ်တာ၊ score တွေကို round လုပ်တာမျိုးပါ။

You'll know it worked when:

Universal Functions | Thuta Learning