Numpy array တစ်ခုတွင် elements များကို မည်ကဲ့သို့ရွှေ့ရမည် (ဥပမာများနှင့်အတူ)
NumPy array ၏ဒြပ်စင်များကို offset လုပ်ရန် အောက်ပါနည်းလမ်းများထဲမှ တစ်ခုကို သင်သုံးနိုင်သည်။
နည်းလမ်း 1- Shift element (မူရင်းဒြပ်စင်အားလုံးကို သိမ်းဆည်းထားပါ)
#shift each element two positions to the right data_new = np. roll (data, 2)
နည်းလမ်း 2- Shift ဒြပ်စင်များ (ဒြပ်စင်များကို အစားထိုးရန် ခွင့်ပြုသည်)
#define shifting function
def shift_elements (arr, num, fill_value):
result = np. empty_like (arr)
if num > 0:
result[:num] = fill_value
result[num:] = arr[:-num]
elif num < 0:
result[num:] = fill_value
result[:num] = arr[-num:]
else :
result[:] = arr
return result
#shift each element two positions to the right (replace shifted elements with zero)
data_new = shift_elements(data, 2, 0)
အောက်ဖော်ပြပါ ဥပမာများသည် နည်းလမ်းတစ်ခုစီကို လက်တွေ့အသုံးချနည်းကို ပြသထားသည်။
နည်းလမ်း 1- Shift element (မူရင်းဒြပ်စင်အားလုံးကို သိမ်းဆည်းထားပါ)
NumPy array တစ်ခုစီ၏ ဒြပ်စင်တစ်ခုစီကို ညာဘက်သို့ ရာထူးနှစ်ခုပြောင်းရန် np.roll() လုပ်ဆောင်ချက်ကို အောက်ပါကုဒ်တွင် ဖော်ပြသည်-
import numpy as np #create NumPy array data = np. array ([1, 2, 3, 4, 5, 6]) #shift each element two positions to the right data_new = np. roll (data, 2) #view new NumPy array data_new array([5, 6, 1, 2, 3, 4])
ဒြပ်စင်တစ်ခုစီကို ညာဘက်သို့ ရာထူးနှစ်ခုပြောင်းထားပြီး array ၏အဆုံးရှိ element များကို ရှေ့သို့သာရွှေ့ထားကြောင်း သတိပြုပါ။
np.roll() လုပ်ဆောင်ချက်တွင် အနှုတ်နံပါတ်တစ်ခုကိုလည်း အသုံးပြု၍ အစိတ်အပိုင်းများကို ဘယ်ဘက်သို့ ပြောင်းနိုင်သည်-
import numpy as np #create NumPy array data = np. array ([1, 2, 3, 4, 5, 6]) #shift each element three positions to the left data_new = np. roll (data, -3) #view new NumPy array data_new array([4, 5, 6, 1, 2, 3])
နည်းလမ်း 2- Shift ဒြပ်စင်များ (ဒြပ်စင်များကို အစားထိုးရန် ခွင့်ပြုသည်)
NumPy array ၏ဒြပ်စင်များကိုပြောင်းရန်နှင့်ပြောင်းထားသောဒြပ်စင်များကိုအချို့တန်ဖိုးတစ်ခုဖြင့်အစားထိုးရန်ကျွန်ုပ်တို့သည်စိတ်ကြိုက်လုပ်ဆောင်မှုကိုလည်းသတ်မှတ်နိုင်သည်။
ဥပမာအားဖြင့်၊ ကျွန်ုပ်တို့သည် အောက်ဖော်ပြပါ လုပ်ဆောင်ချက်ကို ဒြပ်စင်များပြောင်းရန်နှင့် ရွှေ့ထားသောဒြပ်စင်အားလုံးကို တန်ဖိုး 0 ဖြင့် အစားထိုးနိုင်သည်-
import numpy as np
#create NumPy array
data = np. array ([1, 2, 3, 4, 5, 6])
#define custom function to shift elements
def shift_elements (arr, num, fill_value):
result = np. empty_like (arr)
if num > 0:
result[:num] = fill_value
result[num:] = arr[:-num]
elif num < 0:
result[num:] = fill_value
result[:num] = arr[-num:]
else :
result[:] = arr
return result
#shift each element two positions to the right and replace shifted values with zero
data_new = shift_elements(data, 2, 0)
#view new NumPy array
data_new
array([0, 0, 1, 2, 3, 4])
ဒြပ်စင်များကို ဘယ်ဘက်သို့ပြောင်းရန် လုပ်ဆောင်ချက်တွင် အနှုတ်နံပါတ်ကိုလည်း အသုံးပြုနိုင်သည်။
import numpy as np
#create NumPy array
data = np. array ([1, 2, 3, 4, 5, 6])
#define custom function to shift elements
def shift_elements (arr, num, fill_value):
result = np. empty_like (arr)
if num > 0:
result[:num] = fill_value
result[num:] = arr[:-num]
elif num < 0:
result[num:] = fill_value
result[:num] = arr[-num:]
else :
result[:] = arr
return result
#shift each element three positions to the left and replace shifted values with 50
data_new = shift_elements(data, -3, 50)
#view new NumPy array
data_new
array([ 4, 5, 6, 50, 50, 50])
ထပ်လောင်းအရင်းအမြစ်များ
အောက်ဖော်ပြပါ သင်ခန်းစာများသည် NumPy တွင် အခြားသော ဘုံလုပ်ဆောင်ချက်များကို မည်သို့လုပ်ဆောင်ရမည်ကို ရှင်းပြသည်-
NumPy တွင် ဒြပ်စင်ဖြစ်ပျက်မှုများကို ရေတွက်နည်း
NumPy အခင်းအကျင်းကို ကော်လံအလိုက် ဘယ်လိုစီမလဲ။
NumPy array ၏မုဒ်ကို တွက်ချက်နည်း