本文简要介绍
pyspark.ml.feature.BucketedRandomProjectionLSH
的用法。用法:
class pyspark.ml.feature.BucketedRandomProjectionLSH(*, inputCol=None, outputCol=None, seed=None, numHashTables=1, bucketLength=None)
欧几里得距离度量的 LSH 类。输入是密集或稀疏向量,每个向量代表欧几里得距离空间中的一个点。输出将是可配置维度的向量。同一维度的哈希值由相同的哈希函数计算得出。
2.2.0 版中的新函数。
注意:
例子:
>>> from pyspark.ml.linalg import Vectors >>> from pyspark.sql.functions import col >>> data = [(0, Vectors.dense([-1.0, -1.0 ]),), ... (1, Vectors.dense([-1.0, 1.0 ]),), ... (2, Vectors.dense([1.0, -1.0 ]),), ... (3, Vectors.dense([1.0, 1.0]),)] >>> df = spark.createDataFrame(data, ["id", "features"]) >>> brp = BucketedRandomProjectionLSH() >>> brp.setInputCol("features") BucketedRandomProjectionLSH... >>> brp.setOutputCol("hashes") BucketedRandomProjectionLSH... >>> brp.setSeed(12345) BucketedRandomProjectionLSH... >>> brp.setBucketLength(1.0) BucketedRandomProjectionLSH... >>> model = brp.fit(df) >>> model.getBucketLength() 1.0 >>> model.setOutputCol("hashes") BucketedRandomProjectionLSHModel... >>> model.transform(df).head() Row(id=0, features=DenseVector([-1.0, -1.0]), hashes=[DenseVector([-1.0])]) >>> data2 = [(4, Vectors.dense([2.0, 2.0 ]),), ... (5, Vectors.dense([2.0, 3.0 ]),), ... (6, Vectors.dense([3.0, 2.0 ]),), ... (7, Vectors.dense([3.0, 3.0]),)] >>> df2 = spark.createDataFrame(data2, ["id", "features"]) >>> model.approxNearestNeighbors(df2, Vectors.dense([1.0, 2.0]), 1).collect() [Row(id=4, features=DenseVector([2.0, 2.0]), hashes=[DenseVector([1.0])], distCol=1.0)] >>> model.approxSimilarityJoin(df, df2, 3.0, distCol="EuclideanDistance").select( ... col("datasetA.id").alias("idA"), ... col("datasetB.id").alias("idB"), ... col("EuclideanDistance")).show() +---+---+-----------------+ |idA|idB|EuclideanDistance| +---+---+-----------------+ | 3| 6| 2.23606797749979| +---+---+-----------------+ ... >>> model.approxSimilarityJoin(df, df2, 3, distCol="EuclideanDistance").select( ... col("datasetA.id").alias("idA"), ... col("datasetB.id").alias("idB"), ... col("EuclideanDistance")).show() +---+---+-----------------+ |idA|idB|EuclideanDistance| +---+---+-----------------+ | 3| 6| 2.23606797749979| +---+---+-----------------+ ... >>> brpPath = temp_path + "/brp" >>> brp.save(brpPath) >>> brp2 = BucketedRandomProjectionLSH.load(brpPath) >>> brp2.getBucketLength() == brp.getBucketLength() True >>> modelPath = temp_path + "/brp-model" >>> model.save(modelPath) >>> model2 = BucketedRandomProjectionLSHModel.load(modelPath) >>> model.transform(df).head().hashes == model2.transform(df).head().hashes True
相关用法
- Python pyspark Bucketizer用法及代码示例
- Python pyspark BlockMatrix.add用法及代码示例
- Python pyspark BisectingKMeans用法及代码示例
- Python pyspark BlockMatrix.colsPerBlock用法及代码示例
- Python pyspark BlockMatrix.subtract用法及代码示例
- Python pyspark BlockMatrix.toLocalMatrix用法及代码示例
- Python pyspark BlockMatrix.toIndexedRowMatrix用法及代码示例
- Python pyspark BlockMatrix.rowsPerBlock用法及代码示例
- Python pyspark BlockMatrix.numCols用法及代码示例
- Python pyspark BlockMatrix.numColBlocks用法及代码示例
- Python pyspark BlockMatrix.numRowBlocks用法及代码示例
- Python pyspark BinaryClassificationEvaluator用法及代码示例
- Python pyspark BlockMatrix.toCoordinateMatrix用法及代码示例
- Python pyspark BinaryClassificationMetrics用法及代码示例
- Python pyspark BlockMatrix.transpose用法及代码示例
- Python pyspark BisectingKMeansModel用法及代码示例
- Python pyspark BlockMatrix.numRows用法及代码示例
- Python pyspark BlockMatrix.multiply用法及代码示例
- Python pyspark Broadcast用法及代码示例
- Python pyspark BlockMatrix.blocks用法及代码示例
- Python pyspark Binarizer用法及代码示例
- Python pyspark create_map用法及代码示例
- Python pyspark date_add用法及代码示例
- Python pyspark DataFrame.to_latex用法及代码示例
- Python pyspark DataStreamReader.schema用法及代码示例
注:本文由纯净天空筛选整理自spark.apache.org大神的英文原创作品 pyspark.ml.feature.BucketedRandomProjectionLSH。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。