我有一个有45045个变量的数据框架,R中只有90个观测值。我做了一个主成分分析来降低维度,我将使用14个主成分。我需要做预测,我想尝试使用朴素贝叶斯方法。我不能对转换后的数据使用预测功能,并且我不理解错误。
下面是一些代码:
data.pca <- prcomp(data)我将使用14台PC:
newdata <- as.data.frame(data.pca$x[,1:14]) #dimension: 90x14培训:
库(Naivebayes)
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个观测的标签为4。
发布于 2019-10-12 12:00:26
您可以尝试仅从您的代码修改的以下代码
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,])https://stackoverflow.com/questions/58350003
复制相似问题