r- как мне создать матрицу путаницы из моего набора данных - PullRequest
0 голосов
/ 04 октября 2018

Может кто-нибудь посоветовать мне и помочь мне создать Матрицу смешения для модели SVM, потому что я получаю следующую ошибку:

"Error: 'data' and 'reference' should be factors with the same levels." 

из приведенного ниже кода Матрицы смешения ...

confusionMatrix(predA, tmp_test$Score)

Я также пытался

confusionMatrix(table(predA, tmp_test)) 

Затем я получил следующую ошибку ...

"Error in table(predA, tmp_test) : all arguments must have the same length"

Модель SVM - это регрессия.

таблица сэмплов ...

Unhelpful Score 
7     1
8     3
5     1
7     2
4     1 
4     1
5     1
9     2
6     1
5     1
11    3

Есть 2108 объектов и 2 переменные.Отсутствуют отсутствующие или недействительные данные или 0 (ноль) значений.Бесполезное значение варьируется от 4 до 2016. Значение балла варьируется от 1 до 3.

Вот мой мой код ...

# Random sampling
samplesize = 0.60 * nrow(dsTemp)
set.seed(80)
index = sample(seq_len(nrow(dsTemp)), size = samplesize)

# Create training and test set
datatrain = dsTemp[ index, ]
datatest = dsTemp[ -index, ]


library(caret)
library(e1071)
library(tidyverse)

tmp_train <-datatrain
tmp_test <- datatest

#orginally datatypes were int but I had to change to factor for the model 
#to work
dsTemp$Score <- factor(dsTemp$Score)
dsTemp$Unhelpful <- factor(dsTemp$Unhelpful)

dsTemp$Unhelpful <- factor(dsTemp$Unhelpful)
dsTemp$Score <- factor(dsTemp$Score)

#svm model
Model <- svm(Score ~., data=tmp_train,kernel='linear',gamma=0.2,cost=100)

#predictions
predA <- predict(svmModel, tmp_test)

РЕДАКТИРОВАТЬ

tmp_train$Score <- factor(tmp_train$Score)
tmp_test$Score <- factor(tmp_test$Score)

tmp_train$HelpfulnessDenominator <- factor(tmp_train$HelpfulnessDenominator)
tmp_test$HelpfulnessDenominator <- factor(tmp_test$HelpfulnessDenominator)

Получение ошибки после

confusionMatrix(predA, tmp_test) 

или

confusionMatrix(table(predA, tmp_test))  

str(predA)
 Factor w/ 3 levels "1","2","3": 1 1 1 1 1 1 1 1 1 1 ...
 - attr(*, "names")= chr [1:1264] "927" "1179" "1655" "156" …

str(tmp_test$Score)
Factor w/ 3 levels "1","2","3": 1 3 3 3 1 1 1 2 2 3 ...

1 Ответ

0 голосов
/ 04 октября 2018

Похоже, вы не переходите на factors и в поезде, и в тестовом наборе, а вместо этого в dsTemp:

dsTemp$Score <- factor(dsTemp$Score)
dsTemp$Unhelpful <- factor(dsTemp$Unhelpful)

dsTemp$Unhelpful <- factor(dsTemp$Unhelpful)
dsTemp$Score <- factor(dsTemp$Score) #also this is just a repetition

Вместо этого, вероятно, должно быть:

tmp_train$Score <- factor(tmp_train$Score)
tmp_test$Score <- factor(tmp_test$Score)

Потому что это наборы данных, которые вы вызываете позже:

#svm model
Model <- svm(Score ~., data=tmp_train,kernel='linear',gamma=0.2,cost=100)

#predictions
predA <- predict(svmModel, tmp_test)

И это правильный вызов для confusionMatrix:

confusionMatrix(predA, tmp_test$Score)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...