【问题标题】:How to fix predict.naive_bayes using no features for prediction in R如何在 R 中使用无预测功能来修复 predict.naive_bayes
【发布时间】: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 &lt;- predict(mod.nb, newdata)
  • 您好,当您想使用公式界面时,您的 newdata 数据集应包含变量 label 和 14 个组件。然后您可以使用以下内容:mod.nb &lt;- naive_bayes(label ~ PC1+...+ PC14, data = newdata) 或更简单的mod.nb &lt;- naive_bayes(label ~ ., data = newdata)
  • 有关如何使用formula interfacematrix/vector interface 的更多示例,请参阅扩展文档:majkamichal.github.io/naivebayes/articles/naivebayes.html Best, Michal
  • 谢谢,我想我可以使用标签和列分隔的向量,所以我把 data = NULL。当我将所有内容放在一个数据框中时,它就可以工作。

标签: r pca predict naivebayes


【解决方案1】:

您可以尝试仅从您的代码修改的以下代码

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-17
    • 2018-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多