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.BucketedRandomProjectionLSH;
0027 import org.apache.spark.ml.feature.BucketedRandomProjectionLSHModel;
0028 import org.apache.spark.ml.linalg.Vector;
0029 import org.apache.spark.ml.linalg.Vectors;
0030 import org.apache.spark.ml.linalg.VectorUDT;
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 BucketedRandomProjectionLSH.
0044  * Run with:
0045  *   bin/run-example ml.JavaBucketedRandomProjectionLSHExample
0046  */
0047 public class JavaBucketedRandomProjectionLSHExample {
0048   public static void main(String[] args) {
0049     SparkSession spark = SparkSession
0050       .builder()
0051       .appName("JavaBucketedRandomProjectionLSHExample")
0052       .getOrCreate();
0053 
0054     // $example on$
0055     List<Row> dataA = Arrays.asList(
0056       RowFactory.create(0, Vectors.dense(1.0, 1.0)),
0057       RowFactory.create(1, Vectors.dense(1.0, -1.0)),
0058       RowFactory.create(2, Vectors.dense(-1.0, -1.0)),
0059       RowFactory.create(3, Vectors.dense(-1.0, 1.0))
0060     );
0061 
0062     List<Row> dataB = Arrays.asList(
0063         RowFactory.create(4, Vectors.dense(1.0, 0.0)),
0064         RowFactory.create(5, Vectors.dense(-1.0, 0.0)),
0065         RowFactory.create(6, Vectors.dense(0.0, 1.0)),
0066         RowFactory.create(7, Vectors.dense(0.0, -1.0))
0067     );
0068 
0069     StructType schema = new StructType(new StructField[]{
0070       new StructField("id", DataTypes.IntegerType, false, Metadata.empty()),
0071       new StructField("features", new VectorUDT(), false, Metadata.empty())
0072     });
0073     Dataset<Row> dfA = spark.createDataFrame(dataA, schema);
0074     Dataset<Row> dfB = spark.createDataFrame(dataB, schema);
0075 
0076     Vector key = Vectors.dense(1.0, 0.0);
0077 
0078     BucketedRandomProjectionLSH mh = new BucketedRandomProjectionLSH()
0079       .setBucketLength(2.0)
0080       .setNumHashTables(3)
0081       .setInputCol("features")
0082       .setOutputCol("hashes");
0083 
0084     BucketedRandomProjectionLSHModel model = mh.fit(dfA);
0085 
0086     // Feature Transformation
0087     System.out.println("The hashed dataset where hashed values are stored in the column 'hashes':");
0088     model.transform(dfA).show();
0089 
0090     // Compute the locality sensitive hashes for the input rows, then perform approximate
0091     // similarity join.
0092     // We could avoid computing hashes by passing in the already-transformed dataset, e.g.
0093     // `model.approxSimilarityJoin(transformedA, transformedB, 1.5)`
0094     System.out.println("Approximately joining dfA and dfB on distance smaller than 1.5:");
0095     model.approxSimilarityJoin(dfA, dfB, 1.5, "EuclideanDistance")
0096       .select(col("datasetA.id").alias("idA"),
0097         col("datasetB.id").alias("idB"),
0098         col("EuclideanDistance")).show();
0099 
0100     // Compute the locality sensitive hashes for the input rows, then perform approximate nearest
0101     // neighbor search.
0102     // We could avoid computing hashes by passing in the already-transformed dataset, e.g.
0103     // `model.approxNearestNeighbors(transformedA, key, 2)`
0104     System.out.println("Approximately searching dfA for 2 nearest neighbors of the key:");
0105     model.approxNearestNeighbors(dfA, key, 2).show();
0106     // $example off$
0107 
0108     spark.stop();
0109   }
0110 }