Github user sethah commented on a diff in the pull request:
https://github.com/apache/spark/pull/16715#discussion_r99923657
 Diff: python/pyspark/ml/feature.py 
@@ 120,6 +122,200 @@ def getThreshold(self):
return self.getOrDefault(self.threshold)
+class LSHParams(Params):
+ """
+ Mixin for Locality Sensitive Hashing(LSH) algorithm parameters.
+ """
+
+ numHashTables = Param(Params._dummy(), "numHashTables", "number of hash tables, where
" +
+ "increasing number of hash tables lowers the false negative
rate, " +
+ "and decreasing it improves the running performance.",
+ typeConverter=TypeConverters.toInt)
+
+ def __init__(self):
+ super(LSHParams, self).__init__()
+
+ @since("2.2.0")
+ def setNumHashTables(self, value):
+ """
+ Sets the value of :py:attr:`numHashTables`.
+ """
+ return self._set(numHashTables=value)
+
+ @since("2.2.0")
+ def getNumHashTables(self):
+ """
+ Gets the value of numHashTables or its default value.
+ """
+ return self.getOrDefault(self.numHashTables)
+
+
+class LSHModel():
+ """
+ Mixin for Locality Sensitive Hashing(LSH) models.
+ """
+
+ @since("2.2.0")
+ def approxNearestNeighbors(self, dataset, key, numNearestNeighbors, singleProbing=True,
+ distCol="distCol"):
+ """
+ Given a large dataset and an item, approximately find at most k items which have
the
+ closest distance to the item. If the :py:attr:`outputCol` is missing, the method
will
+ transform the data; if the :py:attr:`outputCol` exists, it will use that. This
allows
+ caching of the transformed data when necessary.
+
+ :param dataset: The dataset to search for nearest neighbors of the key.
+ :param key: Feature vector representing the item to search for.
+ :param numNearestNeighbors: The maximum number of nearest neighbors.
+ :param distCol: Output column for storing the distance between each result row
and the key.
+ Use "distCol" as default value if it's not specified.
+ :return: A dataset containing at most k items closest to the key. A distCol is
added
+ to show the distance between each row and the key.
+ """
+ return self._call_java("approxNearestNeighbors", dataset, key, numNearestNeighbors,
+ distCol)
+
+ @since("2.2.0")
+ def approxSimilarityJoin(self, datasetA, datasetB, threshold, distCol="distCol"):
+ """
+ Join two dataset to approximately find all pairs of rows whose distance are smaller
than
+ the threshold. If the :py:attr:`outputCol` is missing, the method will transform
the data;
+ if the :py:attr:`outputCol` exists, it will use that. This allows caching of
the
+ transformed data when necessary.
+
+ :param datasetA: One of the datasets to join.
+ :param datasetB: Another dataset to join.
+ :param threshold: The threshold for the distance of row pairs.
+ :param distCol: Output column for storing the distance between each result row
and the key.
+ Use "distCol" as default value if it's not specified.
+ :return: A joined dataset containing pairs of rows. The original rows are in
columns
+ "datasetA" and "datasetB", and a distCol is added to show the distance
of
+ each pair.
+ """
+ return self._call_java("approxSimilarityJoin", datasetA, datasetB, threshold,
distCol)
+
+
+@inherit_doc
+class BucketedRandomProjectionLSH(JavaEstimator, LSHParams, HasInputCol, HasOutputCol,
HasSeed,
+ JavaMLReadable, JavaMLWritable):
+ """
+ .. note:: Experimental
+
+ LSH class for Euclidean distance metrics.
+ The input is dense or sparse vectors, each of which represents a point in the Euclidean
+ distance space. The output will be vectors of configurable dimension. Hash value
in the
+ same dimension is calculated by the same hash function.
+
+ .. seealso:: `Stable Distributions \
+ <https://en.wikipedia.org/wiki/Localitysensitive_hashing#Stable_distributions>`_
+ .. seealso:: `Hashing for Similarity Search: A Survey <https://arxiv.org/abs/1408.2927>`_
+
+ >>> from pyspark.ml.linalg import Vectors
+ >>> data = [(Vectors.dense([1.0, 1.0 ]),),
+ ... (Vectors.dense([1.0, 1.0 ]),),
+ ... (Vectors.dense([1.0, 1.0 ]),),
+ ... (Vectors.dense([1.0, 1.0]),)]
+ >>> df = spark.createDataFrame(data, ["keys"])
+ >>> rp = BucketedRandomProjectionLSH(inputCol="keys", outputCol="values",
+ ... seed=12345, bucketLength=1.0)
+ >>> model = rp.fit(df)
+ >>> model.randUnitVectors
+ [DenseVector([0.3041, 0.9527])]
+ >>> model.transform(df).head()
+ Row(keys=DenseVector([1.0, 1.0]), values=[DenseVector([1.0])])
+ >>> data2 = [(Vectors.dense([2.0, 2.0 ]),),
+ ... (Vectors.dense([2.0, 3.0 ]),),
+ ... (Vectors.dense([3.0, 2.0 ]),),
+ ... (Vectors.dense([3.0, 3.0]),)]
+ >>> df2 = spark.createDataFrame(data2, ["keys"])
+ >>> model.approxNearestNeighbors(df2, Vectors.dense([1.0, 2.0]), 1).collect()
+ [Row(keys=DenseVector([2.0, 2.0]), values=[DenseVector([1.0])], distCol=1.0)]
+ >>> model.approxSimilarityJoin(df, df2, 3.0).select("distCol").head()[0]
 End diff 
Since doctests are also partly used to demonstrate the usage of the algorithm, I don't
think this line is particularly useful. It is quite hard to interpret. I think it might be
nicer to add an "id" column to the dataframes and then do a "show" here to see the joined
dataframes, as in the Scala example. Then again, you end up with:
````
++++
 datasetA datasetB distCol
++++
[[1.0,1.0],Wrappe...[[3.0,2.0],Wrappe...2.23606797749979
++++
````
Which is also confusing! Thoughts on which option is better?

If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.


To unsubscribe, email: reviewsunsubscribe@spark.apache.org
For additional commands, email: reviewshelp@spark.apache.org
