Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
address comments
  • Loading branch information
WeichenXu123 committed Apr 13, 2018
commit 20968c1101d7c19bd81bf561e47e6b477fe0a19a
33 changes: 22 additions & 11 deletions python/pyspark/ml/stat.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,12 +308,12 @@ def normL2(col, weightCol=None):
return Summarizer._get_single_metric(col, weightCol, "normL2")

@staticmethod
def _check_param(featureCol, weightCol):
def _check_param(featuresCol, weightCol):
if weightCol is None:
weightCol = lit(1.0)
if not isinstance(featureCol, Column) or not isinstance(weightCol, Column):
if not isinstance(featuresCol, Column) or not isinstance(weightCol, Column):
raise TypeError("featureCol and weightCol should be a Column")
return featureCol, weightCol
return featuresCol, weightCol

@staticmethod
def _get_single_metric(col, weightCol, metric):
Expand All @@ -339,26 +339,28 @@ def metrics(*metrics):
- normL2: the Euclidian norm for each coefficient.
- normL1: the L1 norm of each coefficient (sum of the absolute values).

:param metrics metrics that can be provided.
:return a Summarizer
:param metrics:
metrics that can be provided.
:return:
an object of :py:class:`pyspark.ml.stat.SummaryBuilder`

Note: Currently, the performance of this interface is about 2x~3x slower then using the RDD
interface.
"""
sc = SparkContext._active_spark_context
js = JavaWrapper._new_java_obj("org.apache.spark.ml.stat.Summarizer.metrics",
_to_seq(sc, metrics))
return SummarizerBuilder(js)
return SummaryBuilder(js)


class SummarizerBuilder(object):
class SummaryBuilder(JavaWrapper):
"""
.. note:: Experimental

A builder object that provides summary statistics about a given column.

Users should not directly create such builders, but instead use one of the methods in
:py:class:`pyspark.ml.stat.Summary`
:py:class:`pyspark.ml.stat.Summarizer`

.. versionadded:: 2.4.0

Expand All @@ -367,13 +369,22 @@ def __init__(self, js):
self._js = js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should call the super's init method, and it should store js in _java_obj (which is set in the JavaWrapper init).


@since("2.4.0")
def summary(self, featureCol, weightCol=None):
def summary(self, featuresCol, weightCol=None):
"""
Returns an aggregate object that contains the summary of the column with the requested
metrics.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's copy the docs for arguments & return value from Scala


:param featuresCol:
a column that contains features Vector object.
:param weightCol
a column that contains weight value. Default weight is 1.0.
:return:
an aggregate column that contains the statistics. The exact content of this
structure is determined during the creation of the builder.

"""
featureCol, weightCol = Summarizer._check_param(featureCol, weightCol)
return Column(self._js.summary(featureCol._jc, weightCol._jc))
featuresCol, weightCol = Summarizer._check_param(featuresCol, weightCol)
return Column(self._js.summary(featuresCol._jc, weightCol._jc))


if __name__ == "__main__":
Expand Down