Numpy တွင် matrix တစ်ခုသို့ အတန်းတစ်ခုထည့်နည်း (ဥပမာများနှင့်အတူ)
NumPy တွင် matrix တစ်ခုသို့ အတန်းတစ်ခုထည့်ရန် အောက်ပါ syntax ကို သုံးနိုင်သည်-
#add new_row to current_matrix current_matrix = np. vstack ([current_matrix, new_row])
အချို့သောအခြေအနေများနှင့်ကိုက်ညီသည့် matrix သို့ အတန်းများကိုသာထည့်ရန် အောက်ပါ syntax ကို သင်အသုံးပြုနိုင်သည်။
#only add rows where first element is less than 10 current_matrix = np. vstack ((current_matrix, new_rows[new_rows[:,0] < 10 ]))
အောက်ပါဥပမာများသည် ဤ syntax ကိုလက်တွေ့တွင်မည်သို့အသုံးပြုရမည်ကိုပြသထားသည်။
ဥပမာ 1- NumPy ရှိ matrix သို့ အတန်းတစ်ခုထည့်ပါ။
အောက်ပါကုဒ်သည် NumPy ရှိ matrix တစ်ခုသို့ အတန်းအသစ်တစ်ခုထည့်နည်းကို ပြသသည်-
import numpy as np
#define matrix
current_matrix = np. array ([[1,2,3], [4, 5, 6], [7, 8, 9]])
#define row to add
new_row = np. array ([10, 11, 12])
#add new row to matrix
current_matrix = np. vstack ([current_matrix, new_row])
#view updated matrix
current_matrix
array([[ 1, 2, 3],
[4,5,6],
[7, 8, 9],
[10, 11, 12]])
နောက်ဆုံးအတန်းကို matrix သို့ အောင်မြင်စွာထည့်ထားကြောင်း သတိပြုပါ။
ဥပမာ 2- အခြေအနေအပေါ်အခြေခံ၍ Matrix သို့ အတန်းများထည့်ပါ။
အောက်ပါကုဒ်သည် သီးခြားအခြေအနေတစ်ခုအပေါ် အခြေခံ၍ ရှိပြီးသား matrix တစ်ခုသို့ အတန်းအသစ်များစွာကို ထည့်သွင်းနည်းကို ပြသသည်-
import numpy as np
#define matrix
current_matrix = np. array ([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
#define potential new rows to add
new_rows = np. array ([[6, 8, 10], [8, 10, 12], [10, 12, 14]])
#only add rows where first element in row is less than 10
current_matrix = np. vstack ((current_matrix, new_rows[new_rows[:,0] < 10 ]))
#view updated matrix
current_matrix
array([[ 1, 2, 3],
[4,5,6],
[7, 8, 9],
[6, 8, 10],
[8, 10, 12]])
ပထမဒြပ်စင် 10 ထက်နည်းသော အတန်းများကိုသာ ထည့်ထားသည်။
မှတ်ချက် – vstack() လုပ်ဆောင်ချက်အတွက် အွန်လိုင်းစာရွက်စာတမ်းအပြည့်အစုံကို ဤနေရာတွင် ရှာတွေ့နိုင်ပါသည်။
ထပ်လောင်းအရင်းအမြစ်များ
အောက်ဖော်ပြပါ သင်ခန်းစာများသည် NumPy တွင် အခြားသော ဘုံလုပ်ဆောင်ချက်များကို မည်သို့လုပ်ဆောင်ရမည်ကို ရှင်းပြသည်-
NumPy array တွင် တန်ဖိုးအညွှန်းကိန်းကို မည်သို့ရှာရမည်နည်း။
Pandas DataFrame တွင် Numpy array ကိုမည်သို့ထည့်မည်နည်း။
NumPy အခင်းအကျင်းကို Pandas DataFrame သို့ ဘယ်လိုပြောင်းမလဲ။