Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何计算不同阈值的真阳性率(TPR)和假阳性率(FPR),以生成分类模型的ROC_Java_Api_Machine Learning_Weka_Roc - Fatal编程技术网

Java 如何计算不同阈值的真阳性率(TPR)和假阳性率(FPR),以生成分类模型的ROC

Java 如何计算不同阈值的真阳性率(TPR)和假阳性率(FPR),以生成分类模型的ROC,java,api,machine-learning,weka,roc,Java,Api,Machine Learning,Weka,Roc,我建立了一个机器学习模型,使用朴素贝叶斯多项式对文档进行分类。我正在使用JavaWekaAPI来训练和测试模型。为了评估模型性能,我想生成ROC曲线。我不明白如何计算不同阈值的TPR和FPR。我附上了我的源代码和示例数据集。如果有人能帮我计算生成ROC曲线的不同阈值的TPR和FPR,我将不胜感激。提前感谢你的帮助。 我的Java代码: package smote; import java.io.File; import java.util.Random; impo

我建立了一个机器学习模型,使用朴素贝叶斯多项式对文档进行分类。我正在使用JavaWekaAPI来训练和测试模型。为了评估模型性能,我想生成ROC曲线。我不明白如何计算不同阈值的TPR和FPR。我附上了我的源代码和示例数据集。如果有人能帮我计算生成ROC曲线的不同阈值的TPR和FPR,我将不胜感激。提前感谢你的帮助。 我的Java代码:

    package smote;
    import java.io.File;
    import java.util.Random;
    import weka.classifiers.Classifier;
    import weka.classifiers.bayes.NaiveBayesMultinomial;
    import weka.core.Instance;
    import weka.core.Instances;
    import weka.core.converters.ConverterUtils.DataSource;
    import weka.filters.Filter;
    import weka.filters.unsupervised.attribute.StringToWordVector;
    public class calRoc {
        public static void main(String agrs[]) throws Exception{
            String fileRootPath = "...../DocsFIle.arff";
            Instances rawData = DataSource.read(fileRootPath);
            StringToWordVector filter = new StringToWordVector(10000);
            filter.setInputFormat(rawData);
            String[] options = { "-W", "10000", "-L", "-M", "2",
                            "-stemmer", 
            "weka.core.stemmers.IteratedLovinsStemmer", 
                            "-stopwords-handler", 
            "weka.core.stopwords.Rainbow", 
                            "-tokenizer", 
            "weka.core.tokenizers.AlphabeticTokenizer" 
                            };
            filter.setOptions(options);
            filter.setIDFTransform(true);
            filter.setStopwords(new 

      File("/Research/DoctoralReseacher/IEICE/Dataset/stopwords.txt"));
      Instances data = Filter.useFilter(rawData,filter);
      data.setClassIndex(0);        

      int numRuns = 10;
      double[] recall=new double[numRuns];
      double[] precision=new double[numRuns];
      double[] fmeasure=new double[numRuns];
      double tp, fp, fn, tn;
      String classifierName[] = { "NBM"};
      double totalPrecision,totalRecall,totalFmeasure;
     totalPrecision=totalRecall=totalFmeasure=0;
     double avgPrecision, avgRecall, avgFmeasure;
     avgPrecision=avgRecall=avgFmeasure=0;                 
     for(int run = 0; run < numRuns; run++) {
        Classifier classifier = null;
        classifier = new NaiveBayesMultinomial();
        int folds = 10;         
        Random random = new Random(1);
        data.randomize(random);
        data.stratify(folds);
        tp = fp = fn = tn = 0;
        for (int i = 0; i < folds; i++) {
            Instances trains = data.trainCV(folds, i,random);
            Instances tests = data.testCV(folds, i);
            classifier.buildClassifier(trains);             
            for (int j = 0; j < tests.numInstances(); j++) {
                Instance instance = tests.instance(j);                  
                double classValue = instance.classValue();                  
                double result = classifier.classifyInstance(instance);
                if (result == 0.0 && classValue == 0.0) {
                    tp++;
                } else if (result == 0.0 && classValue == 1.0) {
                    fp++;
                } else if (result == 1.0 && classValue == 0.0) {
                    fn++;
                } else if (result == 1.0 && classValue == 1.0) {
                    tn++;
                }
            }   
        }

        if (tn + fn > 0)
            precision[run] = tn / (tn + fn);
        if (tn + fp > 0)
            recall[run] = tn / (tn + fp);
        if (precision[run] + recall[run] > 0)
            fmeasure[run] = 2 * precision[run] * recall[run] / (precision[run] + recall[run]);
        System.out.println("The "+(run+1)+"-th run");
        System.out.println("Precision: " + precision[run]);
        System.out.println("Recall: " + recall[run]);
        System.out.println("Fmeasure: " + fmeasure[run]);
        totalPrecision+=precision[run];
        totalRecall+=recall[run];
        totalFmeasure+=fmeasure[run];

     }
     avgPrecision=totalPrecision/numRuns;
     avgRecall=totalRecall/numRuns;
     avgFmeasure=totalFmeasure/numRuns;
     System.out.println("avgPrecision: " + avgPrecision);
     System.out.println("avgRecall: " + avgRecall);
     System.out.println("avgFmeasure: " + avgFmeasure);
    }

你听起来有点困惑;默认情况下,ROC曲线是跨所有可能的阈值计算的,而不是针对特定的阈值-请参阅了解一些解释感谢您的回复@desertnaut。是的,对于如何准确计算不同阈值的TPR和FPR,我有点困惑。我不是用默认的求值函数来生成ROC,我想用TP、FP、FN、TN来计算每个阈值点的TPR和FPR。我想用Java代码计算TPR和FPR,比如精度和召回率(参见源代码)。ROC对于分类方法来说毫无意义,因为算法已经做出了决定,并且不再有阈值变化。你需要获得类概率或一些原始的定量分数。我不知道你会怎么对weka这么做。也许可以试着找到一个回归算法。谢谢你的评论和建议。
Sample Dataset with few instances:

@relation 'CamelBug'

@attribute Feature string

@attribute class-att {0,1}

@data

'XQuery creates an empty out message that makes it impossible to chain 
 more processors behind it ',1

'org apache camel Message hasAttachments is buggy ',0

'unmarshal new JaxbDataFormat com foo bar returning JAXBElement ',0

'Can t get the soap header when the camel cxf endpoint working in the 
  PAYLOAD data fromat ',0

'camel jetty Exchange failures should not be returned as ',1
'Delayer not working as expected ',1
'ParallelProcessing and executor flags are ignored in Multicast 
  processor ',1