#decision Tree
data2 = read.csv("C:/Users/sagar/Downloads/yeast.csv")
View(data2)
#Creating Train and Test Set
library(caTools)
set.seed(123)
split=sample.split(Y=data2$erl,SplitRatio=2/3)
train_set=subset(x=data2,split==TRUE)
test_set=subset(x=data2,split==FALSE)
dim(train_set)
## [1] 989 9
dim(test_set)
## [1] 495 9
#Building a Model
library(rpart)
fit=rpart(formula=erl~.,data=train_set,method="class")
summary(fit)
## Call:
## rpart(formula = erl ~ ., data = train_set, method = "class")
## n= 989
##
## CP nsplit rel error xerror xstd
## 1 0.03703704 0 1.0000000 1 0.3318132
## 2 0.01000000 3 0.8888889 1 0.3318132
##
## Variable importance
## vac name gvh mit nuc mcg alm
## 42 17 13 12 6 6 5
##
## Node number 1: 989 observations, complexity param=0.03703704
## predicted class=0.5 expected loss=0.009100101 P(node) =1
## class counts: 980 9
## probabilities: 0.991 0.009
## left son=2 (927 obs) right son=3 (62 obs)
## Primary splits:
## mcg < 0.765 to the left, improve=0.4062646, (0 missing)
## gvh < 0.795 to the left, improve=0.3011789, (0 missing)
## name splits as RRLLLLLLLL, improve=0.1734075, (0 missing)
## vac < 0.525 to the left, improve=0.1572512, (0 missing)
## mit < 0.365 to the left, improve=0.1354665, (0 missing)
## Surrogate splits:
## name splits as LRRRLLLLLL, agree=0.957, adj=0.306, (0 split)
## gvh < 0.765 to the left, agree=0.956, adj=0.290, (0 split)
##
## Node number 2: 927 observations
## predicted class=0.5 expected loss=0.005393743 P(node) =0.9373104
## class counts: 922 5
## probabilities: 0.995 0.005
##
## Node number 3: 62 observations, complexity param=0.03703704
## predicted class=0.5 expected loss=0.06451613 P(node) =0.06268959
## class counts: 58 4
## probabilities: 0.935 0.065
## left son=6 (42 obs) right son=7 (20 obs)
## Primary splits:
## name splits as RRRLL-LL-L, improve=1.0838710, (0 missing)
## vac < 0.525 to the left, improve=0.5505376, (0 missing)
## alm < 0.495 to the left, improve=0.5293255, (0 missing)
## mit < 0.365 to the left, improve=0.4314900, (0 missing)
## mcg < 0.865 to the right, improve=0.1647220, (0 missing)
## Surrogate splits:
## alm < 0.445 to the left, agree=0.79, adj=0.35, (0 split)
## vac < 0.485 to the right, agree=0.71, adj=0.10, (0 split)
##
## Node number 6: 42 observations
## predicted class=0.5 expected loss=0 P(node) =0.04246714
## class counts: 42 0
## probabilities: 1.000 0.000
##
## Node number 7: 20 observations, complexity param=0.03703704
## predicted class=0.5 expected loss=0.2 P(node) =0.02022245
## class counts: 16 4
## probabilities: 0.800 0.200
## left son=14 (13 obs) right son=15 (7 obs)
## Primary splits:
## vac < 0.525 to the left, improve=2.9714290, (0 missing)
## mit < 0.355 to the left, improve=1.1252750, (0 missing)
## alm < 0.495 to the left, improve=0.4000000, (0 missing)
## mcg < 0.815 to the left, improve=0.1582418, (0 missing)
## gvh < 0.745 to the right, improve=0.1582418, (0 missing)
## Surrogate splits:
## gvh < 0.645 to the right, agree=0.75, adj=0.286, (0 split)
## mit < 0.22 to the right, agree=0.75, adj=0.286, (0 split)
## nuc < 0.235 to the left, agree=0.70, adj=0.143, (0 split)
##
## Node number 14: 13 observations
## predicted class=0.5 expected loss=0 P(node) =0.01314459
## class counts: 13 0
## probabilities: 1.000 0.000
##
## Node number 15: 7 observations
## predicted class=1 expected loss=0.4285714 P(node) =0.007077856
## class counts: 3 4
## probabilities: 0.429 0.571
str(data)
## function (..., list = character(), package = NULL, lib.loc = NULL, verbose = getOption("verbose"),
## envir = .GlobalEnv, overwrite = TRUE)
library(rpart.plot)
rpart.plot(fit)

predict_unseen=predict(object=fit,newdata=test_set,type="class")
predict_unseen
## 2 4 5 8 11 13 16 20 21 22 24 25 26 31 32 33
## 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
## 34 37 50 53 58 59 61 65 67 68 69 71 73 82 84 87
## 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
## 88 89 94 97 104 106 107 111 114 115 118 126 130 132 133 134
## 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
## 136 137 138 139 145 150 151 167 173 174 175 179 181 183 189 190
## 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
## 193 195 198 202 206 214 216 219 221 223 224 225 230 231 239 241
## 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
## 243 247 249 250 251 257 261 262 263 264 265 272 276 277 278 282
## 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 1 0.5 0.5 0.5 0.5 0.5 0.5 0.5
## 291 295 296 297 298 301 302 304 306 316 319 320 323 324 327 330
## 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
## 333 336 337 340 343 350 355 359 360 363 366 369 376 379 380 383
## 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
## 385 387 389 394 397 398 404 405 407 411 414 416 421 429 434 435
## 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
## 438 447 449 450 451 454 460 461 462 465 472 474 476 478 479 484
## 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
## 486 488 490 493 495 496 499 502 505 506 516 520 522 525 527 533
## 0.5 0.5 0.5 0.5 0.5 1 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
## 534 536 538 539 541 543 545 548 550 552 553 555 556 561 564 569
## 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
## 570 575 577 579 582 583 588 589 590 592 595 596 600 603 606 609
## 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
## 613 615 621 623 624 625 626 628 629 630 635 638 639 641 644 645
## 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
## 646 649 650 654 658 659 660 663 664 665 669 670 672 675 685 689
## 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
## 690 691 692 693 708 710 713 722 723 728 731 732 733 735 736 738
## 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
## 740 742 744 747 748 751 757 759 760 769 780 782 783 784 785 791
## 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
## 793 796 801 802 806 817 820 821 823 825 826 832 835 836 841 844
## 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
## 846 847 849 850 853 854 859 864 865 866 869 872 885 887 888 890
## 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
## 891 894 895 897 898 904 905 907 908 912 913 915 917 919 920 921
## 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
## 925 928 933 936 937 938 942 943 945 947 950 951 953 965 968 977
## 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
## 981 983 985 989 994 995 997 998 1008 1011 1016 1017 1019 1028 1034 1037
## 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
## 1053 1054 1059 1062 1063 1064 1065 1066 1070 1073 1074 1078 1080 1083 1085 1086
## 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
## 1087 1088 1099 1103 1105 1109 1110 1111 1115 1116 1118 1119 1122 1123 1124 1126
## 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
## 1132 1137 1141 1146 1147 1149 1150 1155 1158 1165 1167 1168 1172 1181 1184 1188
## 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
## 1191 1197 1198 1199 1202 1203 1208 1211 1213 1215 1216 1218 1220 1223 1226 1230
## 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
## 1232 1234 1238 1239 1243 1252 1253 1259 1264 1265 1270 1273 1279 1282 1284 1286
## 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
## 1289 1291 1296 1298 1301 1305 1308 1311 1312 1313 1318 1319 1323 1327 1329 1331
## 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
## 1335 1337 1339 1351 1352 1353 1360 1372 1377 1389 1390 1392 1397 1399 1402 1405
## 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
## 1406 1410 1411 1420 1422 1424 1425 1426 1430 1433 1435 1438 1442 1443 1445 1447
## 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
## 1448 1452 1453 1459 1460 1466 1467 1470 1472 1473 1475 1476 1477 1480 1481
## 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
## Levels: 0.5 1
# Confusion Matrix
cmat=table(test_set$pox,predict_unseen)
cmat
## predict_unseen
## 0.5 1
## 0 485 2
## 0.5 3 0
## 0.83 5 0
#Accuracy
sum(diag(cmat))/sum(cmat)
## [1] 0.979798
plot(fit)
text(fit)
