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

Linear Algebra

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

NumPy မှာ linear algebra calculation တွေအတွက် np.dot(), @, np.linalg module တွေရှိပါတယ်။ Matrix multiplication, inverse, determinant, solving equations စတာတွေကို Python နဲ့လွယ်လွယ်ကူကူတွက်နိုင်ပါတယ်။ Data science, machine learning, computer graphics, engineering calculation တွေမှာ အခြေခံအုတ်မြစ်လိုအရေးကြီးပါတယ်။

python
import numpy as np

A = np.array([
    [1, 2],
    [3, 4]
])

B = np.array([
    [5, 6],
    [7, 8]
])

matrix_product = A @ B
determinant = np.linalg.det(A)

print("Matrix multiplication:
", matrix_product)
print("Determinant:", determinant)

A @ B က matrix multiplication လုပ်တာပါ။ Element-wise multiplication မဟုတ်ပါ။ np.linalg.det(A) က matrix A ရဲ့ determinant ကိုတွက်ပါတယ်။ Matrix multiplication မှာ A ရဲ့ column count နဲ့ B ရဲ့ row count ကိုက်ရပါတယ်။

You should see
Matrix multiplication: [[19 22] [43 50]] Determinant: -2.0000000000000004

Info

Element-wise multiplication လိုချင်ရင် A * B သုံးပါ။ Matrix multiplication လိုချင်ရင် A @ B သို့မဟုတ် np.dot(A, B) သုံးပါ။ ဒီနှစ်ခုကို မရောပါနဲ့။

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

  • Floating-point calculation ကြောင့် determinant က -2.0 အစား -2.0000000000000004 လို့ပေါ်နိုင်ပါတယ်။ ဒါက computer decimal calculation ရဲ့ သဘာဝပါ။ လိုအပ်ရင် round() သုံးပါ။

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

Equation system တွေကိုလက်နဲ့ဖြေမယ့်အစား np.linalg.solve() နဲ့ matrix form ကနေ ဖြေနိုင်ပါတယ်။

python
import numpy as np

# Solve equations:
# 2x + y = 8
# x + 3y = 13
A = np.array([[2, 1], [1, 3]])
b = np.array([8, 13])

solution = np.linalg.solve(A, b)
print("x and y:", solution)

You'll know it worked when: x and y: [2.2 3.6]

Linear Algebra | Thuta Learning