警告:as.data.frame.default中的错误:无法将“ naiveBayes”类强制转换为data.frame

问题描述

我有一个脚本可以运行朴素贝叶斯情绪分析。我成功运行了脚本。但是,当我在rshiny上调用脚本时。我的脚本有一个错误“无法将类天真贝叶斯强制转换为数据框”。有人可以帮我吗?

    corpus.clean <- twitclean %>%
      tm_map(content_transformer(tolower)) %>% 
      tm_map(removePunctuation) %>%
      tm_map(removeNumbers) %>%
      tm_map(removeWords,stopwords::stopwords("id",source = "stopwords-iso")) %>%
      tm_map(stripwhitespace)
    
    dtm <- DocumentTermMatrix(corpus.clean)
    {
      m <- as.matrix(dtm)
      v <- sort(rowSums(m),decreasing=TRUE)
      d <- data.frame(word = names(v),freq=v)
    }
    dataframe<-data.frame(text=unlist(sapply(corpus.clean,`[`)),stringsAsFactors=FALSE)
    corpus.txt<-dataframe
    
    inspect(dtm[40:50,10:15])
    
    df.train <- df[1:7500,]
    df.test <- df[7501:10000,]
    
    dtm.train <- dtm[1:7500,]
    dtm.test <- dtm[7501:10000,]
    
    corpus.train <- corpus[1:7500]
    corpus.test <- corpus[7501:10000]
    
    dim(dtm.train)
    
    fivefreq <- findFreqTerms(dtm.train,10)
    length((fivefreq))
    fivefreq
    ## [1] 12144
    
    # Use only 5 most frequent words (fivefreq) to build the DTM
    
    dtm.train.nb <- DocumentTermMatrix(corpus.train,control=list(dictionary = fivefreq))
    
    dim(dtm.train.nb)
    ## [1]  1500 12144
    
    dtm.test.nb <- DocumentTermMatrix(corpus.test,control=list(dictionary = fivefreq))
    
    dim(dtm.train.nb)
    
    # Function to convert the word frequencies to yes (presence) and no (absence) labels
    convert_count <- function(x) {
      y <- ifelse(x > 0,1,0)
      y <- factor(y,levels=c(0,1),labels=c("No","Yes"))
      y
    }
    
    # Apply the convert_count function to get final training and testing DTMs
    trainNB <- apply(dtm.train.nb,2,convert_count)
    testNB <- apply(dtm.test.nb,convert_count)
    trainNBm <- as.data.frame(as.matrix(trainNB))
    # Train the classifier
    system.time( classifier <-naiveBayes(trainNB,df.train$klasifikasi,laplace = 1) )
    
    # Use the NB classifier we built to make predictions on the test set.
    system.time(pred <- predict(classifier,newdata=testNB) )
    # Use the NB classifier we built to make predictions on the test set.
system.time(pred <- predict(classifier,newdata=testNB) )

# Create a truth table by tabulating the predicted class labels with the actual class labels 
table("Predictions"= pred,"Actual" = df.test$klasifikasi )

# Prepare the confusion matrix
conf.mat <- confusionMatrix(pred,df.test$klasifikasi)

conf.mat

我想我在这里出错了,但是直到现在我都没有找到解决方案。

system.time( classifier <-naiveBayes(trainNB,laplace = 1) )

这是我的rshiny ui.R

library(shiny)

# Define UI for application that draws a histogram
shinyUI(fluidPage(

    # Application title
    titlePanel("Analysis Sentimen"),# Sidebar with a slider input for number of bins
    sidebarLayout(
        sidebarPanel(
        ),mainPanel(
            tabsetPanel(type="tab",tabPanel("Data File",tableOutput("negpos"),tableOutput("table")),tabPanel("Preprocessing Data",tableOutput("corpustxt")),tabPanel("Confusion Matrix",tableOutput("confmat"),tableOutput("confacc")),tabPanel("Hasil Prediksi",tableOutput("testpred")),tabPanel("Plot",plotOutput("plotpred")))
    )
)))

server.R

library(shiny)

source("C:/Documents/latihan.R")
# Define server logic required to draw a histogram
shinyServer(function(input,output) {
    
    output$table <- renderTable({
        df
    })
    
    output$corpustxt <- renderTable({
        corpus.txt()
    })
    
    output$confmat <- renderTable({
        conf.mat
    })
    
    output$negpos <- renderTable({
        table(df$klasifikasi)
        
    output$plotpred <- renderPlot({
        plotpred
    })
    })
})

这是我的错误

Warning: Error in as.data.frame.default: cannot coerce class ‘"naiveBayes"’ to a data.frame
  81: stop
  80: as.data.frame.default
  78: data.frame
  77: write.table
  72: observeEventHandler [C:\Users\Documents\AnalisisSentimen\AnalisisSentimenApp/server.R#211]
   1: runApp

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)