簡體   English   中英

在測試和訓練數據集上使用朴素貝葉斯 function

[英]Using the naive Bayes function on a test and training set of data

我正在嘗試在訓練和測試數據集上使用 NaiveBayes function。 我正在使用這個有用的網站: https://rpubs.com/riazakhan94/naive_bayes_classifier_e1071

但是,由於某種原因它不起作用,這是我得到的錯誤:“表中的錯誤(train$Class,trainPred):所有 arguments 必須具有相同的長度。”

這是我正在使用的代碼,我猜它是一個超級簡單的修復。 數據集的 x 和 y 列在 class 列上進行預測:

https://github.com/samuelc12359/NaiveBayes.git


test <- read.csv(file="TestX.csv",header=FALSE)
train <- read.csv(file="TrainX.csv",header=FALSE)

Names <- c("x","y","Class")
colnames(test)<- Names
colnames(train)<- Names

NBclassfier=naiveBayes(Class~x+y, data=train)
print(NBclassfier)


trainPred=predict(NBclassfier,train, type="class")
trainTable=table(train$Class, trainPred)
testPred=predict(NBclassfier, newdata=test, type="class")
testTable=table(test$Class, testPred)
print(trainTable)
print(testTable)

您需要將Class列轉換為因子,例如:

train$Class = factor(train$Class)
test$Class = factor(test$Class)

然后,當您調用naiveBayes()進行訓練並隨后進行預測時,它會按照您的預期進行。

或者,您可以將預測類型更改為"raw"並直接將它們轉換為結果。 比如像這樣:

train_predictions = predict(NBclassfier,train, type="raw")
trainPred = 1 * (train_predictions[, 2] >= 0.5 )
trainTable=table(train$Class, trainPred)
test_predictions = predict(NBclassfier, newdata=test, type="raw")
testPred = 1 * (test_predictions[, 2] >= 0.5 )
testTable=table(test$Class, testPred)
print(trainTable)
print(testTable)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM