[英]How to fix predict.naive_bayes using no features for prediction in R
我有一個包含 45045 個變量的數據框,在 R 中只有 90 個觀察值。 我做了一個 PCA 來減少維度,我將使用 14 個主成分。 我需要做預測,我想嘗試使用朴素貝葉斯方法。 我不能將預測 function 與轉換后的數據一起使用,我不理解錯誤。
這是一些代碼:
data.pca <- prcomp(data)
我將使用 14 台電腦:
newdata <- as.data.frame(data.pca$x[,1:14]) #dimension: 90x14
訓練:
圖書館(朴素貝葉斯)
mod.nb <- naive_bayes(label ~ newdata$PC1+...+newdata$PC14, data = NULL)
Tryna 預測第 50 個觀測值:
test.pca <- predict(data.pca, newdata = data[50,])
test.pca <- as.data.frame(test.pca)
test.pca <- test.pca[,1:14]
pred <- predict(mod.nb, test.pca)
我收到這些錯誤:
predict.naive_bayes(): Only 0 feature(s) out of 14 defined in the naive_bayes object "mod.nb" are used for prediction.
predict.naive_bayes(): No feature in the newdata corresponds to probability tables in the object. Classification is done based on the prior probabilities
標簽向量是一個級別為 1 到 6 的因子,對於我嘗試預測結果的任何觀察結果都只有 1。例如,第 50 個觀察結果的 label 為 4。
您可以嘗試僅從您的代碼修改的以下代碼
data.pca <- prcomp(data)
newdata <- as.data.frame(data.pca$x[,1:14])
library(naivebayes)
mod.nb <- naive_bayes(label ~ newdata$PC1+...+newdata$PC14, data = newdata)
test.pca <- predict(mod.nb, newdata = newdata[50,])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.