သင်တန်းတွင် ဒေတာခွဲနည်း & r (နည်းလမ်း 3 ခု) တွင် စမ်းသပ်မှုအစုံ


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

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

နည်းလမ်း 1- Base R ကိုသုံးပါ။

 #make this example reproducible
set. seeds (1)

#use 70% of dataset as training set and 30% as test set
sample <- sample(c( TRUE , FALSE ), nrow(df), replace= TRUE , prob=c( 0.7 , 0.3 ))
train <- df[sample, ]
test <- df[!sample, ]

နည်းလမ်း 2- caTools package ကိုသုံးပါ။

 library (caTools)

#make this example reproducible
set. seeds (1)

#use 70% of dataset as training set and 30% as test set
sample <- sample. split (df$any_column_name, SplitRatio = 0.7 )
train <- subset(df, sample == TRUE )
test <- subset(df, sample == FALSE )

နည်းလမ်း 3- dplyr package ကိုသုံးပါ။

 library (dplyr)

#make this example reproducible
set. seeds (1)

#create ID column
df$id <- 1:nrow(df)

#use 70% of dataset as training set and 30% as test set
train <- df %>% dplyr::sample_frac( 0.70 )
test <- dplyr::anti_join(df, train, by = ' id ')

အောက်ပါဥပမာများသည် R ရှိ built-iniris dataset ဖြင့် လက်တွေ့တွင် နည်းလမ်းတစ်ခုစီကို အသုံးပြုနည်းကို ပြသထားသည်။

ဥပမာ 1- Base R ကို အသုံးပြု၍ လေ့ကျင့်ရေး နှင့် စမ်းသပ်မှု အစုံသို့ ဒေတာကို ခွဲပါ။

အောက်ဖော်ပြပါ ကုဒ်သည် အတန်း၏ 70% ကို လေ့ကျင့်မှုအဖြစ် သတ်မှတ်ပြီး ကျန် 30% အား စမ်းသပ်မှုအဖြစ် အတန်းများကို အသုံးပြုကာ မျက်ဝန်းဒေတာအစုံကို လေ့ကျင့်ရေးနှင့် စမ်းသပ်မှုအစုအဖြစ် ခွဲရန် R အခြေခံကို အသုံးပြုနည်းကို ပြသသည်-

 #load iris dataset
data(iris)

#make this example reproducible
set. seeds (1)

#Use 70% of dataset as training set and remaining 30% as testing set
sample <- sample(c( TRUE , FALSE ), nrow(iris), replace= TRUE , prob=c( 0.7 , 0.3 ))
train <- iris[sample, ]
test <- iris[!sample, ]

#view dimensions of training set
sun(train)

[1] 106 5

#view dimensions of test set
dim(test)

[1] 44 5

ရလဒ်မှ ကျွန်ုပ်တို့ မြင်နိုင်သည်-

  • လေ့ကျင့်မှုအစုံသည် 106 အတန်းနှင့် 5 ကော်လံများ၏ ဒေတာဘောင်တစ်ခုဖြစ်သည်။
  • စမ်းသပ်မှုသည် 44 အတန်းနှင့် 5 ကော်လံများ၏ဒေတာဘလောက်တစ်ခုဖြစ်သည်။

မူရင်းဒေတာဘေ့စ်တွင် စုစုပေါင်းအတန်း 150 တန်းရှိသောကြောင့် လေ့ကျင့်ရေးအစုတွင် ခန့်မှန်းခြေအားဖြင့် 106/150 = 70.6% ပါ၀င်ပါသည်။

ကျွန်ုပ်တို့လိုချင်ပါက လေ့ကျင့်ရေးအစုံ၏ ပထမတန်းအနည်းငယ်ကိုလည်း ပြသနိုင်သည်-

 #view first few rows of training set
head(train)

  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
8 5.0 3.4 1.5 0.2 setosa
9 4.4 2.9 1.4 0.2 setosa

ဥပမာ 2- caTools ကို အသုံးပြု၍ လေ့ကျင့်ရေးနှင့် စမ်းသပ်မှုအစုံသို့ ဒေတာကို ပိုင်းခြားပါ။

အောက်ဖော်ပြပါကုဒ်သည် အတန်း၏ 70% ကို လေ့ကျင့်ရေးအစုအဖြစ်နှင့် ကျန် 30% ကို စမ်းသပ်သတ်မှတ်အဖြစ် အတန်း၏ 70% ကိုအသုံးပြုကာ iris ဒေတာအစုံကို လေ့ကျင့်ရေးနှင့်စမ်းသပ်မှုအစုအဖြစ် ခွဲရန် R ရှိ caTools ပက်ကေ့ဂျ်ကို အသုံးပြုနည်းကို ဖော်ပြသည်-

 library (caTools)

#load iris dataset
data(iris)

#make this example reproducible
set. seeds (1)

#Use 70% of dataset as training set and remaining 30% as testing set
sample <- sample. split (iris$Species, SplitRatio = 0.7 )
train <- subset(iris, sample == TRUE )
test <- subset(iris, sample == FALSE )

#view dimensions of training set
sun(train)

[1] 105 5

#view dimensions of test set
dim(test)

[1] 45 5

ရလဒ်မှ ကျွန်ုပ်တို့ မြင်နိုင်သည်-

  • သင်တန်းအစုံသည် 105 တန်း နှင့် 5 ကော်လံပါ ဒေတာဘောင်တစ်ခုဖြစ်သည်။
  • စမ်းသပ်မှုသည် ၄၅ တန်းနှင့် ကော်လံ ၅ ခုပါသည့် ဒေတာဘလောက်တစ်ခုဖြစ်သည်။

ဥပမာ 3- dplyr ကို အသုံးပြု၍ လေ့ကျင့်ရေးနှင့် စမ်းသပ်မှုအစုံသို့ ဒေတာကို ပိုင်းခြားပါ။

အောက်ဖော်ပြပါကုဒ်သည် အတန်း၏ 70% ကို လေ့ကျင့်ရေးအစုအဖြစ်နှင့် ကျန် 30% ကို စမ်းသပ်သတ်မှတ်အဖြစ် အတန်း၏ 70% ကိုအသုံးပြုကာ iris ဒေတာအစုံကို လေ့ကျင့်ရေးနှင့်စမ်းသပ်မှုအစုအဖြစ် ခွဲရန် R ရှိ caTools ပက်ကေ့ဂျ်ကို အသုံးပြုနည်းကို ဖော်ပြသည်-

 library (dplyr)

#load iris dataset
data(iris)

#make this example reproducible
set. seeds (1)

#create variable ID
iris$id <- 1:nrow(iris)

#Use 70% of dataset as training set and remaining 30% as testing set 
train <- iris %>% dplyr::sample_frac( 0.7 )
test <- dplyr::anti_join(iris, train, by = ' id ')

#view dimensions of training set
sun(train)

[1] 105 6

#view dimensions of test set
dim(test)

[1] 45 6

ရလဒ်မှ ကျွန်ုပ်တို့ မြင်နိုင်သည်-

  • လေ့ကျင့်မှုအစုံသည် 105 တန်းနှင့် 6 ကော်လံများ၏ ဒေတာဘောင်တစ်ခုဖြစ်သည်။
  • စမ်းသပ်မှုသည် ၄၅ တန်းနှင့် ကော်လံ ၆ ခုပါသည့် ဒေတာဘလောက်တစ်ခုဖြစ်သည်။

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

သင့်စက်သင်ယူမှု အယ်လဂိုရီသမ်ကို ချိန်ညှိသောအခါတွင် သင်သည် ဤကော်လံကို မသုံးပါနှင့် (သို့မဟုတ် ၎င်းကို ဒေတာဘောင်များမှ လုံးဝဖယ်ရှားခြင်း) သေချာပါစေ။

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

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

R ဖြင့် MSE တွက်နည်း
R ဖြင့် RMSE တွက်နည်း
R ဖြင့် ချိန်ညှိထားသော R-squared တွက်ချက်နည်း

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

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