【发布时间】:2020-02-09 11:57:03
【问题描述】:
我有一个包含 45045 个变量的数据框,并且在 R 中只有 90 个观察值。我做了一个 PCA 来降低维度,我将使用 14 个主成分。我需要做预测,我想尝试使用朴素贝叶斯方法。我不能对转换后的数据使用预测函数,我不理解错误。
这里有一些代码:
data.pca <- prcomp(data)
我将使用 14 台电脑:
newdata <- as.data.frame(data.pca$x[,1:14]) #dimension: 90x14
培训:
库(naivebayes)
mod.nb <- naive_bayes(label ~ newdata$PC1+...+newdata$PC14, data = NULL)
尝试预测第 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。
【问题讨论】:
-
你没有在训练集和测试集中划分数据。另一件事你为什么要放
data = NULL。在naive_bayes行中应该是data = newdata。使用pred <- predict(mod.nb, newdata)。 -
您好,当您想使用公式界面时,您的
newdata数据集应包含变量label和 14 个组件。然后您可以使用以下内容:mod.nb <- naive_bayes(label ~ PC1+...+ PC14, data = newdata)或更简单的mod.nb <- naive_bayes(label ~ ., data = newdata)。 -
有关如何使用
formula interface或matrix/vector interface的更多示例,请参阅扩展文档:majkamichal.github.io/naivebayes/articles/naivebayes.html Best, Michal -
谢谢,我想我可以使用标签和列分隔的向量,所以我把 data = NULL。当我将所有内容放在一个数据框中时,它就可以工作。
标签: r pca predict naivebayes