R语言-贝叶斯算法过滤手机垃圾短信

完整代码:

sms <- read.csv("/Users/wenfeng/Desktop/R&ML/机器学习和R语言/机器学习实验5 朴素贝叶斯/sms_spam.csv", stringsAsFactors = FALSE)
#install.packages("tm")
library(NLP)
library(tm)
#数据准备
str(sms)
sms$type<-factor(sms$type)
str(sms$type)
table(sms$type)
#数据准备
sms_corpus<-Corpus(VectorSource(sms$text))
print(sms_corpus)
inspect(sms_corpus[1:3])

corpus_clean<-tm_map(sms_corpus,tolower)
corpus_clean<-tm_map(corpus_clean,removeNumbers)
corpus_clean<-tm_map(corpus_clean,removeWords,stopwords())
corpus_clean<-tm_map(corpus_clean,removePunctuation)
corpus_clean<-tm_map(corpus_clean,stripWhitespace)
inspect(corpus_clean[1:3])#验证处理后的数据

sms_dtm<-DocumentTermMatrix(corpus_clean)
sms_dtm

#训练
sms_train <- sms[1:4169, ]
sms_test  <- sms[4170:5559, ]

sms_dtm_train <- sms_dtm[1:4169, ]
sms_dtm_test  <- sms_dtm[4170:5559, ]

sms_corpus_train <- corpus_clean[1:4169]
sms_corpus_test  <- corpus_clean[4170:5559]

prop.table(table(sms_train$type))
prop.table(table(sms_test$type))

#install.packages("wordcloud")
library(RColorBrewer)
library(wordcloud)
wordcloud(sms_corpus_train, min.freq = 30, random.order = FALSE)

spam <- subset(sms_train, type == "spam")
ham  <- subset(sms_train, type == "ham")

wordcloud(spam$text, max.words = 40, scale = c(3, 0.5))
wordcloud(ham$text, max.words = 40, scale = c(3, 0.5))

# 标示大于5次的关键词(为频繁出现的单词创建指示特征)
sms_term <- TermDocumentMatrix(sms_corpus,control = list(removePunctuation = TRUE,stopwords = TRUE))

#获取次数大于5次的词组成字典
#sms_dict <- Dictionary(findFreqTerms(sms_dtm_train, 5))
#sms_list <- Terms(findFreqTerms(sms_term, 5))
sms_dict <- findFreqTerms(sms_term, 5)

sms_train2 <- DocumentTermMatrix(sms_corpus_train, list(dictionary = sms_dict))
sms_test2  <- DocumentTermMatrix(sms_corpus_test, list(dictionary = sms_dict))

convert_counts <- function(x) {
  x <- ifelse(x > 0, 1, 0)
  x <- factor(x, levels = c(0, 1), labels = c("No", "Yes"))
}

sms_train2 <- apply(sms_train2, MARGIN = 2, convert_counts)
sms_test2  <- apply(sms_test2, MARGIN = 2, convert_counts)

#训练模型
#install.packages("e1071")
library(e1071)
sms_classifier <- naiveBayes(sms_train2, sms_train$type)
sms_classifier

sms_test_pred <- predict(sms_classifier, sms_test2)

library(gmodels)
CrossTable(sms_test_pred, sms_test$type,
           prop.chisq = FALSE, prop.t = FALSE, prop.r = FALSE,
           dnn = c('predicted', 'actual'))

sms_classifier2 <- naiveBayes(sms_train2, sms_train$type, laplace = 1)
sms_test_pred2 <- predict(sms_classifier2, sms_test)
CrossTable(sms_test_pred2, sms_est$type,
           prop.chisq = FALSE, prop.t = FALSE, prop.r = FALSE,
           dnn = c('predicted', 'actual'))

你可能感兴趣的:(R语言)