Aim/Objective

To Develop and Create a suitable machine learning model for the dataset assigned. Compare the results with atleast 3 machine learning algorihtm FOR classification.

Literature Survey of nursery dataset

Nursery Database was derived from a hierarchical decision model originally developed to rank applications for nursery schools. It was used during several years in 1980’s when there was excessive enrollment to these schools in Ljubljana, Slovenia, and the rejected applications frequently needed an objective explanation. The final decision depended on three subproblems: occupation of parents and child’s nursery, family structure and financial standing, and social and health picture of the family. The model was developed within expert system shell for decision making DEX (M. Bohanec, V. Rajkovic: Expert system for decision making. Sistemica 1(1), pp. 145-157, 1990.).

The hierarchical model ranks nursery-school applications according to the following concept structure:

NURSERY Evaluation of applications for nursery schools . EMPLOY Employment of parents and child’s nursery . . parents Parents’ occupation . . has_nurs Child’s nursery . STRUCT_FINAN Family structure and financial standings . . STRUCTURE Family structure . . . form Form of the family . . . children Number of children . . housing Housing conditions . . finance Financial standing of the family . SOC_HEALTH Social and health picture of the family . . social Social conditions . . health Health conditions

Input attributes are printed in lowercase. Besides the target concept (NURSERY) the model includes four intermediate concepts: EMPLOY, STRUCT_FINAN, STRUCTURE, SOC_HEALTH. Every concept is in the original model related to its lower level descendants by a set of examples (for these examples sets see [Web Link]).

The Nursery Database contains examples with the structural information removed, i.e., directly relates NURSERY to the eight input attributes: parents, has_nurs, form, children, housing, finance, social, health.

Because of known underlying concept structure, this database may be particularly useful for testing constructive induction and structure discovery methods.

Attribute Information:

parents: usual, pretentious, great_pret has_nurs: proper, less_proper, improper, critical, very_crit form: complete, completed, incomplete, foster children: 1, 2, 3, more housing: convenient, less_conv, critical finance: convenient, inconv social: non-prob, slightly_prob, problematic health: recommended, priority, not_recom

code:

Data manipulation:

getwd()
## [1] "C:/Desktop/5-DATA SCIENCE PROGRAMMING/r studio files"
setwd("C:/Desktop/5-DATA SCIENCE PROGRAMMING")
data=read.csv("nursery.csv")
summary(data)
##    parents            has_nurs             form             children        
##  Length:12960       Length:12960       Length:12960       Length:12960      
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##    housing            finance             social             health         
##  Length:12960       Length:12960       Length:12960       Length:12960      
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##  final.evaluation  
##  Length:12960      
##  Class :character  
##  Mode  :character
str(data)
## 'data.frame':    12960 obs. of  9 variables:
##  $ parents         : chr  "usual" "usual" "usual" "usual" ...
##  $ has_nurs        : chr  "proper" "proper" "proper" "proper" ...
##  $ form            : chr  "complete" "complete" "complete" "complete" ...
##  $ children        : chr  "1" "1" "1" "1" ...
##  $ housing         : chr  "convenient" "convenient" "convenient" "convenient" ...
##  $ finance         : chr  "convenient" "convenient" "convenient" "convenient" ...
##  $ social          : chr  "nonprob" "nonprob" "nonprob" "slightly_prob" ...
##  $ health          : chr  "recommended" "priority" "not_recom" "recommended" ...
##  $ final.evaluation: chr  "recommend" "priority" "not_recom" "recommend" ...
data=data[sample(1:nrow(data)), ]

DATA CLEANING:

converting character data into numbers as factor data

data$parents=factor(data$parents,levels = c("usual","pretentious","great_pret"),labels=c(1,2,3))
data$has_nurs=factor(data$has_nurs,levels = c("proper","less_proper","improper","critical","very_crit"),labels=c(4,3,2,1,0))
data$form=factor(data$form,levels = c("complete","completed","incomplete","foster"),labels=c(4,3,2,1))
data$children<-as.numeric(sub('more',4,data$children))
data$housing=factor(data$housing, levels = c("convenient","less_conv","critical"),labels=c(2,1,0))
data$finance=factor(data$finance,levels = c("convenient","inconv"),labels=c(1,0))
data$social=factor(data$social,levels = c("nonprob","slightly_prob","problematic"),labels=c(3,2,1))
data$health=factor(data$health,levels = c("recommended","priority","not_recom"),labels=c(3,2,1))
data$final.evaluation=factor(data$final.evaluation,levels = c("spec_prior","very_recom","recommend","priority","not_recom"),labels=c(4,3,2,1,0))

converting factor data into numerical except “final.evaluation” as it is the dependent attribute

data$parents=as.numeric(data$parents)
data$has_nurs=as.numeric(data$has_nurs)
data$form=as.numeric(data$form)
data$finance=as.numeric(data$finance)
data$housing=as.numeric(data$housing)
data$social=as.numeric(data$social)
data$health=as.numeric(data$health)

Structure of data

str(data)
## 'data.frame':    12960 obs. of  9 variables:
##  $ parents         : num  3 3 2 2 2 1 2 1 2 1 ...
##  $ has_nurs        : num  5 2 1 3 1 4 2 2 5 3 ...
##  $ form            : num  2 4 4 2 3 3 1 3 2 3 ...
##  $ children        : num  4 1 1 2 4 2 2 2 1 2 ...
##  $ housing         : num  2 2 3 1 1 1 2 1 2 1 ...
##  $ finance         : num  1 1 2 2 1 2 2 2 2 1 ...
##  $ social          : num  3 1 3 1 3 2 2 3 3 3 ...
##  $ health          : num  3 2 2 3 3 1 2 3 3 3 ...
##  $ final.evaluation: Factor w/ 5 levels "4","3","2","1",..: 5 1 4 5 5 4 4 5 5 5 ...

Summary of data

summary(data)
##     parents     has_nurs      form         children       housing 
##  Min.   :1   Min.   :1   Min.   :1.00   Min.   :1.00   Min.   :1  
##  1st Qu.:1   1st Qu.:2   1st Qu.:1.75   1st Qu.:1.75   1st Qu.:1  
##  Median :2   Median :3   Median :2.50   Median :2.50   Median :2  
##  Mean   :2   Mean   :3   Mean   :2.50   Mean   :2.50   Mean   :2  
##  3rd Qu.:3   3rd Qu.:4   3rd Qu.:3.25   3rd Qu.:3.25   3rd Qu.:3  
##  Max.   :3   Max.   :5   Max.   :4.00   Max.   :4.00   Max.   :3  
##     finance        social      health  final.evaluation
##  Min.   :1.0   Min.   :1   Min.   :1   4:4044          
##  1st Qu.:1.0   1st Qu.:1   1st Qu.:1   3: 328          
##  Median :1.5   Median :2   Median :2   2:   2          
##  Mean   :1.5   Mean   :2   Mean   :2   1:4266          
##  3rd Qu.:2.0   3rd Qu.:3   3rd Qu.:3   0:4320          
##  Max.   :2.0   Max.   :3   Max.   :3

Plot of the whole data

plot(data)

Creation of Training and Testing data

library(caTools)
## Warning: package 'caTools' was built under R version 4.1.3
set.seed(500)
summary(data$final.evaluation)
##    4    3    2    1    0 
## 4044  328    2 4266 4320
split= sample.split(data$final.evaluation,SplitRatio=0.8)

training=subset(data,split==T)
#View(training)
dim(training)
## [1] 10368     9
summary(training)
##     parents         has_nurs      form          children        housing 
##  Min.   :1.000   Min.   :1   Min.   :1.000   Min.   :1.000   Min.   :1  
##  1st Qu.:1.000   1st Qu.:2   1st Qu.:1.000   1st Qu.:2.000   1st Qu.:1  
##  Median :2.000   Median :3   Median :3.000   Median :2.000   Median :2  
##  Mean   :1.999   Mean   :3   Mean   :2.495   Mean   :2.502   Mean   :2  
##  3rd Qu.:3.000   3rd Qu.:4   3rd Qu.:3.000   3rd Qu.:4.000   3rd Qu.:3  
##  Max.   :3.000   Max.   :5   Max.   :4.000   Max.   :4.000   Max.   :3  
##     finance          social          health      final.evaluation
##  Min.   :1.000   Min.   :1.000   Min.   :1.000   4:3235          
##  1st Qu.:1.000   1st Qu.:1.000   1st Qu.:1.000   3: 262          
##  Median :1.000   Median :2.000   Median :2.000   2:   2          
##  Mean   :1.498   Mean   :2.009   Mean   :2.001   1:3413          
##  3rd Qu.:2.000   3rd Qu.:3.000   3rd Qu.:3.000   0:3456          
##  Max.   :2.000   Max.   :3.000   Max.   :3.000
test=subset(data,split==F)
#View(test)
dim(test)
## [1] 2592    9
summary(test)
##     parents         has_nurs          form          children    
##  Min.   :1.000   Min.   :1.000   Min.   :1.000   Min.   :1.000  
##  1st Qu.:1.000   1st Qu.:2.000   1st Qu.:2.000   1st Qu.:1.000  
##  Median :2.000   Median :3.000   Median :2.000   Median :3.000  
##  Mean   :2.005   Mean   :3.002   Mean   :2.518   Mean   :2.494  
##  3rd Qu.:3.000   3rd Qu.:4.000   3rd Qu.:4.000   3rd Qu.:3.000  
##  Max.   :3.000   Max.   :5.000   Max.   :4.000   Max.   :4.000  
##     housing         finance          social          health     
##  Min.   :1.000   Min.   :1.000   Min.   :1.000   Min.   :1.000  
##  1st Qu.:1.000   1st Qu.:1.000   1st Qu.:1.000   1st Qu.:1.000  
##  Median :2.000   Median :2.000   Median :2.000   Median :2.000  
##  Mean   :2.002   Mean   :1.508   Mean   :1.963   Mean   :1.998  
##  3rd Qu.:3.000   3rd Qu.:2.000   3rd Qu.:3.000   3rd Qu.:3.000  
##  Max.   :3.000   Max.   :2.000   Max.   :3.000   Max.   :3.000  
##  final.evaluation
##  4:809           
##  3: 66           
##  2:  0           
##  1:853           
##  0:864           
## 

Applying Decision tree algorithm to your dataset (Nursery dataset)

We consider the use of the Decision Tree, modelled using the rpart function. To get a large tree we make the complexity paramter really small (cp). We see in the output, all the trees that are considered in the model, giving the complexity parameter, number of splits, re-substitution error rate, cross-validated error rate and the associated standard error.

#decision tree

library(rpart)
## Warning: package 'rpart' was built under R version 4.1.3
library(rpart.plot)
## Warning: package 'rpart.plot' was built under R version 4.1.3
tree=rpart(formula=final.evaluation~.,training,method='class')

Plotting tree

plot(tree)

rpart.plot(tree,type=4,extra = 101)

Model prediction

#Predicting the model
pred=predict(object=tree,test,type='class')
pred
## 12501  4923  8028  8531 11535  2500 11151  3429  6449  7443  8365  3945  2505 
##     0     0     0     4     0     1     0     0     1     0     4     0     0 
##  8792  9165  7194  4174 10257  8913  1792 11899  4496 12358  1252  2865  7579 
##     4     0     0     4     0     0     1     4     1     4     1     0     4 
##   303  7804  8954  5882  5375  5315  8578  3435 12816  1950  4456  7776  9019 
##     0     4     4     1     1     1     4     0     0     0     1     0     1 
##  3628 10273  3518  9528  2537 12493  7564   469  1698  6994  3380  7500  4986 
##     4     1     4     0     1     4     4     1     0     4     4     0     0 
##  3182 12329 11861  5784  8476  9696  8099 12360  2332  9228 10264  3045  4267 
##     4     4     4     0     4     0     4     0     1     0     1     0     4 
##  8083 10209 12882  5709  5600   315  7497  5735  8389  4895  5080  3511 11761 
##     4     0     0     0     1     0     0     1     4     1     1     4     4 
## 12825 10938  5245   632  1420  4287  6726  9658  2216  9976  1556  8498  1505 
##     0     0     1     1     1     0     0     1     1     1     1     4     1 
##  1459  5130  8105  7340 12457  7739 12673 11989  3203  6701  3149  9098  8681 
##     1     0     4     4     4     4     4     4     4     1     4     4     4 
##  1111 12332   363  9447  7255 10213 10181  4390 12588  8832 11473  4831  2414 
##     1     4     0     0     4     1     4     1     0     0     4     1     1 
##  8292 11115 11241  3063  8301  5557   957 12530 11472 10655  2614 10333  9214 
##     0     0     0     0     0     1     0     4     0     4     4     1     1 
##  3062  9140  9020  2289  9114  8939 12649  2347  5816  1753 12281  1090 12017 
##     4     4     4     0     0     4     4     1     1     1     4     1     4 
##  7172  2576   988  8605  4315  2533  6415  1226  3861 11334  2046  4335  5430 
##     4     1     1     4     4     1     1     1     0     0     0     0     0 
##  1113  9754   701  5166  2021  5072 11346  1844  2371  8188  4324  9703  8963 
##     0     1     1     0     1     1     0     1     1     4     1     1     4 
##  1513  5641   809 12753  6825  5534  4988   739  1599   405 12452  9366 12561 
##     1     1     1     0     0     1     1     1     0     0     4     0     0 
##  3248  4434  4732 10835   319  8731  9677  4264 11338 12654  8078  2271 12758 
##     4     0     1     4     1     1     4     4     4     0     4     0     4 
##  3934  6751  3931 10232  9350 11750  7565 11704  7748 12888  7770 12497 12919 
##     4     1     4     4     4     4     4     4     4     0     0     4     4 
## 11011  6919  1407  4182  2066  7290  4452  8638  8185  7447  5196  6788   361 
##     4     4     0     0     1     0     0     4     4     4     0     1     1 
##  9430  1645  9974 10929  6347   109 11299  9416  2415  7291  9857  4285  9384 
##     1     1     4     0     1     1     4     4     0     4     4     4     0 
## 10748  6496  4513  3381  4647  3339 12295  6380  8967  4862  6629  8317  9556 
##     4     1     1     0     0     0     4     1     0     1     1     4     1 
## 11997  3633  6340   320  1956  6503  8546   539   516 12085  4576 12087  4718 
##     0     0     1     1     0     1     4     1     0     4     1     0     1 
##  2756  5756  7658  9406  6253  2760  1530  5051  5548 11715 10442  5414  2806 
##     4     1     4     1     1     0     0     1     1     0     4     1     4 
## 11953 12153  3169  2832  3624  3794 10094 10335  7709 10987  5470  2103  7366 
##     4     0     4     0     0     4     4     0     4     4     1     0     4 
##   819  9531  3340  4815  8895  1558   124 12038  5459  3281  5820  7149 12584 
##     0     0     4     0     0     1     1     4     1     4     0     0     4 
##  6587 11332  6135  1654  6620   582  7665  1638 11760  1765  7681  2916  7382 
##     1     4     0     1     1     0     0     0     0     1     4     0     4 
##  4243 10758  5107 10185  6125  4619 11866  7202  6310  9232 12656  4618  1273 
##     4     0     1     0     1     1     4     4     1     1     4     1     1 
##  1479  5876  3447 10947  8723  8835  3757  4724  5417  7746 12511 12448 11526 
##     0     1     0     0     4     0     4     1     1     0     4     4     0 
##  6914  8539 12838  7368  6174  6974  2640   794  3267 11596 10147  2164  8704 
##     4     4     4     0     0     4     0     1     0     4     1     1     1 
## 10450  3489  6235  9465  8101  2825 12265  8076  9268  5329  3865  4439  1595 
##     4     0     1     0     4     4     4     0     1     1     4     1     1 
## 12600  5625  6477  5736 11532  3509  1430 10586  5070  3968 12602  7245  5866 
##     0     0     0     0     0     4     1     4     0     4     4     0     1 
##  6640  6641  4849   236  7585  4935  8277  7064 10846  1778 11963  4779  7251 
##     1     1     1     1     4     0     0     4     4     1     4     0     0 
## 11586  5806  2044  4841  2656 10029  9340  1010  7731  7988 11912  4850 10453 
##     0     1     1     1     4     0     1     1     0     4     4     1     4 
##  6883  4303 12751  8296  8261 12754  3830  3674  6139  2075 10979  8162  4712 
##     1     4     4     4     4     4     4     4     1     1     4     4     1 
## 12509  9489  9686 12327  8038  9682  2900 12597  2323  1614  5726  1503  4801 
##     4     0     4     0     4     1     4     0     1     0     1     0     1 
##  3880  2476  9176 12181  1712  1893 10925   997 11375  9388  2995  2469 11168 
##     4     1     4     4     1     0     4     1     4     1     4     0     4 
##  4534  1685  6689 11344  5999 11936  1900  1041  9038  3852  4323 10964  7113 
##     1     1     1     4     1     4     1     0     4     0     0     4     0 
##   695  3924  8160  1144 10464  6686 12113  8435   989  8031  1325 12639  4629 
##     1     0     0     1     0     1     4     4     1     0     1     0     0 
##  2382 10710  2368  7154 12050 11606  2131  7597  6213  5850  2421 12207  4893 
##     0     0     1     4     4     4     1     4     0     0     0     0     0 
##  7945  6841  5264   133 10475  6465  3016  2950 12571  1776  5827  6515   274 
##     4     1     1     1     4     0     4     4     4     0     1     1     1 
##  9499 12856  5254  2524  4659  2679  8503 10437 12035  4367  3468 10445  4419 
##     1     4     1     1     0     0     4     0     4     1     0     4     0 
##  1892 10135  8062   699 12523  8401  7285  4720  2095 11297  9254  3394  3770 
##     1     1     4     0     4     4     4     1     1     4     4     4     4 
## 11147   560  6987   705  6520 12652   174   267  8537   629   454  4567  9933 
##     4     1     0     0     1     4     0     0     4     1     1     1     0 
##  7606  3686  3750  4946   171  6929  7583  7857  8964  8603  8920  9247  5730 
##     4     4     0     1     0     4     4     0     0     4     1     1     0 
## 12309  9872  1616 11498  8906  5341  5284  9259 11035  5444  6112  7029   891 
##     0     4     1     4     4     1     1     1     4     1     1     0     0 
##  9828  7923  8868  2889 11056  4620  7427  5575  4746  4021  4971 10219  8399 
##     0     0     0     0     4     0     4     1     0     4     0     1     4 
##  5553  2060  1920 10482  3102  7264 12480  3065  5334  8285 11935    89   252 
##     0     1     0     0     0     4     0     4     0     4     4     1     0 
## 11522  7693 10656  2484  1566 10154  6714 10931 11355  3272  7649  4944  9103 
##     4     4     0     0     0     4     0     4     0     4     4     0     1 
##   540  1066 12864  5887 12675   543  1732  7717  2028 12807  9278   275  3704 
##     0     1     0     1     0     0     1     4     0     0     4     1     4 
##  7510  8465  8654  5464  4082  4951  4578  1572 10269  8427  7123  2683  3204 
##     4     4     4     1     4     1     0     0     0     0     4     4     0 
##  2755   513  1576   852  3020  7851 10831 11390  5897   451  6167  1192  5818 
##     4     0     1     0     4     0     4     4     1     1     1     1     1 
##  8671 11569  9227 11951   186  2670 11023 11880  8370   146  5082  3647  6837 
##     1     4     4     4     0     0     4     0     0     1     0     4     0 
## 10807  9024  9400 10303  9054    56  7115  3224 12883 10975 12312  6164 11966 
##     4     0     1     1     0     1     4     4     4     4     0     1     4 
##  8570 11555  4186  4601  4071  4421  4206  9132  6366  7392  5793 11820  8903 
##     4     4     4     1     0     1     0     0     0     0     0     0     4 
##  3683  2144  7346 12729  8885 12859   584  5348  7575  5953   266   992 12873 
##     4     1     4     0     4     4     1     1     0     1     1     1     0 
##  5646  7525  5187  4129 12109  2062  5117  5327  3201 12863  2929 11788 12797 
##     0     4     0     4     4     1     1     1     0     4     4     4     4 
##  2343 10127  2467  2662   651  7261 10974 12102  8295  8968  3369 10643  9376 
##     0     4     1     4     0     4     0     0     0     1     0     4     1 
##  9776   381  5792  9252 10421    16 12426  9243  1828  1532 10332  2978 12735 
##     4     0     1     0     4     1     0     0     1     1     0     4     0 
##  1204  6026  4024  7999  6321  4867 12359  5622  7249 12678  5012  9158 12333 
##     1     1     4     4     0     1     4     0     4     0     1     4     0 
##  5787  2304  6317 12393 11325 10627  1448  8353 10076  1387  8225 12500  5378 
##     0     0     1     0     0     4     1     4     4     1     4     4     1 
##  8689  8507  4110  8879  5830 10965  2316  1021  2519  7530  5433  2546  8966 
##     1     4     0     4     1     0     0     1     1     0     0     1     4 
##  4093  6828   983  1537  7237 10231 10707 11785 12078 10849  7543  7214 11374 
##     4     0     1     1     4     1     0     4     0     4     4     4     4 
##   726  2882  2943 12784  1621  2746  2780  4443   550 10479  2192  8625  5836 
##     0     4     0     4     1     4     4     0     1     0     1     0     1 
##  5991 11218  5028  8889  1520  3420  3047   314  4885 11915  5252  4804   449 
##     0     4     0     0     1     0     4     1     1     4     1     1     1 
## 10279  1102  6501  7223 10310 12461  5718   895 12846  8549  6460  2715 11089 
##     1     1     0     4     4     4     0     1     0     4     1     0     4 
##  6291  1814  5731  8896  4169  2158  3166  5978   195 11060  9290  6921  2407 
##     0     1     1     1     4     1     4     1     0     4     4     0     1 
##  5003 12320   893 12884  3567  9803 12388  1655 10747  9790   874  4466   790 
##     1     4     1     4     0     4     4     1     4     1     1     1     1 
##  3973  5289  4525  2703 12942 10034  8184  1574  2233  9606 12475   329  8597 
##     4     0     1     0     0     4     0     1     1     0     4     1     4 
##  6738   463  1341  5209  4480 10080  4548 11028  7910 12143  4216 10341 11995 
##     0     1     0     1     1     0     0     0     4     4     4     0     4 
##  9589  7615  4693  5606  7248 12518  8373  3494  3680 10671  1565  3554  7675 
##     1     4     1     1     0     4     0     4     4     0     1     4     4 
##  1581  7596  9816  2822 11587  5753   339  4138  2881 11475  6761  6653 12385 
##     0     0     0     4     4     1     0     4     4     0     1     1     4 
##  2443  5105  6429  8251  7774  9820  1508  3581  2424 11968  4688 11084  2295 
##     1     1     0     4     4     1     1     4     0     4     1     4     0 
##  9093  7351  5996  9650  4241 12853  1868 12599  4696  1770  4343  4911  4848 
##     0     4     1     4     4     4     1     4     1     0     1     0     0 
##  6004  3018  3031   839  1818  7357  8229  6348  7451  2770 11228  4334  4533 
##     1     0     4     1     0     4     0     0     4     4     4     1     0 
##  6076 10282 11291  5345 10506  8808  3376  3457  8626 12814  6815  4656 10012 
##     1     1     4     1     0     0     4     4     4     4     1     0     1 
##  7433 12526   872  2922   634 11163  1557  1145  6931  2019  7468  5366  3544 
##     4     4     1     0     1     0     0     1     4     0     4     1     4 
##  2939  4073 12089 11798  2624 10373 11975 10417  6005  8818 11781  5436  8714 
##     4     4     4     4     4     4     4     4     1     1     0     0     4 
##  6962  1907  9936  4141  4485  5549  5525  4604   672  4892   587 10220  6669 
##     4     1     0     4     0     1     1     1     0     1     1     4     0 
## 11213  1626 12270  6846  6468  1327  8378   772  7507 11898  5573  2728  5613 
##     4     0     0     0     0     1     4     1     4     0     1     4     0 
##  6684 11387  6188  4104  5916  4048  9427  5225  3911 10733  2400  8821  6328 
##     0     4     1     0     0     4     1     1     4     4     0     1     1 
##  1208   216  1795  6407 11938  1501 11771  5598  9040 10744  5388  6803  6950 
##     1     0     1     1     4     1     4     0     1     4     0     1     4 
##  7400   439  3949 10668 12765  5515  2025   583  4291  3025 12552  5742  1910 
##     4     1     4     0     0     1     0     1     4     4     0     0     1 
##  5409   853  9407  2964 12560  2858  6495  2085  2930  6206  9424    32  7081 
##     0     1     4     0     4     4     0     0     4     1     1     1     4 
##   588  5335  2058  2141  8395  4743  5652  2622  8763 11808  5567  6814 11850 
##     0     1     0     1     4     0     0     0     0     0     1     1     0 
##  5113  4031  5693 10424 12815  6285  8014  8340  9945   117  5677  1216 10547 
##     1     4     1     4     4     0     4     0     0     0     1     1     4 
##  8619 11364  3583  5268  6311  6510  1100  8961  2116  7591 10906   528 10734 
##     0     0     4     0     1     0     1     0     1     4     4     0     0 
##  3286  5132 11230 11716 12760   687  5424  4924 10917  6033  6762   345  5283 
##     4     1     4     4     4     0     0     1     0     0     0     0     0 
##  5085   800  2625  3439  8096 11379  5108   423  6942 12086  4086  1664  7490 
##     0     1     0     4     4     0     1     0     0     4     0     1     4 
## 12795  3698  8059 11494  9670  3954 10687 10419  6630 10868  6481  6589  6845 
##     0     4     4     4     1     0     4     0     0     4     1     1     1 
##  1214 10811  3631  7753  6259  2436  2585  8128 12559  1642 11556  3315  8343 
##     1     4     4     4     1     0     1     4     4     1     0     0     0 
##  7189 11677  2206  4495  8615   734   737 12890  8925  5616  9601  5167  1619 
##     4     4     1     1     4     1     1     4     0     0     1     1     1 
##  4677  5629  1887 11646  7584 11693 10706  2508  4475 11487  3702  7899  3910 
##     0     1     0     0     0     4     4     0     1     0     0     0     4 
##  8917  3032  5896 10088  7986 12472 11197  2558   340 12507  6926  4984  8175 
##     1     4     1     4     0     4     4     1     1     0     4     1     0 
##  7252  6826 12294   378  5144 11698  9334 11471 12744  1563  7023  9506  1084 
##     4     1     0     0     1     4     1     4     0     0     0     4     1 
##  6085 10720 11517  4354  9090  3574 12623  1575  6051  2267 12470  8753  1833 
##     1     4     0     1     0     4     4     0     0     1     4     4     0 
## 11757  1384  5721 12845  1412 10716  8564  2088  3358    36 11165  1186  5845 
##     0     1     0     4     1     0     4     0     4     0     4     1     1 
##  8439  6904  3560    96  2008  8470  7914 12011  9913  7940  9791  7766  5914 
##     0     1     4     0     1     4     0     4     1     4     4     4     1 
## 10233 11264  8417    93  5204  4519  9386 12094  9141 11492  2562   309  5426 
##     0     4     4     0     1     1     4     4     0     4     0     0     1 
##   708    42  6634  1290 11534  2695  3993  6375 11080  7027  8659  1658  3088 
##     0     0     1     0     4     4     0     0     4     4     1     1     4 
## 10055  7593 10256  4805 12868  6851  6637 12138  7886   831  7571  5520  4203 
##     4     0     4     1     4     1     1     0     4     0     4     0     0 
##  1600  4001   152 12256 10941    28 10752 11314  6374  4754  7994  3070 10494 
##     1     4     1     4     0     1     0     4     1     1     4     4     0 
##  2373  9537  9675  6907  1859  3202  5127 11780    88   435  7557   323  7761 
##     0     0     0     1     1     4     0     4     1     0     0     1     0 
##   756 10103   976  5759 12214  9585  8894  4401 10911   762  6489  5835  7849 
##     0     4     1     1     4     0     4     0     0     0     0     0     4 
##  5206 10077  9577  2974  1653  8541   242 11053  1117   230  7762  5255  4706 
##     1     0     1     4     0     0     1     4     1     1     4     1     1 
## 12874  6010  3553  8860  7305  1499  5188  2696  4806    92 10423  8194  3367 
##     4     1     4     1     0     1     1     4     0     1     4     4     4 
##  3425 11559  8244  5579 11521  6060  9071  9008  5964 10724  7901 12111  7047 
##     4     0     0     1     4     0     4     4     0     4     4     0     0 
##  6408  3838  9664  6458  7322  9590  7799  8337  8933  3875  9825 12648  3727 
##     0     4     1     1     4     4     4     0     4     4     0     0     4 
## 11246 10097 11479  6582  5490  5161  4783  2809  8909  6453  6226  6794   370 
##     4     4     4     0     0     1     1     4     4     0     1     1     1 
##  1522   823   531  2480  9389  9065 11499  7090  8858  1825  5893  8609 10284 
##     1     1     0     1     4     4     0     4     4     1     1     4     0 
##  3386  8706  2912  4499  3669  2426  8886   525  3246  3251  3236  3951  4544 
##     4     0     4     1     0     1     0     0     0     4     4     0     1 
##  9028 11268  6355 10540  4707  8496  6319  7211  5947  9539  5214  1149  9815 
##     1     0     1     4     0     0     1     4     1     4     0     0     4 
## 11602  2324  3876  1766  8633  1504  9197   878  2724  2529 10871 11298  7409 
##     4     1     0     1     4     1     4     1     0     0     4     0     4 
##  2987  3828  6665  6146  1696  4934  8164  2810 12361    34  1832  8878  7124 
##     4     0     1     1     1     1     4     4     4     1     1     1     4 
##  6229  5223  8077  7163  3490  8891  3436 11566  8103  8456  7060 11795  4246 
##     1     0     4     4     4     4     4     4     0     4     4     4     4 
##  6955  6096  3540  2083  1257  1485   901  9210 10537  2163  1957  8298  8584 
##     4     0     0     1     0     0     1     0     4     0     1     0     4 
##  6691  1299  1971  5350  7229 12412  9012  4587  6879 10793  6294  5812  2652 
##     1     0     0     1     4     4     0     0     0     4     0     1     0 
##  5237  2549 10601  8063  7707  3007 10653  4753 12832 11877  6109  6122  6517 
##     1     1     4     4     0     4     0     1     4     0     1     1     1 
##  5686  6946  3798   440  1881  7997  8653  2412  8423 10538  5313  3385 10997 
##     1     4     0     1     0     4     1     0     4     4     0     4     4 
##  4348  3808  5472  3807   917 12935  7590  2893  4079 10591  5261  3694  7840 
##     1     4     0     0     1     4     0     4     4     4     1     4     4 
##  9457  4833  2456  9865  7306  4398   422  9855  2941  5920  3755  9470 10789 
##     1     0     1     1     4     0     1     0     4     1     4     4     4 
##  5630  1423  1919 11723   180  6890  2319  8308  4721  9586 11923  3568  3784 
##     1     1     1     4     0     1     0     4     1     1     4     4     4 
## 12449  7630  7280 10174 12396 10524  1639  6968 11967  3324  4101  4346 10739 
##     4     4     4     1     0     0     1     4     0     0     0     1     4 
## 10909  2860  1686  6428  2006  4902  4210  9742  2230  7274  8273   649  4462 
##     4     4     0     1     1     0     4     1     1     4     4     1     1 
##  3758  4638  1334  4284 12524  1514  1641 10527  9326  2965  7365   547   322 
##     4     0     1     0     4     1     0     0     4     4     0     1     1 
##  5045  9119  4757  9241  8533  5645  3465 10673  1127 11848 10812 12247 10145 
##     1     4     1     1     4     1     0     4     1     4     0     4     4 
##  9193  4261 10951  4402  7455 12357  2102  8851  1388  2582 10814 11579 12264 
##     1     4     4     1     0     0     1     1     1     1     4     4     0 
## 11635 11112  7470  9884 10473  5531 11851 10742  8927 10353 12666   197  6432 
##     4     0     0     4     0     1     4     4     4     0     0     1     0 
## 10631  3365  3273 10281  6573  1899  4342 11280  7767  9353  2106  4919 12491 
##     4     4     0     0     0     0     1     0     0     4     0     1     4 
##  6177  3255 11242  1453  6734  7240   775   630   112    11  4112  4365  4683 
##     0     0     4     1     1     4     1     0     1     1     4     0     0 
##  7135  2247  6821  2775  5597  9690 10046  7981  2839   960   356  3895  2509 
##     4     0     1     0     1     0     4     4     4     0     1     4     1 
## 10957 12512  2850  9554  4778  1138   384  9032  6419  8203  7348  6405  4991 
##     4     4     0     4     1     1     0     4     1     4     4     0     1 
##  9691  2719 11284  8952   431  4362 12150  6519   928   141  7283  7069  7698 
##     1     4     4     0     1     0     0     0     1     0     4     4     0 
## 10505  2653  7483  3543 11941 11410 11829  9755  2534  3584  1624 11638  2336 
##     4     4     4     0     4     4     0     4     1     4     1     4     1 
##  8741  4256 11423 12900  3305  7949 12604  9707  1829  1968  3666  1914  4737 
##     4     4     4     0     4     4     4     4     1     0     0     0     0 
##  7573  8114  9723  8080 10048 12913  8151  3552  3414  4517  6079  5298  9131 
##     4     4     0     4     1     4     0     0     0     1     1     0     4 
##   212  3655  4609  3661  4400  6610   591  5259  4254  2587  5665  1890 11614 
##     1     4     1     4     1     1     0     0     0     1     1     0     4 
##  1865  9286  7257  7951  4322  5607 10579 11003  8716 10654  2890 10943  3053 
##     1     1     0     4     1     0     4     4     1     4     4     4     4 
##   688  8557  9947  3299  4997  7659 11965 11523 11620     6 12520 10379  7599 
##     1     4     4     4     1     0     4     0     4     0     4     4     0 
##  2747  5277 12841  2826  1089  6491  2035  2969  5927 11567  6763 12400  5543 
##     4     0     4     0     0     1     1     4     1     4     1     4     1 
##  5870  2017  8288 11520 12179  8825  7098 12069  4357 10762  6180  2486  1760 
##     1     1     4     0     4     4     0     0     1     4     0     1     1 
##  7982  8007  8468  2279  8655  2853  3905  7126  6087  3505  7878  6765  4230 
##     4     0     4     1     0     0     4     4     0     4     0     0     0 
## 11087  9362  1636  5314  6284  8259  3384  1215 12933 11013 11000  3621 10803 
##     4     4     1     1     1     0     0     0     0     0     4     0     0 
##  1800  6795  9575  2487  1831  9836  5741   697  7574  9950  5691  6362 11083 
##     0     0     4     0     1     4     1     1     4     4     0     1     4 
## 10850 10460 11339  8255  6031 12324   787  9160  8863  7491  4488  7416 10133 
##     4     4     4     4     1     0     1     1     1     0     0     0     4 
##  7344  3444  4663 11478   406  2422  2168 11476  7129  3392  3105   776 12522 
##     0     0     1     0     1     1     1     4     4     4     0     1     0 
##   694 10682  1527  9758  3777  6258  2282  7685  6207  6148  7512  1788  8018 
##     1     4     0     4     0     0     1     4     0     1     0     0     4 
##  5708 10521  2067  7930  5068  7970  5425  4641 12389    18  4471  4742 12224 
##     1     0     0     4     1     4     1     0     4     0     1     1     4 
##  8011  8841  2968  6782  1329 12211  6099   848 10436  8955  3940 12662  9735 
##     4     0     4     1     0     4     0     1     4     0     4     4     0 
##  9978  5097  3586  1032  8232  1093 12537 10243  8838  9201  7385  5159   753 
##     0     0     4     0     0     1     0     1     0     0     4     1     0 
##   342  1673  3100  3745  1239  3914 11357  6506  6040  1122 12586  6769  3424 
##     0     1     4     4     0     4     4     1     1     0     4     1     4 
##  9199 11993  8142  2814  1594  6959  6580  8592  7097  3487  2331 12001 10121 
##     1     4     0     0     1     4     1     0     4     4     0     4     4 
##   818  8693 10819  4239  9745  6636  7783  3124  1748  4070  6053 10692  6050 
##     1     4     4     0     1     0     4     4     1     4     1     0     1 
##  7673   982  4819  8672  2465  2209 10031   984  5752  8932  7130  9622 12835 
##     4     1     1     4     1     1     4     0     1     1     4     1     4 
## 12829  7836 11918   270  6903 11078  5241  5683  8566  4207 10344  6812 12145 
##     4     0     4     0     0     4     0     1     4     4     0     1     4 
##  2375 12625  7822  6412  7612  3634  1179   102  8180  9294  1958 10167 10697 
##     1     4     4     1     4     4     0     0     4     0     1     0     4 
## 12253  2895   335 11480  5168  2516  1688  2580  4699  2449  8442 12771  9942 
##     4     0     1     4     1     1     1     0     1     1     0     0     0 
##  8213  2660  5267  5518  2759  2352  6181 11418  1834  7343 12540  9620   615 
##     4     4     1     1     4     0     1     0     1     4     0     4     0 
## 10383  3837  9984  8791  7350  3141  2442  2430 12093  8871 12723 10329 10039 
##     0     0     0     1     0     0     0     0     0     0     0     0     1 
##  4417  1366 11464  9977 10884  8013  4704 11138 11408  9382  1944  3341  4336 
##     1     1     4     4     0     0     0     4     4     1     0     4     1 
##  9439 10989  8052  9250  3765  9352  5109  4522  7254  8684 10904  1395 12826 
##     1     0     0     1     0     1     0     1     0     4     4     0     4 
##  3475  6393  4446 12420  4515  3231  4013  1432  4074 12076 10970 10728  9295 
##     4     0     0     0     0     0     4     1     0     4     4     0     1 
##  5581 11737  3134  2704  9768  5306  4232  6036  8772  5565  5890  3418 10848 
##     1     4     4     4     0     1     4     0     0     0     1     4     0 
## 10309  6722  8166  6729 11950  1283   523  6934 10578  4854  1727  7527  7120 
##     1     1     0     0     4     1     1     4     0     0     1     0     4 
##  6448  9874   480  8040  9411  2727  4317  1517 10155 10385 12217  3250  8404 
##     1     1     0     0     0     0     0     1     0     4     4     4     4 
##  8898 10583 12536  6537  4990 11437  8347 11018  6451   765 10598  3979   902 
##     0     4     4     0     1     4     4     4     1     0     4     4     1 
##  6394 12095  7484  7944  9075   535   894  1064 11897 11159  4957  8790  8069 
##     1     4     4     0     0     1     0     1     4     4     1     0     4 
## 10736  8287 12039  1224  3093   138  6208  8551 12837 12422 11872  7230  8722 
##     4     4     0     0     0     0     1     4     0     4     4     0     1 
##  1402  8033  1171  1981  6799 11699  8627  7345  7983  8460 12687  6864  7955 
##     1     4     1     1     1     4     4     4     0     0     0     0     4 
##  6626  2791 11286  5481  7224  5164  9557  2953  2845  9793   169  9567  6091 
##     1     4     0     0     0     1     4     4     4     1     1     0     1 
##  4809 10008 11875  6158  9481  4030  6209  9700  1684  8467 12433 10057 12592 
##     0     0     4     1     1     4     1     1     1     4     4     1     4 
##  6094  3325  6632  5822  6017  1650   206 10221  8109  4007  7158  1998  7939 
##     1     4     1     1     1     0     1     0     0     4     0     0     4 
##  7618 11136  1390 12240  1839  4821 10235  3423 11746  9096  2545  4493 12743 
##     4     0     1     0     0     0     4     0     4     0     1     1     4 
## 11273  5310  6834  3735  9473  3723 10348  2087  7363 11055  2239  1038  4320 
##     4     0     0     0     4     0     1     1     4     0     1     0     0 
##   240 11959  8810  6731   683  9580  6292 10879  2909  1251  6790  6877   833 
##     0     4     4     1     1     1     1     4     4     0     1     1     1 
## 11258 10280 10787   291  4989  3106 11589 11819 12562 11730    12  5046  1481 
##     4     4     4     0     0     4     0     4     4     0     0     0     1 
## 10081  1007  9064  6244  3620  7869  6742 10895 10897  7980  8004 11085  9634 
##     1     1     1     1     4     0     1     4     4     0     0     0     1 
##  4118  5157 10622   403 11608  1896  2733  6370  7127  8656  1535  3810 10934 
##     4     0     4     1     4     0     0     1     4     1     1     0     4 
##  8174  7292  6312 12187  1421  3328  9200  4967  6388 10874  8490  8274 11749 
##     4     4     0     4     1     4     4     1     1     4     0     0     4 
##   411   599  5542  8499  4925  8697  4741  8613  7838  4504  6558  1553  2372 
##     0     1     1     0     1     0     1     0     4     1     0     1     1 
##  7892 10295  1237  9219   425 10944  4201  1365  1372  4304 12062 11068  5146 
##     4     4     1     0     1     0     4     0     1     4     4     4     1 
## 10468 12077 11684   244  4891 10531  3375  3689  3722 10225  3077  2693 12637 
##     4     4     4     1     1     4     0     4     4     1     4     4     4 
##  3946 11810  2891  1238  2255   720  2615 11065    84  2665  8892  7655  7723 
##     4     4     4     1     1     0     4     4     0     4     0     4     4 
##  2778  8824  4351 10613  4370  1931 12820  8743 11343  5701  1982  1607 12020 
##     0     1     1     4     1     1     4     1     0     1     1     1     4 
##  5737 11806  8039  5275  8049 11687  5257  9263 12241 11397 12043  4092  3858 
##     1     4     4     1     0     4     1     4     4     0     4     0     0 
##  6801 10438  5355    87  1123 11358 12022 10502  2932 11855  3896   231  9371 
##     0     4     0     0     1     0     4     4     4     4     4     0     4 
##  1040  2764  9548  1790 12593 12048 11827  5993 11396  9081   926 10403  4066 
##     1     4     4     1     4     0     4     1     4     0     1     4     4 
##  8737 12834 10288 12585   945 10158  1936  5008  9007  9496  1898  4644  1902 
##     1     0     1     0     0     0     1     1     1     1     1     0     0 
##   946   472  5799  9967  9545  6250  2702 12908  5060    45 11655 12450 11095 
##     1     1     0     1     4     1     4     4     1     0     0     0     4 
##  8754  3360  5962   144  3469  5278  6923  9255  4045 10764   492  4122  5404 
##     0     0     1     0     4     1     4     0     4     0     0     0     1 
## 10770  2335  6001 11450 11876  3881  5292  5413  7933 10896 12875  9154  1417 
##     0     1     1     4     4     4     0     1     4     0     4     1     1 
##  6313  8953 10223  4181  8192 12680  2586  1218  8969 10136  7643  9419  8147 
##     1     1     4     4     4     4     0     0     4     4     4     4     4 
##  3763 11142 10160  6059  3682  7039 12160 10368  7963   325  1531  5690  1904 
##     4     0     4     1     4     4     4     0     4     1     1     1     1 
## 10261  1118  5699  3799  3115  1098  1304  5591  6446 10132  5855 11678  5136 
##     1     1     1     4     4     0     1     1     1     1     1     4     0 
##  6529  5680  9789  1246   142  9851  8923  6678  8199  5773  7213  2196  2179 
##     1     1     0     1     1     4     1     0     0     1     4     0     1 
##    73   365  4455  7660  4154  8843 11337  5484  4914  8646 12197  2868  1217 
##     1     1     0     4     4     4     0     0     0     0     4     0     1 
##  9698  8095   602 12346  2034  6805 10327  8861  5958 11550 10470   552  3225 
##     4     4     1     4     0     1     1     4     0     0     0     0     0 
##  3269  7438 12695  9560  2805  1671 11156 11033  1549  7015  5503  9063  7535 
##     4     4     4     4     0     0     4     4     1     4     1     0     4 
##  4382  8657  7877  8453 11020  3037 10334  1045   179 11613  9072  5066 12303 
##     1     4     4     4     4     4     4     1     1     0     0     1     0 
## 12538  6916  1053 11455  7937 12683  6296  8492  7435 11782  5675  6789  2150 
##     4     4     0     4     4     4     1     4     4     4     1     0     1 
## 11773  6431 11558  5309  4126  5886  6886  1306  1689  3284 10701 10189  3688 
##     4     1     4     1     4     0     1     1     0     4     0     1     4 
##  3417  8735  1133  6236  6238  1110 12710  7193 11960  3237  2241  7459  1195 
##     0     4     1     1     1     0     4     4     4     0     0     4     1 
##  4214  3815 12129   468  6075 
##     4     4     0     0     0 
## Levels: 4 3 2 1 0

Confusion matrix: A confusion matrix in R is a table that will categorize the predictions against the actual values. It includes two dimensions, among them one will indicate the predicted values and another one will represent the actual values.

#confusion matrix
cm=table(test$final.evaluation,pred)
cm
##    pred
##       4   3   2   1   0
##   4 734   0   0  75   0
##   3   0   0   0  66   0
##   2   0   0   0   0   0
##   1 204   0   0 649   0
##   0   0   0   0   0 864

Accuracy of the data model according to Decision tree algorithm

#Accuracy
dtacc=sum(diag(cm)/sum(cm))
dtacc
## [1] 0.8668981

Applying NAIVEBAYES algorithm to your dataset (Nursery dataset)

e1071 is a package for R programming that provides functions for statistic and probabilistic algorithms like a fuzzy classifier, naive Bayes classifier, bagged clustering, short-time Fourier transform, support vector machine, etc.. When it comes to SVM, there are many packages available in R to implement it

library(e1071) #Loading e1071 library for the usage of naiveBayes classification
## Warning: package 'e1071' was built under R version 4.1.3
classifier = naiveBayes(x= training[-9],y = training$final.evaluation )
classifier
## 
## Naive Bayes Classifier for Discrete Predictors
## 
## Call:
## naiveBayes.default(x = training[-9], y = training$final.evaluation)
## 
## A-priori probabilities:
## training$final.evaluation
##            4            3            2            1            0 
## 0.3120177469 0.0252700617 0.0001929012 0.3291859568 0.3333333333 
## 
## Conditional probabilities:
##                          parents
## training$final.evaluation     [,1]      [,2]
##                         4 2.307264 0.7706304
##                         3 1.412214 0.4931753
##                         2 1.000000 0.0000000
##                         1 1.742748 0.7694590
##                         0 2.007523 0.8172887
## 
##                          has_nurs
## training$final.evaluation     [,1]      [,2]
##                         4 3.876971 1.1665608
##                         3 1.809160 0.7642842
##                         2 1.000000 0.0000000
##                         1 2.251978 1.1541885
##                         0 3.008102 1.4100911
## 
##                          form
## training$final.evaluation     [,1]     [,2]
##                         4 2.590417 1.117730
##                         3 2.106870 1.030210
##                         2 1.000000 0.000000
##                         1 2.429827 1.109804
##                         0 2.501736 1.121296
## 
##                          children
## training$final.evaluation     [,1]     [,2]
##                         4 2.657496 1.087755
##                         3 1.961832 1.042430
##                         2 1.000000 0.000000
##                         1 2.399648 1.127936
##                         0 2.497975 1.118970
## 
##                          housing
## training$final.evaluation     [,1]      [,2]
##                         4 2.140031 0.7972522
##                         3 1.423664 0.6126798
##                         2 1.000000 0.0000000
##                         1 1.905948 0.8140057
##                         0 2.004630 0.8166016
## 
##                          finance
## training$final.evaluation     [,1]      [,2]
##                         4 1.535085 0.4988446
##                         3 1.320611 0.4676046
##                         2 1.000000 0.0000000
##                         1 1.471433 0.4992564
##                         0 1.502894 0.5000640
## 
##                          social
## training$final.evaluation     [,1]      [,2]
##                         4 2.109737 0.8292808
##                         3 1.500000 0.5009569
##                         2 1.500000 0.7071068
##                         1 1.949604 0.7984694
##                         0 2.013310 0.8193372
## 
##                          health
## training$final.evaluation     [,1]      [,2]
##                         4 1.612056 0.4873571
##                         3 1.000000 0.0000000
##                         2 1.000000 0.0000000
##                         1 1.434222 0.4957270
##                         0 3.000000 0.0000000
summary(classifier) #Summary of the model
##           Length Class  Mode     
## apriori   5      table  numeric  
## tables    8      -none- list     
## levels    5      -none- character
## isnumeric 8      -none- logical  
## call      3      -none- call

Prediction

#Predict the test_set result
y_pred= predict(object = classifier, newdata = test) #Prediting the model with help of testset
y_pred
##    [1] 0 0 0 4 0 3 0 0 4 0 4 0 0 1 0 0 4 0 0 3 4 1 4 3 0 3 0 4 1 1 1 1 4 0 0 0 3
##   [38] 0 2 2 3 2 0 4 4 2 2 0 3 4 0 0 4 4 4 0 4 0 4 0 3 0 3 0 2 4 0 0 0 1 0 0 1 4
##   [75] 1 3 2 4 0 0 3 1 3 0 0 1 1 1 1 4 1 3 0 4 4 4 4 4 4 4 1 4 1 2 3 4 0 0 3 1 1
##  [112] 2 0 0 4 3 1 0 0 0 0 0 3 0 4 0 4 2 3 1 4 1 1 0 0 1 4 1 1 2 4 3 4 4 1 2 4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 1
## [2517] 2 0 0 0 0 0 4 4 4 1 0 0 4 4 2 4 3 0 4 2 2 4 4 2 2 4 3 2 0 0 1 0 4 2 0 2 4
## [2554] 4 1 4 4 4 1 0 1 4 1 4 1 2 0 3 3 0 4 0 1 2 0 1 1 4 3 0 4 4 4 0 0 4 3 4 4 0
## [2591] 0 0
## Levels: 4 3 2 1 0

Confusion matrix: A confusion matrix in R is a table that will categorize the predictions against the actual values. It includes two dimensions, among them one will indicate the predicted values and another one will represent the actual values.

#confusion matrix
cm=table(test[,9],y_pred)
cm
##    y_pred
##       4   3   2   1   0
##   4 670  13  43  83   0
##   3   0  44  22   0   0
##   2   0   0   0   0   0
##   1  65 252 186 350   0
##   0   0   0  16   0 848

Accuracy of the data model according to Naive base algorithm

#Accuracy
NBacc=sum(sum(diag(cm))/sum(cm))
NBacc
## [1] 0.7376543

Applying KNN algorithm to your dataset (Nursery dataset)

KNN is a Supervised Learning algorithm that uses labeled input data set to predict the output of the data points. It is one of the most simple Machine learning algorithms and it can be easily implemented for a varied set of problems. It is mainly based on feature similarity Loading packages

library(caTools)
library()

Feature Scaling: Feature scaling is a method used to normalize the range of independent variables or features of data. In data processing, it is also known as data normalization and is generally performed during the data preprocessing step

#feature scaling
train_scale = scale(training[1:8])
test_scale = scale(test[1:8])

Fitting a knn to the train_set

#fitting a knn to the train_set
library(class)
pred = knn(train_scale,test_scale,training$final.evaluation, k = 3)
pred
##    [1] 0 0 0 4 0 1 0 0 4 0 4 0 0 4 0 0 4 0 0 1 1 1 1 1 0 4 0 1 4 1 1 1 4 0 0 0 1
##   [38] 0 1 1 4 1 0 1 4 1 1 0 1 4 0 0 4 4 4 0 4 0 4 0 1 0 1 0 1 4 0 0 0 1 0 0 1 4
##   [75] 1 3 1 4 0 0 1 1 1 0 0 1 1 1 1 4 1 3 0 4 4 4 4 4 1 4 1 4 1 4 3 4 0 0 4 1 4
##  [112] 1 0 0 4 1 1 0 0 0 0 0 1 0 4 0 4 1 1 4 4 4 1 0 0 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0 0 0 4 0 1 4 4 1 4 0 1 1
## [2517] 1 0 0 0 0 0 4 4 4 1 0 0 4 4 1 1 1 0 4 1 1 4 4 1 1 4 1 1 0 0 1 0 1 1 0 4 4
## [2554] 4 1 4 4 4 1 0 1 1 1 4 1 1 0 1 3 0 4 0 1 4 0 4 1 4 1 0 4 4 4 0 0 4 1 4 4 0
## [2591] 0 0
## Levels: 4 3 2 1 0

Making the Confusion Matrix: A confusion matrix in R is a table that will categorize the predictions against the actual values. It includes two dimensions, among them one will indicate the predicted values and another one will represent the actual values.

# Making the Confusion Matrix
cm = table(test[,9], pred)
cm
##    pred
##       4   3   2   1   0
##   4 769   0   0  39   1
##   3   0  54   0  12   0
##   2   0   0   0   0   0
##   1  26   1   0 826   0
##   0   0   0   0   0 864

Accuracy of the data model according to KNN algorithm

KNNacc=sum(sum(diag(cm))/sum(cm))
KNNacc
## [1] 0.9695216

Including Plots

#Graphs

BARPLOT:

count=table(data$final.evaluation)
count
## 
##    1    2    3    4    5 
## 4044  328    2 4266 4320
barplot(count)
legend(1,1000,legend=c("20MID0086"))

DENSITY PLOT

#density plot:
plot(density(data$final.evaluation))
legend(1,0.3,legend=c("20MID0086"))

HISTOGRAM

#HISTOGRAM
hist(data$final.evaluation,breaks = 3)
legend(1,1000,legend=c("20MID0086"))

LOLLIPOP

#LOLLIPOP
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.1.3
ggplot(data,aes(x=final.evaluation,y=housing))+geom_segment(aes(x=final.evaluation,xend=final.evaluation,y=2,yend=housing))+geom_point()

SCATTER

#scatter
plot(x=data$final.evaluation,y=data$housing,main = "Scatter Plot")
legend(1,2,legend=c("20MID0086"))

HEATMAP

#HEATMAP
dataset=data[c(4,5,8)]
map<-as.matrix(dataset[])
heatmap(map)

Conclusion Result

For Set.seed(500) These are the accuracies Accuracy in Decision tree algorithm:

dtacc
## [1] 0.8668981

Accuracy in NaiveBayes algorithm:

NBacc
## [1] 0.7376543

Accuracy in KNN algorithm:

KNNacc
## [1] 0.9695216

Among these three algorithms: KNN got highest accuracy so The model created using KNN algorithm is more efficient with accuracy

KNNacc
## [1] 0.9695216