|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +# contributor license agreements. See the NOTICE file distributed with |
| 4 | +# this work for additional information regarding copyright ownership. |
| 5 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +# (the "License"); you may not use this file except in compliance with |
| 7 | +# the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +from pyspark.ml.wrapper import JavaEvaluator |
| 19 | +from pyspark.ml.param import Param, Params |
| 20 | +from pyspark.ml.param.shared import HasLabelCol, HasRawPredictionCol |
| 21 | +from pyspark.ml.util import keyword_only |
| 22 | + |
| 23 | +__all__ = ['BinaryClassificationEvaluator'] |
| 24 | + |
| 25 | + |
| 26 | +class BinaryClassificationEvaluator(JavaEvaluator, HasLabelCol, HasRawPredictionCol): |
| 27 | + """ |
| 28 | + Evaluator for binary classification, which expects two input |
| 29 | + columns: rawPrediction and label. |
| 30 | +
|
| 31 | + >>> from pyspark.mllib.linalg import Vectors |
| 32 | + >>> scoreAndLabels = sc.parallelize([ |
| 33 | + ... (0.1, 0.0), (0.1, 1.0), (0.4, 0.0), (0.6, 0.0), (0.6, 1.0), (0.6, 1.0), (0.8, 1.0)]) |
| 34 | + >>> rawPredictionAndLabels = scoreAndLabels.map( |
| 35 | + ... lambda x: (Vectors.dense([1.0 - x[0], x[0]]), x[1])) |
| 36 | + >>> dataset = rawPredictionAndLabels.toDF(["raw", "label"]) |
| 37 | + >>> evaluator = BinaryClassificationEvaluator(rawPredictionCol="raw") |
| 38 | + >>> evaluator.evaluate(dataset) |
| 39 | + 0.70... |
| 40 | + >>> evaluator.evaluate(dataset, {evaluator.metricName: "areaUnderPR"}) |
| 41 | + 0.83... |
| 42 | + """ |
| 43 | + |
| 44 | + _java_class = "org.apache.spark.ml.evaluation.BinaryClassificationEvaluator" |
| 45 | + |
| 46 | + # a placeholder to make it appear in the generated doc |
| 47 | + metricName = Param(Params._dummy(), "metricName", |
| 48 | + "metric name in evaluation (areaUnderROC|areaUnderPR)") |
| 49 | + |
| 50 | + @keyword_only |
| 51 | + def __init__(self, rawPredictionCol="rawPrediction", labelCol="label", |
| 52 | + metricName="areaUnderROC"): |
| 53 | + """ |
| 54 | + __init__(self, rawPredictionCol="rawPrediction", labelCol="label", |
| 55 | + metricName="areaUnderROC") |
| 56 | + """ |
| 57 | + super(BinaryClassificationEvaluator, self).__init__() |
| 58 | + #: param for metric name in evaluation (areaUnderROC|areaUnderPR) |
| 59 | + self.metricName = Param(self, "metricName", |
| 60 | + "metric name in evaluation (areaUnderROC|areaUnderPR)") |
| 61 | + self._setDefault(rawPredictionCol="rawPrediction", labelCol="label", |
| 62 | + metricName="areaUnderROC") |
| 63 | + kwargs = self.__init__._input_kwargs |
| 64 | + self._set(**kwargs) |
| 65 | + |
| 66 | + def setMetricName(self, value): |
| 67 | + """ |
| 68 | + Sets the value of :py:attr:`metricName`. |
| 69 | + """ |
| 70 | + self.paramMap[self.metricName] = value |
| 71 | + return self |
| 72 | + |
| 73 | + def getMetricName(self): |
| 74 | + """ |
| 75 | + Gets the value of metricName or its default value. |
| 76 | + """ |
| 77 | + return self.getOrDefault(self.metricName) |
| 78 | + |
| 79 | + @keyword_only |
| 80 | + def setParams(self, rawPredictionCol="rawPrediction", labelCol="label", |
| 81 | + metricName="areaUnderROC"): |
| 82 | + """ |
| 83 | + setParams(self, rawPredictionCol="rawPrediction", labelCol="label", |
| 84 | + metricName="areaUnderROC") |
| 85 | + Sets params for binary classification evaluator. |
| 86 | + """ |
| 87 | + kwargs = self.setParams._input_kwargs |
| 88 | + return self._set(**kwargs) |
| 89 | + |
| 90 | + |
| 91 | +if __name__ == "__main__": |
| 92 | + import doctest |
| 93 | + from pyspark.context import SparkContext |
| 94 | + from pyspark.sql import SQLContext |
| 95 | + globs = globals().copy() |
| 96 | + # The small batch size here ensures that we see multiple batches, |
| 97 | + # even in these small test examples: |
| 98 | + sc = SparkContext("local[2]", "ml.feature tests") |
| 99 | + sqlContext = SQLContext(sc) |
| 100 | + globs['sc'] = sc |
| 101 | + globs['sqlContext'] = sqlContext |
| 102 | + (failure_count, test_count) = doctest.testmod( |
| 103 | + globs=globs, optionflags=doctest.ELLIPSIS) |
| 104 | + sc.stop() |
| 105 | + if failure_count: |
| 106 | + exit(-1) |
0 commit comments