Saturday, August 8, 2020

Basics of R- Decison Tree using rpart

 library(rpart)

library(rpart.plot)


data1<-read.csv("file:///C:/Users/LENOVO/Desktop/Missing/decison  tree mba.csv", stringsAsFactors = TRUE)


dtm1<-rpart(Specialization~Gender+Previous_Degree,data=data1,

            minsplit=100, minbucket=10, maxdepth=4, method = "class")

dtm1

rpart.plot(dtm1, type = 0, cex = 0.5)

#-------------


dtm1<-rpart(Specialization~Gender+Previous_Degree+Percentage_in_10_Class+

              Percentage_in_12_Class+Percentage_in_Under_Graduate,

            data=data1,

            minsplit=100, minbucket=10, maxdepth=4)

dtm1

rpart.plot(dtm1, type = 0, cex = 0.5)


#-------------------------------

dtm1<-rpart(Specialization~Percentage_in_10_Class+

              Percentage_in_12_Class+Percentage_in_Under_Graduate,

            data=data1,

            minsplit=100, minbucket=10, maxdepth=4, parms = list(split = 'gini'))

dtm1

rpart.plot(dtm1, type = 0, cex = 0.5)


#CP= complexity parameter, α be some number between 0 and ∞ which measures the ’cost’ of adding another

# variable to the model


# pruning

dtm1prone<-prune(dtm1, cp=0.01)

rpart.plot(dtm1prone, type = 0, cex = 0.5)

library()

printcp(dtm1)


str(data1)

dtm1<-rpart(Age_in_years~Percentage_in_10_Class+

              Percentage_in_12_Class+Percentage_in_Under_Graduate,

            data=data1, method = "anova" )

rpart.plot(dtm1, type = 0, cex = 0.5)


predict1<-predict(dtm1, type = "class")

confusionMatrix(predict1, data1$Specialization)


No comments:

Post a Comment