Github user sethah commented on a diff in the pull request:
https://github.com/apache/spark/pull/16715#discussion_r100971720
--- Diff: python/pyspark/ml/feature.py ---
@@ -755,6 +945,103 @@ def maxAbs(self):
@inherit_doc
+class MinHashLSH(JavaEstimator, LSHParams, HasInputCol, HasOutputCol,
HasSeed,
+ JavaMLReadable, JavaMLWritable):
+
+ """
+ .. note:: Experimental
+
+ LSH class for Jaccard distance.
+ The input can be dense or sparse vectors, but it is more efficient if
it is sparse.
+ For example, `Vectors.sparse(10, [(2, 1.0), (3, 1.0), (5, 1.0)])`
means there are 10 elements
+ in the space. This set contains elements 2, 3, and 5. Also, any input
vector must have at
+ least 1 non-zero index, and all non-zero values are treated as binary
"1" values.
+
+ .. seealso:: `Wikipedia on MinHash
<https://en.wikipedia.org/wiki/MinHash>`_
+
+ >>> from pyspark.ml.linalg import Vectors
+ >>> from pyspark.sql.functions import col
+ >>> data = [(0, Vectors.sparse(6, [0, 1, 2], [1.0, 1.0, 1.0]),),
+ ... (1, Vectors.sparse(6, [2, 3, 4], [1.0, 1.0, 1.0]),),
+ ... (2, Vectors.sparse(6, [0, 2, 4], [1.0, 1.0, 1.0]),)]
+ >>> df = spark.createDataFrame(data, ["id", "features"])
+ >>> mh = MinHashLSH(inputCol="features", outputCol="hashes",
seed=12345)
+ >>> model = mh.fit(df)
+ >>> model.transform(df).head()
+ Row(id=0, features=SparseVector(6, {0: 1.0, 1: 1.0, 2: 1.0}),
hashes=[DenseVector([-1638925...
+ >>> data2 = [(3, Vectors.sparse(6, [1, 3, 5], [1.0, 1.0, 1.0]),),
+ ... (4, Vectors.sparse(6, [2, 3, 5], [1.0, 1.0, 1.0]),),
+ ... (5, Vectors.sparse(6, [1, 2, 4], [1.0, 1.0, 1.0]),)]
+ >>> df2 = spark.createDataFrame(data2, ["id", "features"])
+ >>> key = Vectors.sparse(6, [1, 2], [1.0, 1.0])
+ >>> model.approxNearestNeighbors(df2, key, 1).collect()
+ [Row(id=5, features=SparseVector(6, {1: 1.0, 2: 1.0, 4: 1.0}),
hashes=[DenseVector([-163892...
+ >>> model.approxSimilarityJoin(df, df2, 0.6,
distCol="JaccardDistance").select(
+ ... col("datasetA.id").alias("idA"),
+ ... col("datasetB.id").alias("idB"),
+ ... col("JaccardDistance")).show()
+ +---+---+---------------+
+ |idA|idB|JaccardDistance|
+ +---+---+---------------+
+ | 1| 4| 0.5|
+ | 0| 5| 0.5|
+ +---+---+---------------+
+ ...
+ >>> mhPath = temp_path + "/mh"
+ >>> mh.save(mhPath)
+ >>> mh2 = MinHashLSH.load(mhPath)
+ >>> mh2.getOutputCol() == mh.getOutputCol()
+ True
+ >>> modelPath = temp_path + "/mh-model"
+ >>> model.save(modelPath)
+ >>> model2 = MinHashLSHModel.load(modelPath)
--- End diff --
let's add an equality check here and for BRP. For example for IDFModel we
have:
`loadedModel.transform(df).head().idf == model.transform(df).head().idf`
---
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 [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]