0
有關更多上下文,請參閱question listed here。帶插入符號的Text2Vec分類 - 樸素貝葉斯警告消息
我試圖使用text2vec
構建的文檔術語矩陣來訓練使用caret
包的樸素貝葉斯(nb
)模型。但是,我得到這樣的警告消息:
警告消息: 在的eval(XPR,ENVIR = ENVIR): 模型擬合失敗Fold01.Rep1:usekernel = FALSE,FL = 0,調整= 1個錯誤NaiveBayes.default(X,Y,usekernel = FALSE,FL =參數$佛羅里達州,...): 零方差至少一類變量:
請幫我理解這條消息,並哪些步驟我需要避免模型擬合失敗。我有一種感覺,我需要從DTM中刪除更多的稀疏詞彙,但我不確定。
代碼來構建模型:
control <- trainControl(method="repeatedcv", number=10, repeats=3, savePredictions=TRUE, classProbs=TRUE)
Train_PRDHA_String.df$Result <- ifelse(Train_PRDHA_String.df$Result == 1, "X", "Y")
(warn=1)
(warnings=2)
t4 = Sys.time()
svm_nb <- train(x = as.matrix(dtm_train), y = as.factor(Train_PRDHA_String.df$Result),
method = "nb",
trControl=control,
tuneLength = 5,
metric ="Accuracy")
print(difftime(Sys.time(), t4, units = 'sec'))
代碼來構建文檔詞矩陣(Text2Vec):
library(text2vec)
library(data.table)
#Define preprocessing function and tokenization fucntion
preproc_func = tolower
token_func = word_tokenizer
#Union both of the Text fields - learn vocab from both fields
union_txt = c(Train_PRDHA_String.df$MAKTX_Keyword, Train_PRDHA_String.df$PH_Level_04_Description_Keyword)
#Create an iterator over tokens with the itoken() function
it_train = itoken(union_txt,
preprocessor = preproc_func,
tokenizer = token_func,
ids = Train_PRDHA_String.df$ID,
progressbar = TRUE)
#Build Vocabulary
vocab = create_vocabulary(it_train)
vocab
#Dimensional Reduction
pruned_vocab = prune_vocabulary(vocab,
term_count_min = 10,
doc_proportion_max = 0.5,
doc_proportion_min = 0.001)
vectorizer = vocab_vectorizer(pruned_vocab)
#Start building a document-term matrix
#vectorizer = vocab_vectorizer(vocab)
#learn vocabulary from Train_PRDHA_String.df$MAKTX_Keyword
it1 = itoken(Train_PRDHA_String.df$MAKTX_Keyword, preproc_func,
token_func, ids = Train_PRDHA_String.df$ID)
dtm_train_1 = create_dtm(it1, vectorizer)
#learn vocabulary from Train_PRDHA_String.df$PH_Level_04_Description_Keyword
it2 = itoken(Train_PRDHA_String.df$PH_Level_04_Description_Keyword, preproc_func,
token_func, ids = Train_PRDHA_String.df$ID)
dtm_train_2 = create_dtm(it2, vectorizer)
#Combine dtm1 & dtm2 into a single matrix
dtm_train = cbind(dtm_train_1, dtm_train_2)
#Normalise
dtm_train = normalize(dtm_train, "l1")
dim(dtm_train)
感謝pepo。那就是訣竅。下一次會記住一個可重複的例子! – UbuntuNewbie