Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  *
0009  *    http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  */
0017 
0018 package org.apache.spark.examples.mllib;
0019 
0020 // $example on$
0021 import java.util.Arrays;
0022 import java.util.List;
0023 
0024 import scala.Tuple2;
0025 
0026 import org.apache.spark.api.java.*;
0027 import org.apache.spark.mllib.evaluation.MultilabelMetrics;
0028 import org.apache.spark.SparkConf;
0029 // $example off$
0030 
0031 public class JavaMultiLabelClassificationMetricsExample {
0032   public static void main(String[] args) {
0033     SparkConf conf = new SparkConf().setAppName("Multilabel Classification Metrics Example");
0034     JavaSparkContext sc = new JavaSparkContext(conf);
0035     // $example on$
0036     List<Tuple2<double[], double[]>> data = Arrays.asList(
0037       new Tuple2<>(new double[]{0.0, 1.0}, new double[]{0.0, 2.0}),
0038       new Tuple2<>(new double[]{0.0, 2.0}, new double[]{0.0, 1.0}),
0039       new Tuple2<>(new double[]{}, new double[]{0.0}),
0040       new Tuple2<>(new double[]{2.0}, new double[]{2.0}),
0041       new Tuple2<>(new double[]{2.0, 0.0}, new double[]{2.0, 0.0}),
0042       new Tuple2<>(new double[]{0.0, 1.0, 2.0}, new double[]{0.0, 1.0}),
0043       new Tuple2<>(new double[]{1.0}, new double[]{1.0, 2.0})
0044     );
0045     JavaRDD<Tuple2<double[], double[]>> scoreAndLabels = sc.parallelize(data);
0046 
0047     // Instantiate metrics object
0048     MultilabelMetrics metrics = new MultilabelMetrics(scoreAndLabels.rdd());
0049 
0050     // Summary stats
0051     System.out.format("Recall = %f\n", metrics.recall());
0052     System.out.format("Precision = %f\n", metrics.precision());
0053     System.out.format("F1 measure = %f\n", metrics.f1Measure());
0054     System.out.format("Accuracy = %f\n", metrics.accuracy());
0055 
0056     // Stats by labels
0057     for (int i = 0; i < metrics.labels().length - 1; i++) {
0058       System.out.format("Class %1.1f precision = %f\n", metrics.labels()[i], metrics.precision(
0059         metrics.labels()[i]));
0060       System.out.format("Class %1.1f recall = %f\n", metrics.labels()[i], metrics.recall(
0061         metrics.labels()[i]));
0062       System.out.format("Class %1.1f F1 score = %f\n", metrics.labels()[i], metrics.f1Measure(
0063         metrics.labels()[i]));
0064     }
0065 
0066     // Micro stats
0067     System.out.format("Micro recall = %f\n", metrics.microRecall());
0068     System.out.format("Micro precision = %f\n", metrics.microPrecision());
0069     System.out.format("Micro F1 measure = %f\n", metrics.microF1Measure());
0070 
0071     // Hamming loss
0072     System.out.format("Hamming loss = %f\n", metrics.hammingLoss());
0073 
0074     // Subset accuracy
0075     System.out.format("Subset accuracy = %f\n", metrics.subsetAccuracy());
0076     // $example off$
0077 
0078     sc.stop();
0079   }
0080 }