我正在使用precrec
包来评估多个模型并绘制ROC和PR-ROC。
我想在最后做一个模型比较,但似乎我不能在同一块上绘制两个模型。
这是我的尝试:
library(precrec)
library(caret)
library(e1071)
classifier = svm(formula = Class ~ .,
data = train_smote_maison,
type = 'C-classification',
kernel = "linear",
probability = TRUE,
cross = 3,
cost = 1)
test_svm_plot = df[train.test.split == 2,]
predictions_svm2 <- predict(classifier,newdata = test_svm_plot, probability=T)
svm2_predict_obj <- mmdata(as.numeric(predictions_svm2),test_svm_plot$Class)
svm2_perfromance <- evalmod(svm2_predict_obj)
classifier_logreg <- glm(data = train, family = "binomial",
formula = Class ~ .)
test_glm = test
test_glm_plot = df[train.test.split == 2,]
predictions_logreg <- predict(classifier_logreg,newdata = test_glm, type = "response")
logreg_predict_obj <- mmdata(predictions_logreg,test_glm$Class)
logreg_performance <- evalmod(mdat = logreg_predict_obj)
plot(svm2_perfromance, "ROC")
plot(logreg_performance, "ROC", add=TRUE, col='red')
有人知道如何确保我可以在同一块上获得两个 ROC 吗?
提前谢谢。
您可以使用"fortify"为每个性能对象制作一个 ggplot 数据帧,然后将它们组合 (rbind( 并使用 ggplot 绘制。
library(ggplot2)
svm2_df <- fortify(svm2_perfromance)
logreg_df <- fortify(logreg_performance)
svm2_df$classifier <- "svm2"
logreg_df$classifier <- "logreg"
performance_df <- rbind(svm2_df, logreg_df)
roc <- performance_df[performance_df$classifier == "ROC",]
ggplot(roc, aes(x=x, y=y, group = classifier)) + geom_line(aes(color = classifier))
import plotly.express as px
x = ["K 邻居分类器", "随机森林分类器", "逻辑回归", "XGBoost 分类器"]
y = [knn_accuracy100, rf_accuracy100, lr_accuracy100, xgb_accuracy100]
图 = px.bar(x=x, y=y, 颜色=x, title="模型比较 - 模型精度", 标签={ "x": "模型", 'y': '模型精度'},( 图所示((