Monday, November 16, 2026

Bird Migration & Flight Pattern Analysis (Part-6)

၁။ နောက်ခံသဘောတရားများ (Dimensionality Reduction & Probabilistic Clustering)

bird_tracking.csv ဒေတာထဲတွင် ငှက်တစ်ကောင်၏ ပျံသန်းမှုကို ဖော်ပြနေသော Feature ပေါင်းများစွာ (speed_2d, altitude, direction, altitude_diff စသည်) ပါဝင်ပါသည်။ ထိုအချက်အလက်များကို ပိုမိုနက်ရှိုင်းစွာ ခွဲခြမ်းစိတ်ဖြာရန် PCA နှင့် GMM တို့ကို ပေါင်းစပ်အသုံးပြုခြင်းသည် Machine Learning နယ်ပယ်တွင် အလွန်အသုံးဝင်သော နည်းစနစ်တစ်ခု ဖြစ်ပါသည်။

(က) Principal Component Analysis (PCA - ဒိုင်မန်းရှင်း လျှော့ချခြင်း)

လက်တွေ့ဒေတာများတွင် Feature ၅ ခု သို့မဟုတ် ၁၀ ခု ရှိနေသောအခါ လူသားများအနေဖြင့် 2D သို့မဟုတ် 3D ဂရပ်များပေါ်တွင် တိုက်ရိုက်မြင်သာအောင် ဆွဲကြည့်ရန် မဖြစ်နိုင်ပါ။ ထို့အပြင် Feature အချင်းချင်း ဆက်နွှယ်မှု (Correlation) များ ရှိနေတတ်ပါသည် (ဥပမာ- ငှက်သည် အမြင့်ပျံလေ အမြန်နှုန်း ပိုမြင့်လေ ဖြစ်ခြင်း)။

  • PCA ၏ အလုပ်လုပ်ပုံ: မူလ Feature များစွာ (ဥပမာ- 5D) မှ အရေးကြီးဆုံး အချက်အလက် (Variance) အများဆုံး ပါဝင်သည့် ဝင်ရိုးအသစ်များ (Principal Components: PC1, PC2) အဖြစ် သင်္ချာနည်းအရ ချုံ့ယူပေးခြင်း ဖြစ်ပါသည်။

  • Orthogonality (ထောင့်မှန်ကျမှု): PC1 သည် ဒေတာတစ်ခုလုံး၏ ကွဲပြားမှု (Variance) အများဆုံးကို ဖမ်းယူပေးပြီး၊ PC2 သည် PC1 နှင့် ထောင့်မှန်ကျ ($90^\circ$) တည်ရှိကာ ဒုတိယအများဆုံး ကျန်ရှိနေသော ကွဲပြားမှုကို ဖမ်းယူပေးပါသည်။

  • Loadings & Explained Variance Ratio: PC1 နှင့် PC2 တို့က မူလဒေတာ၏ အချက်အလက် မည်မျှရာခိုင်နှုန်းကို ထိန်းသိမ်းထားနိုင်သနည်း (explained_variance_ratio_) နှင့် မူလ Feature တစ်ခုချင်းစီက PC1, PC2 အပေါ် မည်မျှသက်ရောက်မှု ရှိသနည်း (components_) ကို တိုင်းတာကြည့်ရှုနိုင်ပါသည်။

(ခ) Gaussian Mixture Models (GMM - ဖြစ်နိုင်ခြေအခြေပြု အုပ်စုခွဲခြင်း)

ယခင်လေ့လာခဲ့သော K-Means နှင့် ယခု GMM တို့၏ အဓိက ကွာခြားချက်မှာ ပြတ်သားစွာ ခွဲခြားခြင်း (Hard Clustering) နှင့် ဖြစ်နိုင်ခြေ ရာခိုင်နှုန်းဖြင့် ခွဲခြားခြင်း (Soft / Probabilistic Clustering) ဖြစ်ပါသည်။

သဘာဝလောကတွင် ငှက်တစ်ကောင်သည် "အစာရှာနေရာမှ ခရီးဝေးရွှေ့ပြောင်းခြင်း" သို့ ရုတ်တရက် ခလုတ်နှိပ်သလို ပြောင်းလဲသွားခြင်း မဟုတ်ဘဲ ကြားကာလ အသွင်ကူးပြောင်းမှုများ (Transitional States - ဥပမာ အမြင့်တက်ရင်း လေစီးကြောင်းရှာနေချိန်) ရှိတတ်ပါသည်။ ထိုအခြေအနေများကို GMM က အတိအကျ ဖမ်းယူနိုင်ပါသည်။

နှိုင်းယှဉ်ချက်K-Means ClusteringGaussian Mixture Model (GMM)
အုပ်စုခွဲပုံစံHard Clustering: အမှတ်တစ်ခုသည် အုပ်စု (၁) ခုတည်းသာ ဖြစ်ရမည် (0 သို့မဟုတ် 1)။Soft Clustering: အုပ်စုတိုင်းအတွက် ဖြစ်နိုင်ခြေ ရာခိုင်နှုန်း (predict_proba) ထုတ်ပေးသည် (ဥပမာ- နားနေ 5%, အစာရှာ 65%, ခရီးသွား 30%)။
အုပ်စု ပုံသဏ္ဌာန်အချင်းဝက်တူ စက်ဝိုင်းပုံသဏ္ဌာန် (Spherical) များကိုသာ ဖမ်းယူနိုင်သည်။covariance_type='full' သုံးပါက စောင်းနေသော ဘဲဥပုံသဏ္ဌာန် (Elliptical) အုပ်စုများကိုပါ တိကျစွာ ဖမ်းယူနိုင်သည်။
တွက်ချက်မှု စနစ်ဗဟိုမှတ်နှင့် အကွာအဝေး (Euclidean Distance) ကိုသာ အခြေခံသည်။Expectation-Maximization (EM) Algorithm ကိုသုံး၍ Gaussian Distribution တစ်ခုချင်းစီ၏ ပျမ်းမျှ ($\mu$) နှင့် ပြန့်ကျဲမှု ($\Sigma$) ကို တွက်ချက်သည်။

၂။ Python Code Implementation

အောက်ပါပရိုဂရမ်တွင် Eric ၏ ပျံသန်းမှုဆိုင်ရာ Feature ၅ ခု (speed_2d, altitude, altitude_diff, နှင့် စက်ဝိုင်းပုံ ဦးတည်ရာကို ပြောင်းလဲထားသော dir_sin, dir_cos) ကို အသုံးပြု၍ GMM ဖြင့် အပြုအမူ ဖြစ်နိုင်ခြေများ တွက်ချက်ပြီး PCA ဖြင့် 2D မြင်ကွင်းပေါ်တွင် ပုံဖော်ပြသထားပါသည်။

Python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.mixture import GaussianMixture

# ၁။ ဒေတာဖတ်ရှုခြင်းနှင့် Eric ၏ ဒေတာကို အချိန်အစဉ်လိုက် စီစဉ်ခြင်း
birddata = pd.read_csv("bird_tracking.csv")
eric_data = birddata[birddata['bird_name'] == 'Eric'].copy()
eric_data['date_time'] = pd.to_datetime(eric_data['date_time'], format='mixed')
eric_data = eric_data.sort_values('date_time').reset_index(drop=True)

# ၂။ Feature Engineering (ပျံသန်းမှု လက္ခဏာ ၅ ခု တည်ဆောက်ခြင်း)
# အမြင့်အတက်အကျနှုန်း (Vertical Speed / Climb Rate)
eric_data['altitude_diff'] = eric_data['altitude'].diff().fillna(0)

# Direction (-180 မှ +180 ဒီဂရီ) ကို စက်ဝိုင်းပုံသဘောတရား မပျက်စေရန် sin နှင့် cos ပြောင်းလဲခြင်း
dir_rad = np.radians(eric_data['direction'])
eric_data['dir_sin'] = np.sin(dir_rad)
eric_data['dir_cos'] = np.cos(dir_rad)

feature_cols = ['speed_2d', 'altitude', 'altitude_diff', 'dir_sin', 'dir_cos']
eric_data = eric_data.dropna(subset=feature_cols).reset_index(drop=True)

X = eric_data[feature_cols]

# ၃။ Feature Scaling ပြုလုပ်ခြင်း (PCA နှင့် GMM နှစ်ခုလုံးအတွက် မဖြစ်မနေ လိုအပ်ပါသည်)
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# ၄။ Dimensionality Reduction: 5D ဒေတာကို PCA ဖြင့် 2D သို့ လျှော့ချခြင်း
pca = PCA(n_components=2, random_state=42)
X_pca = pca.fit_transform(X_scaled)

eric_data['PC1'] = X_pca[:, 0]
eric_data['PC2'] = X_pca[:, 1]

var_ratio = pca.explained_variance_ratio_
print("=== ၁။ PCA (Dimensionality Reduction) ရလဒ်များ ===")
print(f"PC1 မှ ထိန်းသိမ်းထားနိုင်သော အချက်အလက် (Variance) : {var_ratio[0]*100:.2f}%")
print(f"PC2 မှ ထိန်းသိမ်းထားနိုင်သော အချက်အလက် (Variance) : {var_ratio[1]*100:.2f}%")
print(f"2D ဂရပ်တစ်ခုတည်းဖြင့် စုစုပေါင်း ဖော်ပြနိုင်စွမ်း        : {sum(var_ratio)*100:.2f}%\n")

# မူလ Feature များက PC1 နှင့် PC2 အပေါ် သက်ရောက်မှု (PCA Loadings) ဇယား
loadings = pd.DataFrame(
    pca.components_.T,
    columns=['PC1_Weight', 'PC2_Weight'],
    index=feature_cols
)
print("--- PCA Feature Loadings (ဝင်ရိုးတစ်ခုချင်းစီ၏ ကိုယ်စားပြုမှု) ---")
print(loadings.round(3))
print()

# ၅။ Probabilistic Clustering: Gaussian Mixture Model (GMM) တည်ဆောက်ခြင်း
# 5D ဒေတာအပြည့်အစုံ (X_scaled) ပေါ်တွင် full covariance ဖြင့် Fit လုပ်ပါမည်
gmm = GaussianMixture(n_components=3, covariance_type='full', random_state=42)
raw_clusters = gmm.fit_predict(X_scaled)
raw_probs = gmm.predict_proba(X_scaled)

# အမြန်နှုန်း အနည်းအများအလိုက် Cluster နံပါတ်များကို 0, 1, 2 အစဉ်လိုက် ပြန်စီခြင်း
centers_original = scaler.inverse_transform(gmm.means_)
sorted_idx = np.argsort(centers_original[:, 0]) # speed_2d (index 0) ဖြင့် စီခြင်း
label_map = {old_idx: new_idx for new_idx, old_idx in enumerate(sorted_idx)}

# Cluster အမည်နှင့် ဖြစ်နိုင်ခြေ ကော်လံများကို စနစ်တကျ ပြန်စီ၍ ထည့်သွင်းခြင်း
eric_data['gmm_cluster'] = [label_map[c] for c in raw_clusters]
sorted_probs = raw_probs[:, sorted_idx]

eric_data['Prob_Resting'] = sorted_probs[:, 0]
eric_data['Prob_Foraging'] = sorted_probs[:, 1]
eric_data['Prob_Migration'] = sorted_probs[:, 2]

# ခန့်မှန်းချက် တစ်ခုချင်းစီ၏ အမြင့်ဆုံး ယုံကြည်မှုနှုန်း (Confidence) တွက်ချက်ခြင်း
eric_data['max_confidence'] = sorted_probs.max(axis=1)

# ယုံကြည်မှု 75% အောက်ရောက်နေသော အသွင်ကူးပြောင်းဆဲ အခြေအနေများ (Transitional States)
uncertain_mask = eric_data['max_confidence'] < 0.75
print("=== ၂။ GMM (Probabilistic Clustering) ရလဒ်များ ===")
print(f"စုစုပေါင်း မှတ်တမ်းအရေအတွက်                  : {len(eric_data)} ခု")
print(f"အသွင်ကူးပြောင်းဆဲ/မရေရာသော အမှတ်များ (<75%) : {np.sum(uncertain_mask)} ခု ({np.mean(uncertain_mask)*100:.2f}%)\n")

print("--- အသွင်ကူးပြောင်းဆဲ အခြေအနေ နမူနာ ၅ ခု (Soft Probabilities) ---")
sample_cols = ['speed_2d', 'altitude', 'Prob_Resting', 'Prob_Foraging', 'Prob_Migration', 'gmm_cluster']
print(eric_data[uncertain_mask][sample_cols].head(5).round(3))

# ၆။ ရလဒ်များကို ဂရပ် (၂) ခုဖြင့် ပုံဖော်ခြင်း
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 7))

colors = ['#1f77b4', '#2ca02c', '#d62728']
cluster_names = [
    'Cluster 0: Resting (အနားယူချိန်)',
    'Cluster 1: Foraging (အစာရှာချိန်)',
    'Cluster 2: Migration (ခရီးဝေးရွှေ့ပြောင်းချိန်)'
]

# --- ဘယ်ဘက်ဂရပ် (ax1): 2D PCA Space ပေါ်မှ GMM Clusters နှင့် မရေရာသော အမှတ်များ ---
for i in range(3):
    subset = eric_data[eric_data['gmm_cluster'] == i]
    ax1.scatter(
        subset['PC1'], subset['PC2'],
        c=colors[i], label=cluster_names[i], s=14, alpha=0.45
    )

# အသွင်ကူးပြောင်းဆဲ အမှတ်များ (<75% confidence) ကို အနက်ရောင်အဝိုင်းများဖြင့် ဝိုင်းပြခြင်း
uncertain_points = eric_data[uncertain_mask]
ax1.scatter(
    uncertain_points['PC1'], uncertain_points['PC2'],
    facecolors='none', edgecolors='black', s=45, linewidths=0.8, alpha=0.7,
    label='Transitional / Uncertain (<75% Prob)'
)

ax1.set_title("GMM Probabilistic Clusters Projected in 2D PCA Space", fontsize=12, fontweight='bold')
ax1.set_xlabel(f"Principal Component 1 ({var_ratio[0]*100:.1f}% Variance)")
ax1.set_ylabel(f"Principal Component 2 ({var_ratio[1]*100:.1f}% Variance)")
ax1.legend(loc='upper right', fontsize=8.5)
ax1.grid(True, linestyle='--', alpha=0.5)

# --- ညာဘက်ဂရပ် (ax2): PCA Feature Loadings (PC1 နှင့် PC2 ၏ အဓိပ္ပာယ်) ---
x_pos = np.arange(len(feature_cols))
width = 0.35

rects1 = ax2.bar(x_pos - width/2, loadings['PC1_Weight'], width, label='PC1 Weight', color='#34495e')
rects2 = ax2.bar(x_pos + width/2, loadings['PC2_Weight'], width, label='PC2 Weight', color='#e67e22')

ax2.axhline(0, color='black', linewidth=0.8)
ax2.set_title("PCA Feature Loadings (မူလ Feature များ၏ သက်ရောက်မှု)", fontsize=12, fontweight='bold')
ax2.set_xticks(x_pos)
ax2.set_xticklabels(feature_cols, fontsize=10, fontweight='bold')
ax2.set_ylabel("Component Weight (Loading)")
ax2.legend()
ax2.grid(True, axis='y', linestyle='--', alpha=0.5)

plt.tight_layout()
plt.show()



၃။ ရလဒ်များနှင့် ဂရပ်များကို အဓိပ္ပာယ်ဖွင့်ဆိုခြင်း

  1. PCA Feature Loadings ဂရပ် (ညာဘက်ပုံ) မှ တွေ့ရှိချက်

    • မူလ 5-Dimensional ဒေတာကို 2D ဝင်ရိုးနှစ်ခုတည်းသို့ ချုံ့လိုက်သော်လည်း အရေးကြီးသော အချက်အလက် စုစုပေါင်း၏ ၅၀% ကျော် ကို ထိန်းသိမ်းထားနိုင်ပါသည်။

    • ညာဘက် Bar Chart ကို ကြည့်ခြင်းဖြင့် PC1 သည် ငှက်၏ ပျံသန်းမှု ဦးတည်ရာ (dir_sin, dir_cos) နှင့် ဆက်စပ်နေပြီး၊ PC2 သည် ပျံသန်းမှု အရှိန်နှင့် အမြင့် (speed_2d, altitude) တို့ကို အဓိက ကိုယ်စားပြုနေကြောင်း တွေ့မြင်နိုင်ပါသည်။

  2. GMM Soft Probabilities နှင့် အသွင်ကူးပြောင်းဆဲ အခြေအနေများ (ဘယ်ဘက်ပုံ)

    • Console တွင် ထွက်ပေါ်လာသော uncertain_mask ဇယားကို ကြည့်ပါက ဥပမာ- အမြန်နှုန်း 4.8 m/s ဝန်းကျင် ရှိနေချိန်တွင် မော်ဒယ်က "အစာရှာနေခြင်း ၅၅%၊ ခရီးဝေးရွှေ့ပြောင်းခြင်း ၄၄%၊ နားနေခြင်း ၁%" စသဖြင့် ဖြစ်နိုင်ခြေများကို ခွဲဝေတွက်ချက်ပေးပါသည်။

    • ဘယ်ဘက်ဂရပ်တွင် အနက်ရောင်အဝိုင်းလေးများဖြင့် ဝိုင်းပြထားသော နေရာများသည် Cluster တစ်ခုနှင့်တစ်ခု စပ်ကြား (Decision Boundary) တွင် ရှိနေသည့် အမှတ်များဖြစ်ပြီး၊ ငှက်သည် အပြုအမူတစ်ခုမှ နောက်တစ်ခုသို့ ကူးပြောင်းနေသည့် အခိုက်အတန့်များကို တိကျစွာ ထောက်ပြပေးနေခြင်း ဖြစ်ပါသည်။

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.