imatiach-msft commented on a change in pull request #23157:
[SPARK-26185][PYTHON]add weightCol in python MulticlassClassificationEvaluator
URL: https://github.com/apache/spark/pull/23157#discussion_r251622289
##########
File path: python/pyspark/mllib/evaluation.py
##########
@@ -192,16 +192,53 @@ class MulticlassMetrics(JavaModelWrapper):
0.66...
>>> metrics.weightedFMeasure(2.0)
0.65...
+ >>> predAndLabelsWithOptWeight = sc.parallelize([(0.0, 0.0, 1.0), (0.0,
1.0, 1.0),
+ ... (0.0, 0.0, 1.0), (1.0, 0.0, 1.0), (1.0, 1.0, 1.0), (1.0, 1.0,
1.0), (1.0, 1.0, 1.0),
+ ... (2.0, 2.0, 1.0), (2.0, 0.0, 1.0)])
+ >>> metrics = MulticlassMetrics(predAndLabelsWithOptWeight)
+ >>> metrics.confusionMatrix().toArray()
+ array([[ 2., 1., 1.],
+ [ 1., 3., 0.],
+ [ 0., 0., 1.]])
+ >>> metrics.falsePositiveRate(0.0)
+ 0.2...
+ >>> metrics.precision(1.0)
+ 0.75...
+ >>> metrics.recall(2.0)
+ 1.0...
+ >>> metrics.fMeasure(0.0, 2.0)
+ 0.52...
+ >>> metrics.accuracy
+ 0.66...
+ >>> metrics.weightedFalsePositiveRate
+ 0.19...
+ >>> metrics.weightedPrecision
+ 0.68...
+ >>> metrics.weightedRecall
+ 0.66...
+ >>> metrics.weightedFMeasure()
+ 0.66...
+ >>> metrics.weightedFMeasure(2.0)
+ 0.65...
.. versionadded:: 1.4.0
"""
- def __init__(self, predictionAndLabels):
- sc = predictionAndLabels.ctx
+ def __init__(self, predAndLabelsWithOptWeight):
+ sc = predAndLabelsWithOptWeight.ctx
sql_ctx = SQLContext.getOrCreate(sc)
- df = sql_ctx.createDataFrame(predictionAndLabels, schema=StructType([
- StructField("prediction", DoubleType(), nullable=False),
- StructField("label", DoubleType(), nullable=False)]))
+ numCol = len(predAndLabelsWithOptWeight.first())
+ schema = None
+ if (numCol == 2):
Review comment:
minor: you can refactor out the StructField's below to variables
(prediction, label) or create an initial list and append the weight to it (to
limit duplicate code, as first two StructFields are exactly the same)
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]