Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  *
0009  *    http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  */
0017 
0018 package org.apache.spark.examples.ml;
0019 
0020 import org.apache.spark.sql.SparkSession;
0021 
0022 // $example on$
0023 import java.util.Arrays;
0024 import java.util.List;
0025 
0026 import org.apache.spark.ml.feature.MinHashLSH;
0027 import org.apache.spark.ml.feature.MinHashLSHModel;
0028 import org.apache.spark.ml.linalg.Vector;
0029 import org.apache.spark.ml.linalg.VectorUDT;
0030 import org.apache.spark.ml.linalg.Vectors;
0031 import org.apache.spark.sql.Dataset;
0032 import org.apache.spark.sql.Row;
0033 import org.apache.spark.sql.RowFactory;
0034 import org.apache.spark.sql.types.DataTypes;
0035 import org.apache.spark.sql.types.Metadata;
0036 import org.apache.spark.sql.types.StructField;
0037 import org.apache.spark.sql.types.StructType;
0038 
0039 import static org.apache.spark.sql.functions.col;
0040 // $example off$
0041 
0042 /**
0043  * An example demonstrating MinHashLSH.
0044  * Run with:
0045  *   bin/run-example ml.JavaMinHashLSHExample
0046  */
0047 public class JavaMinHashLSHExample {
0048   public static void main(String[] args) {
0049     SparkSession spark = SparkSession
0050       .builder()
0051       .appName("JavaMinHashLSHExample")
0052       .getOrCreate();
0053 
0054     // $example on$
0055     List<Row> dataA = Arrays.asList(
0056       RowFactory.create(0, Vectors.sparse(6, new int[]{0, 1, 2}, new double[]{1.0, 1.0, 1.0})),
0057       RowFactory.create(1, Vectors.sparse(6, new int[]{2, 3, 4}, new double[]{1.0, 1.0, 1.0})),
0058       RowFactory.create(2, Vectors.sparse(6, new int[]{0, 2, 4}, new double[]{1.0, 1.0, 1.0}))
0059     );
0060 
0061     List<Row> dataB = Arrays.asList(
0062       RowFactory.create(0, Vectors.sparse(6, new int[]{1, 3, 5}, new double[]{1.0, 1.0, 1.0})),
0063       RowFactory.create(1, Vectors.sparse(6, new int[]{2, 3, 5}, new double[]{1.0, 1.0, 1.0})),
0064       RowFactory.create(2, Vectors.sparse(6, new int[]{1, 2, 4}, new double[]{1.0, 1.0, 1.0}))
0065     );
0066 
0067     StructType schema = new StructType(new StructField[]{
0068       new StructField("id", DataTypes.IntegerType, false, Metadata.empty()),
0069       new StructField("features", new VectorUDT(), false, Metadata.empty())
0070     });
0071     Dataset<Row> dfA = spark.createDataFrame(dataA, schema);
0072     Dataset<Row> dfB = spark.createDataFrame(dataB, schema);
0073 
0074     int[] indices = {1, 3};
0075     double[] values = {1.0, 1.0};
0076     Vector key = Vectors.sparse(6, indices, values);
0077 
0078     MinHashLSH mh = new MinHashLSH()
0079       .setNumHashTables(5)
0080       .setInputCol("features")
0081       .setOutputCol("hashes");
0082 
0083     MinHashLSHModel model = mh.fit(dfA);
0084 
0085     // Feature Transformation
0086     System.out.println("The hashed dataset where hashed values are stored in the column 'hashes':");
0087     model.transform(dfA).show();
0088 
0089     // Compute the locality sensitive hashes for the input rows, then perform approximate
0090     // similarity join.
0091     // We could avoid computing hashes by passing in the already-transformed dataset, e.g.
0092     // `model.approxSimilarityJoin(transformedA, transformedB, 0.6)`
0093     System.out.println("Approximately joining dfA and dfB on Jaccard distance smaller than 0.6:");
0094     model.approxSimilarityJoin(dfA, dfB, 0.6, "JaccardDistance")
0095       .select(col("datasetA.id").alias("idA"),
0096         col("datasetB.id").alias("idB"),
0097         col("JaccardDistance")).show();
0098 
0099     // Compute the locality sensitive hashes for the input rows, then perform approximate nearest
0100     // neighbor search.
0101     // We could avoid computing hashes by passing in the already-transformed dataset, e.g.
0102     // `model.approxNearestNeighbors(transformedA, key, 2)`
0103     // It may return less than 2 rows when not enough approximate near-neighbor candidates are
0104     // found.
0105     System.out.println("Approximately searching dfA for 2 nearest neighbors of the key:");
0106     model.approxNearestNeighbors(dfA, key, 2).show();
0107     // $example off$
0108 
0109     spark.stop();
0110   }
0111 }