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 # $example on$
0019 from pyspark.mllib.evaluation import MultilabelMetrics
0020 # $example off$
0021 from pyspark import SparkContext
0022 
0023 if __name__ == "__main__":
0024     sc = SparkContext(appName="MultiLabelMetricsExample")
0025     # $example on$
0026     scoreAndLabels = sc.parallelize([
0027         ([0.0, 1.0], [0.0, 2.0]),
0028         ([0.0, 2.0], [0.0, 1.0]),
0029         ([], [0.0]),
0030         ([2.0], [2.0]),
0031         ([2.0, 0.0], [2.0, 0.0]),
0032         ([0.0, 1.0, 2.0], [0.0, 1.0]),
0033         ([1.0], [1.0, 2.0])])
0034 
0035     # Instantiate metrics object
0036     metrics = MultilabelMetrics(scoreAndLabels)
0037 
0038     # Summary stats
0039     print("Recall = %s" % metrics.recall())
0040     print("Precision = %s" % metrics.precision())
0041     print("F1 measure = %s" % metrics.f1Measure())
0042     print("Accuracy = %s" % metrics.accuracy)
0043 
0044     # Individual label stats
0045     labels = scoreAndLabels.flatMap(lambda x: x[1]).distinct().collect()
0046     for label in labels:
0047         print("Class %s precision = %s" % (label, metrics.precision(label)))
0048         print("Class %s recall = %s" % (label, metrics.recall(label)))
0049         print("Class %s F1 Measure = %s" % (label, metrics.f1Measure(label)))
0050 
0051     # Micro stats
0052     print("Micro precision = %s" % metrics.microPrecision)
0053     print("Micro recall = %s" % metrics.microRecall)
0054     print("Micro F1 measure = %s" % metrics.microF1Measure)
0055 
0056     # Hamming loss
0057     print("Hamming loss = %s" % metrics.hammingLoss)
0058 
0059     # Subset accuracy
0060     print("Subset accuracy = %s" % metrics.subsetAccuracy)
0061     # $example off$