-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel_tree.r
More file actions
58 lines (43 loc) · 1.25 KB
/
Copy pathModel_tree.r
File metadata and controls
58 lines (43 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#read data
Mydata <- read.csv("Mushrooms.csv")
#view dataset information
str(Mydata)
#views rows and colums
dim(Mydata)
#view head of dataset
head(Mydata)
#view tail of dataset
tail(Mydata)
#information gain function
InformationGain <- function( tble ) {
tble <- as.data.frame.matrix(tble)
entropyBefore <- Entropy(colSums(tble))
s <- rowSums(tble)
entropyAfter <- sum (s / sum(s) * apply(tble, MARGIN = 1, FUN = Entropy ))
informationGain <- entropyBefore - entropyAfter
return (informationGain)
}
#information gain of attribute ex.odor
tble <- table(Mydata[,c('odor', 'class')])
InformationGain(tble)
# split dataset into trainset and testset
set.seed(142)
dt = sort(sample(nrow(Mydata), nrow(Mydata)*.8))
trainset <- na.omit(Mydata[dt,])
testset <- na.omit(Mydata[-dt,])
#show rows and colums of trainset
dim(trainset)
#show rows and colums of testset
dim(testset)
#Train Model
tree_train <- rpart(class~.,data=trainset,control = rpart.control(cp = .0005))
#plot tree model
rpart.plot(tree_train)
#print splitting rules of tree
print(tree_train)
#predict with testset
pred <- predict(tree_train,testset,type = "class")
#view Cross-Validation dataset
table(pred,testset$class)
#view Confusion Matrix and Statistics
confusionMatrix(pred, testset$class)