[英]feature selection for Naive Bayes
我與朴素貝葉斯進行了分類。 目標是通過文本預測4個因素。 數據如下所示:
'data.frame': 387 obs. of 2 variables:
$ reviewText: chr "I love this. I have a D800. I am mention my camera to make sure that you understand that this product is not ju"| __truncated__ "I hate buying larger gig memory cards - because there's always that greater risk of losing the photos, and/or r"| __truncated__ "These chromebooks are really a pretty nice idea -- Almost no maintaince (no maintaince?), no moving parts, smal"| __truncated__ "Purchased, as this drive allows a much speedier read/write and is just below a full SSD (they need to drop the "| __truncated__ ...
$ pragmatic : Factor w/ 4 levels "-1","0","1","9": 4 4 4 3 3 4 3 3 3...
我用caret
包進行了分類。 分類的代碼如下所示:
sms_corpus <- Corpus(VectorSource(sms_raw$text))
sms_corpus_clean <- sms_corpus %>%
tm_map(content_transformer(tolower)) %>%
tm_map(removeNumbers) %>%
tm_map(removeWords, stopwords(kind="en")) %>%
tm_map(removePunctuation) %>%
tm_map(stripWhitespace)
sms_dtm <- DocumentTermMatrix(sms_corpus_clean)
train_index <- createDataPartition(sms_raw$type, p=0.5, list=FALSE)
sms_raw_train <- sms_raw[train_index,]
sms_raw_test <- sms_raw[-train_index,]
sms_corpus_clean_train <- sms_corpus_clean[train_index]
sms_corpus_clean_test <- sms_corpus_clean[-train_index]
sms_dtm_train <- sms_dtm[train_index,]
sms_dtm_test <- sms_dtm[-train_index,]
sms_dict <- findFreqTerms(sms_dtm_train, lowfreq= 5)
sms_train <- DocumentTermMatrix(sms_corpus_clean_train, list(dictionary=sms_dict))
sms_test <- DocumentTermMatrix(sms_corpus_clean_test, list(dictionary=sms_dict))
convert_counts <- function(x) {
x <- ifelse(x > 0, 1, 0)
x <- factor(x, levels = c(0, 1), labels = c("Absent", "Present"))
}
sms_train <- sms_train %>% apply(MARGIN=2, FUN=convert_counts)
sms_test <- sms_test %>% apply(MARGIN=2, FUN=convert_counts)
ctrl <- trainControl(method="cv", 10)
set.seed(8)
sms_model1 <- train(sms_train, sms_raw_train$type, method="nb",
trControl=ctrl)
sms_predict1 <- predict(sms_model1, sms_test)
cm1 <- confusionMatrix(sms_predict1, sms_raw_test$type)
當我以這種方式使用該模型時,這意味着我同時對所有4個變量進行了預測,但得到的Accuracy:0.5469
很低Accuracy:0.5469
,混淆矩陣如下所示。
Reference
Prediction -1 0 1 9
-1 0 0 1 0
0 0 0 0 0
1 9 5 33 25
9 11 3 33 72
當我分別對所有4個變量進行預測時,可以獲得更好的結果。 分類的代碼與上面相同,但是我不是df$sensorial <- factor(df$sensorial)
df$sensorial <- as.factor(df$sensorial == 9)
df$sensorial <- factor(df$sensorial)
而是df$sensorial <- as.factor(df$sensorial == 9)
。 對於其他變量,我使用1
, -1
或0
而不是9
。 如果以這種方式進行操作,我將獲得Accuracy: 0.772
(代表9
, Accuracy:0.829
(代表-1
, Accuracy:0.9016
(代表0
和Accuracy:0.7959
(代表1
。 此外,結果要好得多。 因此,它必須與特征選擇有關。 結果不同的原因可能是特征對於不同的值通常是相同的。 因此,一種可能的解決方案是賦予這些功能更高的重要性,這些功能僅在存在某個值時才出現,而在其他值不存在時才出現。 有沒有辦法以這種方式選擇特征,以便如果同時對所有4個變量進行預測,模型會更好? 像加權術語文檔矩陣之類的東西?
編輯:
我計算了Cihan Ceyhan告訴的四個值的權重:
prop.table(table(sms_raw_train$type))
-1 0 1 9
0.025773196 0.005154639 0.180412371 0.788659794
modelweights <- ifelse(sms_raw_train$type == -1,
(1/table(sms_raw_train$type)[1]) * 0.25,
ifelse(sms_raw_train$type == 0,
(1/table(sms_raw_train$type)[2]) * 0.25,
ifelse(sms_raw_train$type == 1,
(1/table(sms_raw_train$type)[3]) * 0.25,
ifelse(sms_raw_train$type == 9,
(1/table(sms_raw_train$type)[4]) * 0.25,9))))
但是結果卻不是更好Accuracy:0.5677
Reference
Prediction -1 0 1 9
-1 1 0 1 1
0 1 0 1 0
1 11 3 32 20
9 7 5 33 76
因此,最好是分別計算每個值的結果,然后像發布的第二個解決方案一樣對結果求和。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.