R language to build Decision Tree for wine dataset

description: build Decision Tree for wine dataset in R language

read data From File

1
2
3
4
wine<-read.table("E:\\wine.data",
sep=",",
col.names = c("Type","Alcohol","Malic","Ash","Alcalinity","Magnesium", "Phenols","Flavanoids", "Nonflavanoids", "Proanthcyanins", "Color", "Hue", "Dilution", "Proline"))
wine$Type=factor(wine$Type)

Data mining using J48 Decision Tree classifier

1
2
3
4
5
6
wine.scale <- cbind(wine[1], scale(wine[-1])) 
data.size <- nrow(wine.scale)
set.seed(1111)
samp <- c(sample(1:data.size, data.size * 0.7))
data.tr <- wine.scale[samp, ]
data.test <- wine.scale[-samp, ]

J48 Model

1
2
3
4
wine_dt=J48(Type~.,data=data.tr)
wine_dt
prediction_dt = predict(wine_dt, data.test[,2:14])
prediction_dt

rpart get tree rules

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
library(rpart)

res = rpart(Type ~., data = data.tr)
res

terminal_nodes = rownames(res$frame)[res$frame$var =="<leaf>"]
path.rpart(res ,nodes=terminal_nodes)
rules = path.rpart(res ,nodes=terminal_nodes)
listed_rules = unlist(rules)
sapply(rules,"[",-1)

frm <- res$frame
names <- row.names(frm)

prediction_dt = predict(res, data.test[,2:14], type="matrix")
prediction_dt

party

1
2
3
4
5
6
7
8
update.packages()
Sys.setenv(JAVA_HOME = "D:\\Program Files\\Java\\jdk1.8.0_151")
install.packages("zoo")
install.packages("party")
install.packages("sandwich")
library(party)
mparty = party(Type ~., data = data.tr)
mparty

party ctree()

1
2
3
4
p_ctree <- ctree(Type ~., data=data.tr)
ctree_class <- predict(p_ctree,data.test[,2:14], type="response")
test_ctree <- predict(p_ctree,data.test[,2:14], type="node")
test_ctree