Pandas dataframe မှ ရထားတစ်ခု ဖန်တီးပြီး စမ်းသပ်နည်း


စက်သင်ယူမှုမော်ဒယ်များကို ဒေတာအတွဲများနှင့် အံဝင်ခွင်ကျဖြစ်စေသောအခါ၊ ကျွန်ုပ်တို့သည် ဒေတာအတွဲများကို နှစ်စုံအဖြစ် ခွဲလေ့ရှိသည်-

1. Training set- မော်ဒယ်ကို လေ့ကျင့်ရန် အသုံးပြုသည် (မူရင်းဒေတာအတွဲ၏ 70-80%)

2. စမ်းသပ်မှုအစုံ- မော်ဒယ်စွမ်းဆောင်ရည်၏ ဘက်မလိုက်သော ခန့်မှန်းချက် (မူရင်းဒေတာအတွဲ၏ 20-30%) ရရှိရန် အသုံးပြုသည်။

Python တွင်၊ ပန်ဒါ DataFrame ကို လေ့ကျင့်ရေးအစုံနှင့် စမ်းသပ်မှုအစုအဖြစ် ခွဲရန် ဘုံနည်းလမ်းနှစ်ခုရှိသည်။

နည်းလမ်း 1- sklearn ၏ train_test_split() ကိုသုံးပါ

 from sklearn. model_selection import train_test_split

train, test = train_test_split(df, test_size= 0.2 , random_state= 0 )

နည်းလမ်း 2- ပန်ဒါများမှ sample() ကိုသုံးပါ။

 train = df. sample (frac= 0.8 , random_state= 0 )
test = df. drop ( train.index )

အောက်ပါနမူနာများသည် အောက်ပါ pandas DataFrame ဖြင့် နည်းလမ်းတစ်ခုစီကို အသုံးပြုနည်းကို ပြသသည်-

 import pandas as pd
import numpy as np

#make this example reproducible
n.p. random . seeds (1)

#create DataFrame with 1,000 rows and 3 columns
df = pd. DataFrame ( {' x1 ': np.random.randint (30,size=1000),
                   ' x2 ': np. random . randint (12, size=1000),
                   ' y ': np. random . randint (2, size=1000)})

#view first few rows of DataFrame
df. head ()

        x1 x2 y
0 5 1 1
1 11 8 0
2 12 4 1
3 8 7 0
4 9 0 0

ဥပမာ 1- sklearn မှ train_test_split() ကိုသုံးပါ။

အောက်ပါကုဒ်သည် pandas DataFrame ကို လေ့ကျင့်ရေးနှင့် စမ်းသပ်မှုအစုံအဖြစ် ခွဲရန် sklearn ‘s train_test_split() လုပ်ဆောင်ချက်ကို အသုံးပြုပုံကို ပြသသည်-

 from sklearn. model_selection import train_test_split

#split original DataFrame into training and testing sets
train, test = train_test_split(df, test_size= 0.2 , random_state= 0 )

#view first few rows of each set
print ( train.head ())

     x1 x2 y
687 16 2 0
500 18 2 1
332 4 10 1
979 2 8 1
817 11 1 0

print ( test.head ())

     x1 x2 y
993 22 1 1
859 27 6 0
298 27 8 1
553 20 6 0
672 9 2 1

#print size of each set
print (train. shape , test. shape )

(800, 3) (200, 3)

ရလဒ်မှ ၂ စုံဖန်တီးထားသည်ကို ကျွန်ုပ်တို့တွေ့မြင်နိုင်သည်-

  • သင်တန်းအစုံ- အတန်း 800 နှင့် ကော်လံ 3 ခု
  • စမ်းသပ်မှုအစုံ- အတန်း 200 နှင့် ကော်လံ 3 ခု

test_size သည် test set နှင့် သက်ဆိုင်မည့် မူရင်း DataFrame မှ စူးစမ်းလေ့လာမှုများ၏ ရာခိုင်နှုန်းကို ထိန်းချုပ်ပြီး random_state တန်ဖိုးသည် ခွဲခြမ်းကို ပြန်ထုတ်နိုင်စေကြောင်း သတိပြုပါ။

ဥပမာ 2- ပန်ဒါများမှ sample() ကိုသုံးပါ။

ပန်ဒါ DataFrame ကို လေ့ကျင့်ရေး နှင့် စမ်းသပ်မှု အစုံအဖြစ် ခွဲရန် အောက်ပါ ကုဒ်သည် pandas နမူနာ() လုပ်ဆောင်ချက်ကို အသုံးပြုနည်းကို ပြသသည်-

 #split original DataFrame into training and testing sets
train = df. sample (frac= 0.8 , random_state= 0 )
test = df. drop ( train.index )

#view first few rows of each set
print ( train.head ())

     x1 x2 y
993 22 1 1
859 27 6 0
298 27 8 1
553 20 6 0
672 9 2 1

print ( test.head ())

    x1 x2 y
9 16 5 0
11 12 10 0
19 5 9 0
23 28 1 1
28 18 0 1

#print size of each set
print (train. shape , test. shape )

(800, 3) (200, 3)

ရလဒ်မှ ၂ စုံဖန်တီးထားသည်ကို ကျွန်ုပ်တို့တွေ့မြင်နိုင်သည်-

  • သင်တန်းအစုံ- အတန်း 800 နှင့် ကော်လံ 3 ခု
  • စမ်းသပ်မှုအစုံ- အတန်း 200 နှင့် ကော်လံ 3 ခု

frac သည် လေ့ကျင့်မှုအစုနှင့် သက်ဆိုင်သည့် မူရင်း DataFrame မှ ကြည့်ရှုမှုရာခိုင်နှုန်းကို ထိန်းချုပ်ပြီး random_state တန်ဖိုးသည် ခွဲခြမ်းကို ပြန်လည်ထုတ်လုပ်နိုင်စေကြောင်း သတိပြုပါ။

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

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

Python တွင် Logistic Regression ကို မည်သို့လုပ်ဆောင်မည်နည်း။
Python တွင် Confusion Matrix ဖန်တီးနည်း
Python တွင် မျှတသောတိကျမှုကို တွက်ချက်နည်း

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

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