chapter - data mining trenddataminingtrend.com/2014/wp-content/uploads/2018/02/...• เก บข...
TRANSCRIPT
Eakasit Pacharawongsakda, Ph.D.
Certified RapidMiner Analyst & Ambassador
Data Cube
facebook.com/datacube.th
www.dataminingtrend.com
Advanced Predictive Modeling with R & RapidMiner Studio 7
Introduction to RChapter 3
http://dataminingtrend.com http://facebook.com/datacube.th
Introduction to R• แนะนำ R และ RStudio
• โครงสรางขอมลพนฐานใน R
• การอานไฟลขอมล
• การเขยนโปรแกรมภาษา R เบองตน
• การเลอกเงอนไข (IF)
• การวนรอบ (loop)
• การเขยนฟงกชน
• การสรางกราฟดวย R เบองตน
• การสรางโมเดล classification ดวย R เบองตน
3
http://dataminingtrend.com http://facebook.com/datacube.th
Introduction to R• R คอ ซอฟตแวร open source ทใชทางดานการคำนวณเชงสถต
• ดาวนโหลดไดจาก https://www.r-project.org
• ตดตงไดทง Windows, OS X และ Linux
4
http://dataminingtrend.com http://facebook.com/datacube.th
Introduction to R
5source: https://www.youtube.com/watch?v=TR2bHSJ_eck
http://dataminingtrend.com http://facebook.com/datacube.th
Introduction to R• จากผลการสำรวจของเวบไซต kdnuggets.com พบวาเปนภาษาทมผ
ใชในการวเคราะหขอมลมากทสดในป 2014 และ 2015
6
source: http://www.kdnuggets.com/polls/2015/r-vs-python.html
http://dataminingtrend.com http://facebook.com/datacube.th
Introduction to R• R เปนภาษาโปรแกรมอยางหนงทใชงานงายกวา C/C++ หรอ Java
• พมพคำสงผานทาง R Console
7
http://dataminingtrend.com http://facebook.com/datacube.th
Introduction to R• เพอใหการใชงาน R งายขนมคนพฒนาซอฟตแวร RStudio เพอเปน
editor ซงสามารถสงใหทำงานไดเลย
• ดาวนโหลด RStudio ไดจาก https://www.rstudio.com
8
http://dataminingtrend.com http://facebook.com/datacube.th
Introduction to RStudio
9
1
Console 2 Environment
3
R packages
http://dataminingtrend.com http://facebook.com/datacube.th
Introduction to RStudio• คำสง ? ใชเพอเรยกด help ของ R
10
> ?read.csv
เรยกด help ของ R
http://dataminingtrend.com http://facebook.com/datacube.th
Introduction to R• แนะนำ R และ RStudio
• โครงสรางขอมลพนฐานใน R
• การอานไฟลขอมล
• การเขยนโปรแกรมภาษา R เบองตน
• การเลอกเงอนไข (IF)
• การวนรอบ (loop)
• การเขยนฟงกชน
• การสรางกราฟดวย R เบองตน
• การสรางโมเดล classification ดวย R เบองตน
11
http://dataminingtrend.com http://facebook.com/datacube.th
R Data Structures: Vectors• เวกตอร (Vector)
• เกบขอมลเรยงตามลำดบ และขอมลจะตองเปนประเภทเดยวกนทงหมด เชน ตวเลข หรอ ขอความ
12
ID15101
ID15102
ID15103
ID15104
[1]
[2]
[3]
[4]
เวกเตอร customer_id
> customer_id <- c("ID15101", "ID15102", "ID15103", "ID15104")
การสราง Vector
http://dataminingtrend.com http://facebook.com/datacube.th
R Data Structures: Vectors• เวกตอร (Vector)
• เกบขอมลเรยงตามลำดบ และขอมลจะตองเปนประเภทเดยวกนทงหมด เชน ตวเลข หรอ ขอความ
13
> customer_id[2] [1] "ID15102"
> customer_id[3] [1] "ID15103"
ดงขอมลจาก VectorID15101
ID15102
ID15103
ID15104
[1]
[2]
[3]
[4]
1
2
เวกเตอร customer_id
http://dataminingtrend.com http://facebook.com/datacube.th
R Data Structures: Factors• แฟคเตอร (Factor)
• มลกษณะเหมอนกบเวกเตอร แตเกบขอมลประเภททเปนนอมนอล (nominal) หรอ categorical เทานน
14
FEMALE
MALE
FEMALE
FEMALE
[1]
[2]
[3]
[4]
แฟคเตอร gender
> gender <- factor(c("FEMALE", "MALE", "FEMALE",
"FEMALE"))
การสราง Factor
> gender [1] FEMALE MALE FEMALE FEMALE Levels: FEMALE MALE
การเรยกใชงาน Factor
http://dataminingtrend.com http://facebook.com/datacube.th
R Data Structures: List• ลสต (List)
• เกบขอมลเรยงตามลำดบ และเกบขอมลตางประเภท (type) กนได (ซงไมเหมอน Vector ทเกบประเภทเดยวกนเทานน)
15
ID15101
ID15102
ID15103
ID15104
[1]
[2]
[3]
[4]
เวกเตอร customer_id
48
40
51
23
เวกเตอร age
FEMALE
MALE
FEMALE
FEMALE
เวกเตอร gender
17546.0
30085.1
16575.4
20375.4
เวกเตอร income
ID15101 48 FEMALE 17546.0
$customer_id $age $gender $income
ลสต john_doe
[1]
[2]
[3]
[4]
[1]
[2]
[3]
[4]
[1]
[2]
[3]
[4]
http://dataminingtrend.com http://facebook.com/datacube.th
R Data Structures: List
16
> age <- c(48,40,51,23); > gender <- c("FEMALE","MALE","FEMALE","FEMALE"); > income <- c(17546.0,30085.1,16575.4,20375.4); > john_doe <- list(customer_id = customer_id[1], age = age[1], gender = gender[1], income = income[1])
การสราง List
> john_doe$customer_id [1] "ID15101"
การดงขอมลจาก List
http://dataminingtrend.com http://facebook.com/datacube.th
R Data Structures: List
17
> john_doe$customer_id
[1] "ID15101"
> john_doe$age [1] 48
> john_doe$gender [1] "FEMALE"
> john_doe$income [1] 17546
การดงขอมลจาก List
> john_doe $customer_id [1] "ID15101" $age [1] 48 $gender [1] "FEMALE" $income [1] 17546
การดงขอมลจาก List
http://dataminingtrend.com http://facebook.com/datacube.th
R Data Structures: Data Frame• ดาตา เฟรม (Data frame)
• เกบขอมลในลกษณะของตาราง ประกอบดวยแถว (row) และ คอลมน (column) ซงเกบคาประเภทตางกนได
18
ID15101
ID15102เวกเตอร
customer_id
48
40
[1]
[2]
เวกเตอร age
FEMALE
MALE
เวกเตอร gender
17546.0
30085.1
เวกเตอร income
ID15101 48 FEMALE 17546.0
$customer_id $age $gender $income
ID15101 48 FEMALE 17546.0
[1]
[2]
[1]
[2]
[1]
[2]
[1]
[2]
ดาตา เฟรม customer
http://dataminingtrend.com http://facebook.com/datacube.th
R Data Structures: Data Frame
19
> customer <- data.frame(customer_id, age, gender, income)
การสราง Data frame
> customer customer_id age gender income 1 ID15101 48 FEMALE 17546.0 2 ID15102 40 MALE 30085.1 3 ID15103 51 FEMALE 16575.4 4 ID15104 23 FEMALE 20375.4
การดงขอมลจาก Data frame
http://dataminingtrend.com http://facebook.com/datacube.th
R Data Structures: Data Frame
20
> customer[1,1] # [i,j], i = row, j = column [1] ID15101 Levels: ID15101 ID15102 ID15103 ID15104
> customer[1,] # show only 1st row customer_id age gender income 1 ID15101 48 FEMALE 17546
> customer[,1] # show only 1st column [1] ID15101 ID15102 ID15103 ID15104 Levels: ID15101 ID15102 ID15103 ID15104
การดงขอมลจาก Data frame
http://dataminingtrend.com http://facebook.com/datacube.th
R Data Structures: Data Frame
21
> customer[1,1:2] customer_id age
1 ID15101 48
> customer[1,c(1:2,4)] customer_id age income 1 ID15101 48 17546
> customer[1,-3] customer_id age income 1 ID15101 48 17546
การดงขอมลจาก Data frame
http://dataminingtrend.com http://facebook.com/datacube.th
R Data Structures: Matrix• เมทรกซ (Matrix)
• เกบขอมลในรปแบบแถวและคอลมนเหมอนกบ data frame แตตองปนขอมลประเภทเดยวกนสวนใหญจะเปนขอมลประเภทตวเลข
22
100 500 900 1300
200 600 1000 1400
300 700 1100 1500
400 800 1200 1600
[1]
[3]
[4]
[1] [2] [3] [4]
[2]
http://dataminingtrend.com http://facebook.com/datacube.th
R Data Structures: Matrix
23
> data <- matrix(c(100,200,300,400,500,600, 700,800,900,1000,1100,1200, 1300,1400,1500,1600), nrow=4, ncol=4)
การสราง Matrix
> data [,1] [,2] [,3] [,4] [1,] 100 500 900 1300 [2,] 200 600 1000 1400 [3,] 300 700 1100 1500 [4,] 400 800 1200 1600
ดงขอมลจาก Matrix
http://dataminingtrend.com http://facebook.com/datacube.th
Introduction to R• แนะนำ R และ RStudio
• โครงสรางขอมลพนฐานใน R
• การอานไฟลขอมล
• การเขยนโปรแกรมภาษา R เบองตน
• การเลอกเงอนไข (IF)
• การวนรอบ (loop)
• การเขยนฟงกชน
• การสรางกราฟดวย R เบองตน
• การสรางโมเดล classification ดวย R เบองตน
24
http://dataminingtrend.com http://facebook.com/datacube.th
Importing Data• คำสงสำหรบการอานขอมลประเภท CSV
• ชอไฟลประเภท CSV
• กำหนดใหแถวแรกเปนชอแอตทรบวต
25
> data <- read.csv("Rcustomers.csv",header=TRUE)
การอานไฟล CSV
1 2
1
2
http://dataminingtrend.com http://facebook.com/datacube.th
Importing Data• การอานไฟล CSV ดวย RStudio
• ในสวนของ Environment เลอก Import Dataset
• เลอกเมน From Text File
26
1
http://dataminingtrend.com http://facebook.com/datacube.th
Importing Data• เปลยนชอ dataset ไดในชอง Name
27
2
http://dataminingtrend.com http://facebook.com/datacube.th
Importing Data• ขอมลท import เขามาจะแสดงในสวนดานซายบน
28
ขอมลท import เขามา
http://dataminingtrend.com http://facebook.com/datacube.th
Introduction to R• แนะนำ R และ RStudio
• โครงสรางขอมลพนฐานใน R
• การอานไฟลขอมล
• การเขยนโปรแกรมภาษา R เบองตน
• การเลอกเงอนไข (IF)
• การวนรอบ (loop)
• การเขยนฟงกชน
• การสรางกราฟดวย R เบองตน
• การสรางโมเดล classification ดวย R เบองตน
29
http://dataminingtrend.com http://facebook.com/datacube.th
Conditional execution• คำสงในการตรวจสอบเงอนไข ใช IF
• ถาเงอนไขหลง if เปนจรง (true) จะทำตามคำสงทอยหลง if
• อาจจะมมากกวา 1 คำสงไดแตตองอยในวงเลบ {}
• ถาเงอนไขเปนเทจ (false) จะทำตามคำสงหลง else
30
> if (condition) { # code executed when condition is TRUE } else { # code executed when condition is FALSE }
คำสง IF
http://dataminingtrend.com http://facebook.com/datacube.th
Conditional execution• ตวอยางการใชงาน
31
> accuracy.DT <- 70.5 > accuracy.KNN <- 93.0 > if (accuracy.DT > accuracy.KNN) {
best.technique <- “DT” } else {
best.technique <- “KNN” } > best.technique # KNN
ตวอยางคำสง IF
http://dataminingtrend.com http://facebook.com/datacube.th
Introduction to R• แนะนำ R และ RStudio
• โครงสรางขอมลพนฐานใน R
• การอานไฟลขอมล
• การเขยนโปรแกรมภาษา R เบองตน
• การเลอกเงอนไข (IF)
• การวนรอบ (loop)
• การเขยนฟงกชน
• การสรางกราฟดวย R เบองตน
• การสรางโมเดล classification ดวย R เบองตน
32
http://dataminingtrend.com http://facebook.com/datacube.th
Repetitive execution• คำสงในการวนรอบ (loop)
• คำสง for จะวนรอบโดยท
• name คอ ชอตวแปร
• expression1 คอ เงอนไขในการวนรอบ เชน 1:20
33
> for (name in expression1) { # code executed }
คำสง FOR
http://dataminingtrend.com http://facebook.com/datacube.th
Repetitive execution• ตวอยางคำสงในการวนรอบ (loop)
34
> fold <- seq(1:10) > for (i in 1:length(fold)) {
cat(“Fold no. ”,i,”\n”) } # Fold no. 1 # Fold no. 2 # Fold no. 3 # Fold no. 4 # Fold no. 5 # Fold no. 6 # …
ตวอยางคำสง FOR
> fold <- seq(1:10) > for (i in fold) {
cat(“Fold no. ”,i,”\n”) } # Fold no. 1 # Fold no. 2 # Fold no. 3 # Fold no. 4 # Fold no. 5 # Fold no. 6 # …
ตวอยางคำสง FOR
http://dataminingtrend.com http://facebook.com/datacube.th
Repetitive execution• ตวอยางคำสงในการวนรอบ (loop)
35
> accuracy <- c(70.5, 93.0, 85.6) > best.accuracy <- 0 > for (i in accuracy) {
if (i > best.accuracy) best.accuracy <- i
} > best.accuracy # 93.0
ตวอยางคำสง FOR
http://dataminingtrend.com http://facebook.com/datacube.th
Introduction to R• แนะนำ R และ RStudio
• โครงสรางขอมลพนฐานใน R
• การอานไฟลขอมล
• การเขยนโปรแกรมภาษา R เบองตน
• การเลอกเงอนไข (IF)
• การวนรอบ (loop)
• การเขยนฟงกชน
• การสรางกราฟดวย R เบองตน
• การสรางโมเดล classification ดวย R เบองตน
36
http://dataminingtrend.com http://facebook.com/datacube.th
Function in R• รปแบบของคำสงการสรางฟงกชน (function)
• name คอ ชอฟงกชนทสรางขนมา
• function เปน keyword สำหรบสรางฟงกชน
• arg1, arg2 คอ argument ทสงใหฟงกชน
37
> name <- function(arg1, arg2) { expression
}
คำสง FUNCTION
http://dataminingtrend.com http://facebook.com/datacube.th
Function in R• ตวอยางการสรางฟงกชน rescale01
38
> rescale01 <- function(x) { rng <- range(x, na.rm = TRUE) (x - rng[1]) / (rng[2] - rng[1])
} > data <- c(0,5,10) > result <- rescale01(data) > result # 0.0 0.5 1.0
ตวอยางการสราง FUNCTION
http://dataminingtrend.com http://facebook.com/datacube.th
Function in R• ตวอยางการสรางฟงกชน performance
39
> performance <- function(TP,FP,TN,FN) { precision <- 100*TP/(TP+FP) recall <- 100*TP/(TP+FN) F.measure <- (2*precision*recall)/(precision+recall) list(precision=precision, recall=recall, F.measure=F.measure)
} > TP <- 100 > FP <- 55 > TN <- 35 > FN <- 40
ตวอยางการสราง FUNCTION
http://dataminingtrend.com http://facebook.com/datacube.th
Function in R• ตวอยางการสรางฟงกชน performance (ตอ)
40
> result <- performance(TP,FP,TN,FN) # $precision # [1] 64.51613 # $recall # [1] 71.42857 # $F.measure # [1] 67.79661 > result$precision # [1] 64.51613 > result$F.measure # [1] 71.42857
ตวอยางการสราง FUNCTION
http://dataminingtrend.com http://facebook.com/datacube.th
Introduction to R• แนะนำ R และ RStudio
• โครงสรางขอมลพนฐานใน R
• การอานไฟลขอมล
• การเขยนโปรแกรมภาษา R เบองตน
• การเลอกเงอนไข (IF)
• การวนรอบ (loop)
• การเขยนฟงกชน
• การสรางกราฟดวย R เบองตน
• การสรางโมเดล classification ดวย R เบองตน
41
http://dataminingtrend.com http://facebook.com/datacube.th
Visualization with R• การสรางกราฟแบบตางๆ ดวยภาษา R สามารถทำไดงายโดยใช
package ggplot2
• ตดตง ggplot2 ดวยคำสง install.packages()
• ทดสอบดวยการเรยกใชงานดวยคำสง library()
42
> install.packages("ggplot2")
ตดตง ggplot2
> library("ggplot2")
เรยกใชงาน ggplot2
http://dataminingtrend.com http://facebook.com/datacube.th
Visualization with R: Bar & Line graph• ขอมลทใชงาน ggplot2 ไดตองเปนประเภท data frame เทานน
43
> dat <- data.frame( model = factor(c("DT","KNN","NN"), levels=c("DT","KNN","NN")), accuracy = c(70.5, 93.0, 85.6)) > dat
#> model accuracy #> 1 DT 70.5 #> 2 KNN 93.0 #> 3 NN 85.6
# Load the ggplot2 package > library(ggplot2)
สรางขอมล
http://dataminingtrend.com http://facebook.com/datacube.th
Visualization with R: Bar & Line graph• กำหนดให
• แกน X คอ ตวแปร model
• แกน Y คอ ตวแปร accuracy และแสดงความสงเปนคาตวเลข (โดยการกำหนด parameter stat="identity"
44
> ggplot(data=dat, aes(x=model, y=accuracy))+ geom_bar(stat="identity")
สรางกราฟแทงแบบพนฐาน
http://dataminingtrend.com http://facebook.com/datacube.th
Visualization with R: Bar & Line graph
45
> ggplot(data=dat, aes(x=model, y=accuracy, fill=model)) + geom_bar(stat="identity")
สรางกราฟแทงแบบมส
http://dataminingtrend.com http://facebook.com/datacube.th
Visualization with R: Bar & Line graph
46
> accuracy = c(70.5, 93.0, 85.6) > ggplot(data=dat, aes(x=model, y=accuracy, fill=model)) + geom_bar(stat=“identity") + geom_text(aes(label = accuracy), size = 5, hjust = 0.5, vjust = 2)
สรางกราฟแทงแบบมส
http://dataminingtrend.com http://facebook.com/datacube.th
Visualization with R: Bar & Line graph
47
> ggplot(data=dat, aes(x=model, y=accuracy, fill=model)) + geom_bar(stat=“identity") + geom_text(aes(label = accuracy), size = 5, hjust = 0.5, vjust = 2) + xlab("Classifiers") + ylab("Accuracy") + ggtitle("Compare performance")
สรางกราฟแทงแบบมส
http://dataminingtrend.com http://facebook.com/datacube.th
Visualization with R: Bar & Line graph
48
> ggplot(data=dat, aes(x=model, y=accuracy, group=1))+ geom_line()
สรางกราฟเสนแบบพนฐาน
http://dataminingtrend.com http://facebook.com/datacube.th
Visualization with R: Bar & Line graph
49
> ggplot(data=dat, aes(x=model, y=accuracy, group=1))+ geom_line()+ geom_point()
สรางกราฟเสนและจด
http://dataminingtrend.com http://facebook.com/datacube.th
Visualization with R: Bar & Line graph
50
> ggplot(data=dat, aes(x=model, y=accuracy, group=1)) + geom_line(colour="red") + geom_point(colour="red")
สรางกราฟเสนและจดมส
http://dataminingtrend.com http://facebook.com/datacube.th
Visualization with R: Bar & Line graph• ขอมลมมากกวา 1 ชด
51
> dat2 <- data.frame(dataset = factor(c("fold1", "fold2", "fold3", "fold4", "fold5")), accuracyDT = c(70.5, 76.2, 77.0, 78.1, 74.5), accuracyKNN = c(93.0, 90.2, 91.0, 85.5, 86.6), accuracyNN = c(85.6, 80.1, 82.0, 81.0, 80.0))
> dat2 # dataset accuracyDT accuracyKNN accuracyNN # 1 fold1 70.5 93.0 85.6
# 2 fold2 76.2 90.2 80.1 # 3 fold3 77.0 91.0 82.0
# 4 fold4 78.1 85.5 81.0
# 5 fold5 74.5 86.6 80.0
สรางขอมล
http://dataminingtrend.com http://facebook.com/datacube.th
Visualization with R: Bar & Line graph• เปลยนรปแบบของขอมลตามแบบท ggplot2 รองรบ
52
> library(reshape2) > dat3 <- melt(dat2, id.vars="dataset") > dat3 # dataset variable value 1 fold1 accuracyDT 70.5
2 fold2 accuracyDT 76.2 3 fold3 accuracyDT 77.0
4 fold4 accuracyDT 78.1
5 fold5 accuracyDT 74.5 6 fold1 accuracyKNN 93.0
7 fold2 accuracyKNN 90.2 …
เปลยนรปแบบขอมล
http://dataminingtrend.com http://facebook.com/datacube.th
Visualization with R: Bar & Line graph
53
> bar <- ggplot(data=dat3, aes(x=variable, y=value, group=dataset, fill=dataset)) + geom_bar(stat=“identity", position=position_dodge()) > bar
http://dataminingtrend.com http://facebook.com/datacube.th
Visualization with R: Bar & Line graph
54
> line <- ggplot(data=dat3, aes(x=dataset, y=value, group=variable, colour=variable)) + geom_line()+ geom_point() > line
สรางกราฟเสนแบบหลายชดขอมล
http://dataminingtrend.com http://facebook.com/datacube.th
Visualization with R: Bar & Line graph
55
> source(“D:/datacube/software/R/code/function.R”) > multiplot(line,bar)
สรางกราฟแบบหลายชดขอมล
http://dataminingtrend.com http://facebook.com/datacube.th
Introduction to R• แนะนำ R และ RStudio
• โครงสรางขอมลพนฐานใน R
• การอานไฟลขอมล
• การเขยนโปรแกรมภาษา R เบองตน
• การเลอกเงอนไข (IF)
• การวนรอบ (loop)
• การเขยนฟงกชน
• การสรางกราฟดวย R เบองตน
• การสรางโมเดล classification ดวย R เบองตน
56
http://dataminingtrend.com http://facebook.com/datacube.th
Linear Regression model using R• ในการสรางโมเดล linear regression ใชฟงกชน lm()
• โดยท
• dv คอ dependent variable หรอ แอตทรบวตทเปนลาเบลคำตอบ
• iv คอ independent variable หรอ แอตทรบวตทเปนแอตทรบวตทวไป
• ทง dv และ iv จะเปนแอตทรบวตทอยในขอมล mydata
• ขอมล mydata คอ training data ทจะนำมาสรางโมเดล
57
> m <- lm(dv ~ iv, data=mydata)
การสรางโมเดล
> m <- lm(GPA_grad ~ GPA_undergrad, data=grade)
ตวอยางการสรางโมเดล
http://dataminingtrend.com http://facebook.com/datacube.th
Linear Regression model using R• ในการนำโมเดล linear regression มา predict ใชฟงกชน predict()
• โดยท
• m คอ โมเดล linear regression ทสรางไดจากฟงกชน lm()
• test คอ ขอมลทตองการหาคำตอบ (unlabeled data) หรอ อาจจะเปนขอมลทดสอบกได (test data)
58
> p <- predict(m, test)
การใชงานโมเดล
> p <- predict(m, test)
ตวอยางการใชงานโมเดล
http://dataminingtrend.com http://facebook.com/datacube.th
Example 3.1: Linear Regression• import ไฟล insurance_train.csv จากโฟลเดอร dataset
• สรางโมเดล linear regression โดยการระบแอตทรบวตทเกยวของทงหมด
• expenses คอ ตวแปรทเปนลาเบลคำตอบ
• age, children, bmi, sex, smoker และ region คอ ตวแปรทเปนแอตทรบวตทวไป
59
> insurance_train <- read.csv(“dataset/insurance_train.csv")
import ไฟล CSV
> m <- lm(expenses ~ age + children + bmi + sex + smoker + region, data=insurance_train)
การสรางโมเดล
http://dataminingtrend.com http://facebook.com/datacube.th
Example 3.1: Linear Regression• สรางโมเดล linear regression โดยการระบเครองหมาย .
• เครองหมายจด (.) แทนทกแอตทรบวตทใชในการสรางโมเดล
60
> m <- lm(expenses ~ ., data=insurance_train)
การสรางโมเดล
Call: lm(formula = expenses ~ ., data = insurance_train)
Coefficients:
(Intercept) age sexmale bmi children
-12979.5 259.0 115.2 361.7 462.2
smokeryes regionnorthwest regionsoutheast regionsouthwest
24109.9 -873.0 -996.4 -958.8
เรยกดโมเดล
http://dataminingtrend.com http://facebook.com/datacube.th
Example 3.1: Linear Regression• import ไฟล insurance_test.csv จากโฟลเดอร dataset
• การนำโมเดลทไดมาใชงาน
61
> insurance_test <- read.csv(“dataset/insurance_test.csv")
import ไฟล CSV
> p <- predict(m, insurance_test) > as.data.frame(p)
ตวอยางการใชงานโมเดล
http://dataminingtrend.com http://facebook.com/datacube.th
R extension for RM 7• เพอใหสามารถใชงานภาษา R ไดใน RapidMiner Studio 7 ตอง
ตดตง R Scripting 7.0
• เลอกเมน Extensions > Marketplace (Updates and Extensions…)
62
12
http://dataminingtrend.com http://facebook.com/datacube.th
R extension for RM 7• คนหา R extension
63
3
4
5
6
http://dataminingtrend.com http://facebook.com/datacube.th
R extension for RM 7• เลอก accept the terms of all license agreements
647
8
http://dataminingtrend.com http://facebook.com/datacube.th
R extension for RM 7• เมอดาวนโหลด extension เสรจเรยบรอยแลวจะตอง restart โปรแกรม
RapidMiner Studio 7 ใหม
• ไอคอนของ R Scriptingปรากฏขนมาตอน start
65
9
http://dataminingtrend.com http://facebook.com/datacube.th
R extension for RM 7• ตรวจสอบ path ของ R เพอให R Scripting extension ทำงานได
อยางถกตอง
66
10
11
http://dataminingtrend.com http://facebook.com/datacube.th
Testing R in RapidMiner Studio 7• เลอก process ตวอยางจากโอเปอเรเตอร Execute R
• คนหาโอเปอเรเตอร Execute R จากชอง search
67
1
2
34
http://dataminingtrend.com http://facebook.com/datacube.th
Testing R in RapidMiner Studio 7• แสดงตวอยาง process การสรางโมเดล linear regression ดวย R
68
http://dataminingtrend.com http://facebook.com/datacube.th
Testing R in RapidMiner Studio 7• ลบโอเปอเรเตอร Retrieve และ Split Data
• เลอกโอเปอเรเตอร Read CSV มาใสใน process เพออานไฟล training
69
5
http://dataminingtrend.com http://facebook.com/datacube.th
Testing R in RapidMiner Studio 7• อานไฟล insurance_train.csv ดวยโอเปอเรเตอร Read CSV
• เปลยนแอตทรบวต expenses ใหเปนประเภท label
70
6
http://dataminingtrend.com http://facebook.com/datacube.th
Testing R in RapidMiner Studio 7• เลอกโอเปอเรเตอร Read CSV ตวท 2 มาใสใน process เพออาน
ไฟล testing
71
7
http://dataminingtrend.com http://facebook.com/datacube.th
Testing R in RapidMiner Studio 7• อานไฟล insurance_test.csv ดวยโอเปอเรเตอร Read CSV
• เปลยนแอตทรบวต expenses ใหเปนประเภท label
72
8
http://dataminingtrend.com http://facebook.com/datacube.th
Testing R in RapidMiner Studio 7• คลกทโอเปอเรเตอร Execute R (ชอวา Learn Model)
• คลกทปม Edit Text ในพารามเตอร script
• แกไขคำสงใน R สวนของการสรางโมเดล
• formula = expense ~ .
73
http://dataminingtrend.com http://facebook.com/datacube.th
Testing R in RapidMiner Studio 7• กดปม Run เพอให process ทำงาน
74
ผลการทำนาย