分类模型常用评价指标——混淆矩阵和roc曲线

混淆矩阵

在这里插入图片描述

定量精度
  • 总体分类精度(Overall Accuracy)
    分类正确的样本数/总样本数
    OA=(e+f+g)/(a+b+c+d+e+f+g+h+i)
  • Kappa系数
    P0=OA
    Pe=∑(真实样本数×分类样本数)/样本总数2
    例如:Pe=[(a+b+c)×(a+d+g)+(b+e+h)×(d+e+f)+(g+h+i)×(c+f+i)] /(a+b+c+d+e+f+g+h+i)2
    Kappa=(P0-Pe )/(1-Pe)
  • 用户精度(User accuracy )
    从预测的角度出发,预测正确的样本占该类别总的预测样本数
    • 类别1user_accuracy=a/(a+d+g)
    • 类别2user_accuracy=e/(b+e+h)
    • 类别3user_accuracy=i/(c+f+i)
  • 生产者精度(Producer accuracy)
    用样本的角度出发,被正确预测的样本数占该类别的样本总数
    • 类别1Prod.accuracy=a/(a+b+c)
    • 类别2Prod.accuracy=e/(d+e+f)
    • 类别3Prod.accuracy=i/(g+h+i)
ROC曲线

roc曲线主要用来评估二分类模型的精度
在这里插入图片描述
a:TP(真阳性)
c:FP(假阳性)
b:FN(假阳性)
d:TN(真阴性)

  • 真检率(灵敏度,击中率)
    TPR=a/(a+b)
  • 误检率(特异度,虚惊率)
    FPR=c/(c+d)

ROC曲线的坐标横轴是FPR,坐标纵轴是TPR,ROC曲线描述的不是FPR与TPR间的函数关系
在这里插入图片描述

ROC曲线描述的不是FPR与TPR间的函数关系,FPR与TPR间没有函数关系,FPR与TPR是阈值τ的函数,τ描述的是样本中被检测为阳性的比率,当τ=0时,所有样本被识别为阴性,此时FPR(特异度、虚惊率、误检率)为0(FPR越小越好),但TPR(灵敏度、命中率、真检率)为0(TPR越大越好),当τ=1时所有样本被识别为阳性,此时TPR=1,FPR=1。因此需要选择一个合适的τ值使得TPR越大越好,FPR越小越好。

曲线下面积(Area Under Curve,AUC)
AUC等于roc曲线与坐标横轴围城的面积,是roc曲线的积分,AUC数值越大越好

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在Scala中使用支持向量机模型计混淆矩阵ROC曲线,您需要使用Spark MLlib库。以下是一个简单的示例: ```scala import org.apache.spark.ml.classification.LinearSVC import org.apache.spark.ml.evaluation.BinaryClassificationEvaluator import org.apache.spark.ml.feature.{VectorAssembler, StringIndexer} import org.apache.spark.ml.linalg.Vectors import org.apache.spark.sql.SparkSession val spark = SparkSession.builder().appName("SVMExample").getOrCreate() // Load data val data = spark.read.format("csv").option("header", "true").load("data.csv") // Convert label column to numeric val labelIndexer = new StringIndexer().setInputCol("label").setOutputCol("indexedLabel").fit(data) val indexed = labelIndexer.transform(data) // Assemble feature columns into a vector val assembler = new VectorAssembler().setInputCols(Array("feature1", "feature2")).setOutputCol("features") val assembled = assembler.transform(indexed) // Split data into training and test sets val Array(training, test) = assembled.randomSplit(Array(0.7, 0.3), seed = 12345) // Train SVM model val svm = new LinearSVC().setMaxIter(10).setRegParam(0.1).setElasticNetParam(0.0) val model = svm.fit(training) // Make predictions on test data val predictions = model.transform(test) // Compute evaluation metrics val evaluator = new BinaryClassificationEvaluator().setLabelCol("indexedLabel").setRawPredictionCol("rawPrediction").setMetricName("areaUnderROC") val areaUnderROC = evaluator.evaluate(predictions) val tp = predictions.filter("prediction = 1.0 AND indexedLabel = 1.0").count() val fp = predictions.filter("prediction = 1.0 AND indexedLabel = 0.0").count() val tn = predictions.filter("prediction = 0.0 AND indexedLabel = 0.0").count() val fn = predictions.filter("prediction = 0.0 AND indexedLabel = 1.0").count() val confusionMatrix = Seq( (tp, fp), (fn, tn) ) // Output results println(s"Area under ROC: $areaUnderROC") println(s"Confusion matrix:\n${confusionMatrix.mkString("\n")}") ``` 请注意,这些示例假定您已经将数据加载到Spark DataFrame中,并且已经使用StringIndexer和VectorAssembler转换了数据以进行训练和预测。您需要根据您的数据和模型进行相应的更改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值