简体   繁体   中英

How to fix predict.naive_bayes using no features for prediction in R

I have a data frame with 45045 variables and only 90 observations in R. I did a PCA to reduce the dimension and I'll use 14 principal components. I need do predictions and I wanna try to use the Naive Bayes method. I can't use the predict function with the trasformed data and i'm not understanding the error.

Here is some code:

data.pca <- prcomp(data)

I'll use 14 PCs:

newdata <- as.data.frame(data.pca$x[,1:14]) #dimension: 90x14

Training:

library(naivebayes)

mod.nb <- naive_bayes(label ~ newdata$PC1+...+newdata$PC14, data = NULL)

Tryna predict the 50th observation:

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)

I'm getting these errors:

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

The vector of labels is a factor with levels 1 to 6, and for any observation that I try to predict the result is only 1. The 50th observation, for example, has the label 4.

You can try the following code modified from your code only

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,])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM