ပြုပြင်နည်း- ကိန်းပြည့်စကေးအခင်းအကျင်းများကိုသာ scalar အညွှန်းအဖြစ် ပြောင်းလဲနိုင်သည်။


Python ကိုအသုံးပြုရာတွင် သင်ကြုံတွေ့ရနိုင်သည့် အမှားတစ်ခုမှာ-

 TypeError : only integer scalar arrays can be converted to a scalar index

ဤအမှားသည် များသောအားဖြင့် အကြောင်းရင်း နှစ်ခုထဲမှ တစ်ခုကြောင့် ဖြစ်တတ်သည်-

1. စာရင်းတစ်ခုတွင် array indexing လုပ်ရန် သင်ကြိုးစားခဲ့သည်။

2. သင်သည် မမှန်ကန်သော အထားအသိုကို အသုံးပြု၍ matrices နှစ်ခုကို ပေါင်းစပ်ရန် ကြိုးစားခဲ့သည်။

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

ဥပမာ 1- သင်သည် စာရင်းတစ်ခုတွင် array indexing ကိုလုပ်ဆောင်ရန်ကြိုးစားခဲ့သည်။

ဒဏ္ဍာရီနှင့် အညွှန်းများဖြင့် matplotlib တွင် မျဉ်းဇယားတစ်ခုဖန်တီးရန် အောက်ပါကုဒ်ကို အသုံးပြုရန် ကြိုးစားနေသည်ဆိုပါစို့။

 import numpy as np

#create a list of values
data = [3, 5, 5, 7, 8, 10, 12, 14]

#choose 3 random values from list
random_values = np. random . choice (range(len(data)), size= 2 )

#attempt to use indexing to access elements in list
random_vals = data[random_values. astype (int)]

#view results
random_vals

TypeError : only integer scalar arrays can be converted to a scalar index

စာရင်းတစ်ခုတွင် array indexing ကိုအသုံးပြုရန် ကြိုးပမ်းခဲ့၍ ကျွန်ုပ်တို့သည် အမှားအယွင်းတစ်ခုကို လက်ခံရရှိနေပါသည်။

ဤအမှားကိုရှောင်ရှားရန်၊ ကျွန်ုပ်တို့သည် အောက်ပါအတိုင်း np.array() ကို အသုံးပြု၍ စာရင်းကို NumPy အခင်းအကျင်းသို့ ဦးစွာပြောင်းရပါမည်-

 import numpy as np

#create a list of values
data = [3, 5, 5, 7, 8, 10, 12, 14]

#choose 3 random values from list
random_values = np. random . choice (range(len(data)), size= 2 )

#attempt to use indexing to access elements in list
random_vals = np. array (data)[random_values. astype (int)]

#view results
random_vals

array([5, 7])

ဤတစ်ကြိမ်တွင် စာရင်းကို NumPy ခင်းကျင်းတစ်ခုသို့ ပထမဆုံးပြောင်းလိုက်သောကြောင့် အမှားအယွင်းမရှိဘဲ စာရင်းထဲမှ တန်ဖိုးနှစ်ခုကို ကျပန်းရွေးချယ်နိုင်သည်။

ဥပမာ 2- သင်သည် မမှန်ကန်သော အထားအသိုကို အသုံးပြု၍ matrices နှစ်ခုကို ပေါင်းစပ်ရန် ကြိုးစားခဲ့သည်။

NumPy matrices နှစ်ခုကို ပေါင်းစပ်ရန် အောက်ပါကုဒ်ကို အသုံးပြုရန် ကြိုးစားသည်ဆိုပါစို့။

 import numpy as np

#create twoNumPy matrices
mat1 = np. matrix ([[3, 5], [5, 7]])
mat2 = np. matrix ([[2, 4], [1, 8]])

#attempt to concatenate both matrices
n.p. concatenate (mat1, mat2)

TypeError : only integer scalar arrays can be converted to a scalar index

concatenate() လုပ်ဆောင်ချက်အတွက် tuple အဖြစ် matrices များကို ပေးဆောင်ရန် ပျက်ကွက်ခဲ့သောကြောင့် ကျွန်ုပ်တို့သည် အမှားအယွင်းတစ်ခုကို လက်ခံရရှိနေပါသည်။

ဤအမှားကိုရှောင်ရှားရန်၊ အောက်ပါအတိုင်း concatenate() လုပ်ဆောင်ချက်အတွက် tuple form တွင် matrices များပေးဆောင်ရန် ကွင်းနှစ်ထပ်ကို အသုံးပြုရပါမည်။

 import numpy as np

#create twoNumPy matrices
mat1 = np. matrix ([[3, 5], [5, 7]])
mat2 = np. matrix ([[2, 4], [1, 8]])

#attempt to concatenate both matrices
n.p. concatenate ((mat1, mat2))

matrix([[3, 5],
        [5, 7],
        [2, 4],
        [1, 8]])

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

ထပ်လောင်းအရင်းအမြစ်များ

အောက်ဖော်ပြပါ သင်ခန်းစာများသည် Python ရှိ အခြားသော ဘုံအမှားများကို မည်သို့ပြင်ဆင်ရမည်ကို ရှင်းပြသည်-

Pandas တွင် KeyError ကိုဘယ်လိုပြင်မလဲ။
ပြင်ဆင်နည်း- ValueError- float NaN ကို int သို့ ပြောင်းလဲ၍မရပါ။
ပြုပြင်နည်း- တန်ဖိုးအမှား- Operands များကို ပုံသဏ္ဍာန်များဖြင့် ထုတ်လွှင့်၍မရပါ။

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

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