带闪亮的朴素贝叶斯

问题描述

我正在用朴素贝叶斯做一个电子邮件分类器,但我有错误,我不知道为什么。代码如下:

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",choices = list("ENE","FEB","MAR","DIC")),selectInput(inputId = "dia",label = "Dia",choices =list("LUnes","MARTES","MIERCOLES","JUEVES","VIERnes","SABADO","DOMINGO")),selectInput(inputId = "hora",label = "Hora",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",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","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)

这是结果:enter image description here 我认为错误在于输入条目,因为当我更改条目时,我获得了相同的结果。我的错误在哪里,如何解决

使用 dput 我得到这个:

structure(list(Tipo = c("promocionesItems","promocionesServicios","personalFamiliayamigos","promocionesItems","novedades","promocionesItems"),Dominio = c("selecta-vision.com","hm.com"),Mes = c("FEB","DIC","ENE","MAR"),Dia = c("VIERnes","DOMINGO","LUnes","MIERCOLES"
),Hora = structure(c(15L,16L,5L,9L,3L,15L,7L,13L,9L),.Label = c("1","22"
),class = "factor"),Usuario = c("EMPRESA","EMPRESA","PARTIculaR","EMPRESA")),row.names = c(NA,10L),class = "data.frame")

这是我的数据库中的一张图片enter image description here

谢谢大家!

解决方法

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
  })