简体   繁体   English

如何在 R 中使用无预测功能修复 predict.naive_bayes

[英]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.我有一个包含 45045 个变量的数据框,在 R 中只有 90 个观察值。 I did a PCA to reduce the dimension and I'll use 14 principal components.我做了一个 PCA 来减少维度,我将使用 14 个主成分。 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.我不能将预测 function 与转换后的数据一起使用,我不理解错误。

Here is some code:这是一些代码:

data.pca <- prcomp(data)

I'll use 14 PCs:我将使用 14 台电脑:

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: 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)

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.标签向量是一个级别为 1 到 6 的因子,对于我尝试预测结果的任何观察结果都只有 1。例如,第 50 个观察结果的 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,])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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