我有一个svm模型,我希望将预测结果显示为数据框而不是模型
model <- svm(training$classe ~., data = training, probability=TRUE)
pred<- (model, testing, probability = TRUE)
hist(pred)
目前如果我
class(pred) #it shows a factor.
如果我运行pred
>pred
1
A
attr(,"probabilities")
A B C D E
1 0.9995978 0.0002552521 2.157329e-05 3.019972e-06 0.0001223695
Levels: A B C D E
我需要的是一个数据框,用于绘制5个箱的直方图,每个箱作为一个类标签,它们的频率就是它们的概率。
There are 5 class labels A to E
我不一定要使用直方图。任何其他情节都可以。
答案 0 :(得分:1)
据我所知,你得到了这个结果:
library(e1071)
set.seed(357)
tds <- sample(1:nrow(iris), 10)
iris.train <- iris[-tds, ]
iris.test <- iris[tds, ]
model <- svm(Species ~ ., data = iris, probability = TRUE)
predict(model, newdata = iris.test, probability = TRUE)
17 9 42 34 96 68 135 107 111 140
setosa setosa setosa setosa versicolor versicolor virginica virginica virginica virginica
attr(,"probabilities")
setosa versicolor virginica
17 0.975617665 0.01471624 0.009666098
9 0.965967587 0.02223986 0.011792550
42 0.862565881 0.11739501 0.020039111
34 0.967946010 0.01838272 0.013671268
96 0.015016432 0.98088056 0.004103008
68 0.010676592 0.98680145 0.002521956
135 0.010729187 0.33894236 0.650328456
107 0.010702200 0.37824730 0.611050505
111 0.009757802 0.06857737 0.921664824
140 0.007114249 0.01210420 0.980781547
Levels: setosa versicolor virginica
如果关闭概率,我可以预测一个类,使用table
计算频率并绘制条形图。
model2 <- svm(Species ~ ., data = iris)
barplot(table(predict(model2, newdata = iris.test)))