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 |
#------------------ # Data Preparation #------------------ #Read datasets #Download the data from http://datascience.esy.es/wp-content/uploads/2018/03/CreditData-1.zip train <- read.csv("Credit_train.csv") test <- read.csv("Credit_test.csv") #Rows and Cols dim(train) dim(test) #Columns name colnames(train) colnames(test) #Show head(train) head(test) #---------- # Bayesian #---------- library(caret) library(e1071) library(AUC) #train model.Bayes <- naiveBayes(DEFAULT~., data = train) model.Bayes #test pc <-NULL pc <- predict(model.Bayes, test, type = "class") summary(pc) xtab <- table(pc, test$DEFAULT) caret::confusionMatrix(xtab, positive = "Y") #lift chart pb <-NULL pb <- predict(model.Bayes, test, type = "raw") pb <- as.data.frame(pb) pred.Bayes <- data.frame(test$DEFAULT,pb$Y) colnames(pred.Bayes) <- c("target","score") lift.Bayes <- lift(target ~ score, data = pred.Bayes, cuts=10, class="Y") xyplot(lift.Bayes, main="Bayesian Classifier - Lift Chart", type=c("l","g"), lwd=2 , scales=list(x=list(alternating=FALSE,tick.number = 10) ,y=list(alternating=FALSE,tick.number = 10))) #roc chart labels <- as.factor(ifelse(pred.Bayes$target=="Y", 1, 0)) predictions <- pred.Bayes$score auc(roc(predictions, labels), min = 0, max = 1) plot(roc(predictions, labels), min=0, max=1, type="l", main="Bayesian Classifier - ROC Chart") |
Ejercicio en R Bayesian Puro
Mapa -> Predecir el Futuro (inferir) -> Modelizacion -> Clasificacion -> Bayesiano Puro -> Ejemplo en R