Python တွင် cosine တူညီမှုကို တွက်နည်း
Cosine တူညီမှု သည် အတွင်းထုတ်ကုန်တစ်ခု၏ ကွက်လပ်နှစ်ခုကြားရှိ တူညီမှုအား တိုင်းတာမှုတစ်ခုဖြစ်သည်။
Vector နှစ်ခုဖြစ်သော A နှင့် B အတွက် ကိုsine ဆင်တူယိုးမှားကို အောက်ပါအတိုင်း တွက်ချက်သည် ။
ကိုsine ဆင်တူယိုးမှား = ΣA i B i / (√ΣA i 2 √ΣB i 2 )
ဤသင်ခန်းစာသည် NumPy စာကြည့်တိုက်မှ လုပ်ဆောင်ချက်များကို အသုံးပြု၍ Python ရှိ vector များအကြား cosine ဆင်တူမှုများကို တွက်ချက်နည်းကို ရှင်းပြထားသည်။
Python ရှိ vector နှစ်ခုကြားတွင် cosine ဆင်တူသည်။
အောက်ပါကုဒ်သည် Python ရှိ array နှစ်ခုကြား cosine တူညီမှုကို တွက်ချက်နည်းကို ပြသည်-
from numpy import dot from numpy. linalg import norm #define arrays a = [23, 34, 44, 45, 42, 27, 33, 34] b = [17, 18, 22, 26, 26, 29, 31, 30] #calculate Cosine Similarity cos_sim = dot (a, b)/( norm (a)* norm (b)) cos_sim 0.965195008357566
ဇယားနှစ်ခုကြားတွင် cosine တူညီမှုသည် 0.965195 ဖြစ်လာသည်။
ဤနည်းလမ်းသည် မည်သည့်အရှည်၏ array နှစ်ခုတွင်မဆို အလုပ်လုပ်မည်ကို သတိပြုပါ။
import numpy as np from numpy import dot from numpy. linalg import norm #define arrays a = np.random.randint(10, size= 100 ) b = np.random.randint(10, size= 100 ) #calculate Cosine Similarity cos_sim = dot (a, b)/( norm (a)* norm (b)) cos_sim 0.7340201613960431
သို့သော်၊ array နှစ်ခုသည် အရှည်တူညီပါက ၎င်းသည်သာ အလုပ်လုပ်သည်-
import numpy as np from numpy import dot from numpy. linalg import norm #define arrays a = np.random.randint(10, size= 90 ) #length=90 b = np.random.randint(10, size= 100 ) #length=100 #calculate Cosine Similarity cos_sim = dot (a, b)/( norm (a)* norm (b)) cos_sim ValueError : shapes (90,) and (100,) not aligned: 90 (dim 0) != 100 (dim 0)
မှတ်ချက်များ
1. Python ကို အသုံးပြု၍ cosine တူညီမှုကို တွက်ချက်ရန် နည်းလမ်းများစွာ ရှိသည်၊ သို့သော် ဤ Stack Overflow thread တွင် ရှင်းပြထားသည့်အတိုင်း၊ ဤဆောင်းပါးတွင် ရှင်းပြထားသော နည်းလမ်းသည် အမြန်ဆုံး ဖြစ်လာပါသည်။
2. cosine တူညီမှုအကြောင်း ပိုမိုလေ့လာရန်ဤ Wikipedia စာမျက်နှာ ကို ကိုးကားပါ။