简体   繁体   中英

Naive Bayes with Shiny

I´m doing an email classifier with Naive Bayes, but I have and error and I don´t know why. Here is the code:

library(shiny)
library(shinydashboard)
library(e1071)
library(naivebayes)

#Cargar datos
d <- read.csv("C:/Users/jerez/OneDrive/Escritorio/UAL/TFG/BD.csv", sep = ";", header = TRUE)
d$Hora <- as.factor(d$Hora)
str(d)
mod <- naiveBayes(d$Tipo ~ d$Usuario+d$Mes+d$Dia+d$Hora+d$Dominio, data = d)
mod
dput(head(d, 10))
# Define UI 
ui <- fluidPage(
  
  # Application title
  titlePanel("Clasificador Naive Bayes"),
  
  # Sidebar 
  sidebarPanel(
    h4("Atributos de los mensajes"),
    selectInput(inputId = "usu", label = "Recibido de", multiple = FALSE, choices = list("EMPRESA","PARTICULAR")),
    selectInput(inputId = "mes", label = "Mes de creacion", multiple = FALSE, choices = list("ENE","FEB","MAR","DIC")),
    selectInput(inputId = "dia", label = "Dia", multiple = FALSE, choices =list("LUNES","MARTES","MIERCOLES","JUEVES","VIERNES","SABADO","DOMINGO")),
    selectInput(inputId = "hora", label = "Hora", multiple = FALSE, choices =list("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22")),
    selectInput(inputId = "dom", label = "Dominio", multiple = FALSE, choices =list("accounts.google.com","agricolaeltoro.com","email.hm.com","email.hunkemoller.com","gmail.com","google.com","hm.com","hotmail.com","mail-game.net","mail.instagram.com","news.etam.com","nisabelt.com","selecta-vision.com","stackoverflow.email","stackoverflow.email","wordpress.com"))
  ),
  
   mainPanel(
    p("El mensaje sera clasificado como:"),
    verbatimTextOutput("prediccion"),
    
  )
  
)

# Define server 
server <- function(input, output) {
        
  output$prediccion <- renderPrint({
    df <- data.frame(d$Usuario == input$usu, d$Mes == input$mes, d$Dia == input$dia, d$Hora == input$hora, d$Dominio == input$dom)
    prob <- predict(mod, df)
    levels(prob)[prob]
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

An this is the result: enter image description here I think the mistake is in the inputs entries because when I change the entries I obtain the same result. Where is my error and how can to solve it?

With dput I obtain this:

structure(list(Tipo = c("promocionesItems", "promocionesServicios", 
"personalFamiliayamigos", "promocionesItems", "promocionesItems", 
"promocionesItems", "novedades", "novedades", "promocionesItems", 
"promocionesItems"), Dominio = c("selecta-vision.com", "news.etam.com", 
"hotmail.com", "email.hm.com", "email.hm.com", "mail-game.net", 
"selecta-vision.com", "stackoverflow.email", "mail-game.net", 
"hm.com"), Mes = c("FEB", "FEB", "DIC", "MAR", "MAR", "ENE", 
"DIC", "DIC", "ENE", "MAR"), Dia = c("VIERNES", "VIERNES", "DOMINGO", 
"DOMINGO", "DOMINGO", "MARTES", "JUEVES", "LUNES", "LUNES", "MIERCOLES"
), Hora = structure(c(15L, 16L, 5L, 9L, 3L, 15L, 16L, 7L, 13L, 
9L), .Label = c("1", "2", "4", "6", "7", "8", "9", "10", "11", 
"12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22"
), class = "factor"), Usuario = c("EMPRESA", "EMPRESA", "PARTICULAR", 
"EMPRESA", "EMPRESA", "EMPRESA", "EMPRESA", "EMPRESA", "EMPRESA", 
"EMPRESA")), row.names = c(NA, 10L), class = "data.frame")

And here is a picture from my data base: enter image description here

Thank you to all of you! <3

Change this in server :

  output$prediccion <- renderPrint({
    df <- subset(d, Usuario == input$usu & Mes == input$mes & Dia == input$dia & 
                    Hora == input$hora &  Dominio == input$dom)
    prob <- predict(mod, df)
    prob
  })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM