Python တွင် euclidean အကွာအဝေးကို တွက်ချက်နည်း (နမူနာများဖြင့်)


Vector A နှင့် B နှစ်ခုကြားရှိ Euclidean အကွာအဝေးကို အောက်ပါအတိုင်း တွက်ချက်သည်။

ယူကလစ်အကွာအဝေး = √ Σ(A i -B i )

Python ရှိ vector နှစ်ခုကြားရှိ Euclidean အကွာအဝေးကို တွက်ချက်ရန် numpy.linalg.norm လုပ်ဆောင်ချက်ကို ကျွန်ုပ်တို့ အသုံးပြုနိုင်ပါသည်။

 #import functions
import numpy as np
from numpy. linalg import norm

#define two vectors
a = np.array([2, 6, 7, 7, 5, 13, 14, 17, 11, 8])
b = np.array([3, 5, 5, 3, 7, 12, 13, 19, 22, 7])

#calculate Euclidean distance between the two vectors 
norm(ab)

12.409673645990857

vector နှစ်ခုကြားရှိ Euclidean အကွာအဝေးသည် 12.40967 ဖြစ်သွားသည်။

Vector နှစ်ခုသည် အရှည်မတူညီပါက၊ ဤလုပ်ဆောင်ချက်သည် သတိပေးချက်မက်ဆေ့ချ်ကို ထုတ်ပေးမည်ဖြစ်ကြောင်း သတိပြုပါ။

 #import functions
import numpy as np
from numpy. linalg import norm

#define two vectors
a = np.array([2, 6, 7, 7, 5, 13, 14])
b = np.array([3, 5, 5, 3, 7, 12, 13, 19, 22, 7])

#calculate Euclidean distance between the two vectors 
norm(ab)

ValueError : operands could not be broadcast together with shapes (7,) (10,) 

ပန်ဒါ DataFrame ကော်လံနှစ်ခုကြားရှိ Euclidean အကွာအဝေးကို တွက်ချက်ရန် ဤလုပ်ဆောင်ချက်ကိုလည်း အသုံးပြုနိုင်ကြောင်း သတိပြုပါ။

 #import functions
import pandas as pd 
import numpy as np
from numpy. linalg import norm

#define DataFrame with three columns
df = pd.DataFrame({'points': [25, 12, 15, 14, 19, 23, 25, 29],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})

#calculate Euclidean distance between 'points' and 'assists' 
norm(df[' points '] - df[' assists '])

40.496913462633174

ကော်လံနှစ်ခုကြားရှိ Euclidean အကွာအဝေးသည် 40.49691 ဖြစ်လာသည်။

မှတ်ချက်များ

1. Python တွင် Euclidean အကွာအဝေးကို တွက်ချက်ရန် နည်းလမ်းများစွာ ရှိသည်၊ သို့သော် ဤ Stack Overflow thread က ရှင်းပြထားသည့် အတိုင်း၊ ဤနေရာတွင် ရှင်းပြထားသော နည်းလမ်းသည် အမြန်ဆုံး ဖြစ်လာပါသည်။

2. numpy.linalg.norm လုပ်ဆောင်ချက်၏ စာရွက်စာတမ်းအပြည့်အစုံကို ဤနေရာတွင် ရှာဖွေနိုင်ပါသည်။

3. Euclidean အကွာအဝေးအကြောင်း ပိုမိုလေ့လာရန် ဤ Wikipedia စာမျက်နှာ ကို ကိုးကားနိုင်ပါသည်

မှတ်ချက်တစ်ခုထည့်ပါ။

သင့် email လိပ်စာကို ဖော်ပြမည် မဟုတ်ပါ။ လိုအပ်သော ကွက်လပ်များကို * ဖြင့်မှတ်သားထားသည်