【问题标题】:Naive Bayes with Shiny带有闪亮的朴素贝叶斯
【发布时间】:2021-07-20 17:02:43
【问题描述】:

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

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)

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

通过 dput 我得到这个:

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

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

谢谢大家!

【问题讨论】:

  • 代替df &lt;- data.frame(d$Usuario == input$usu.....) 试试df &lt;- subset(d, Usuario == input$usu, Mes == input$mes, Dia == input$dia, Hora == input$hora, Dominio == input$dom)。这行得通吗?
  • 如果我这样做,它会显示一个消息,即 itmes Dia、Hora 和 Dominio 没有创建 :(
  • 您可以将数据添加到您的帖子中吗?编辑您的帖子以包含dput(head(d, 10))
  • 完成。我也从我的数据库中添加了一张图片。

标签: r input shiny predict naivebayes


【解决方案1】:

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

【讨论】:

  • Nothing :( 这是结果:factor(0) 6 级别:novedades personalFamiliayamigos personalProfesional promocionesItems ... social
  • @MaríadelMarSorianoJerez 我认为您的数据中不存在UsuarioMesDia 等的组合。这样做是子集您的数据框,其中Usuarioinput$usu 相同,Mesinput$mes 相同,依此类推。我的建议是只从df &lt;- subset(d, Usuario == input$usu) 开始,看看它是否有效,然后慢慢继续添加过滤器。
猜你喜欢
  • 2018-02-24
  • 2021-07-15
  • 2012-02-21
  • 2011-12-28
  • 2017-02-18
  • 2013-09-09
  • 2015-01-03
  • 2017-02-09
  • 2018-02-14
相关资源
最近更新 更多