Github user jkbradley commented on a diff in the pull request:

    https://github.com/apache/spark/pull/15148#discussion_r80555938
  
    --- Diff: mllib/src/main/scala/org/apache/spark/ml/feature/lsh/LSH.scala ---
    @@ -0,0 +1,290 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.ml.feature.lsh
    +
    +import scala.util.Random
    +
    +import org.apache.spark.ml.{Estimator, Model}
    +import org.apache.spark.ml.linalg.{Vector, VectorUDT}
    +import org.apache.spark.ml.param.{IntParam, ParamMap, ParamValidators}
    +import org.apache.spark.ml.param.shared.{HasInputCol, HasOutputCol}
    +import org.apache.spark.sql._
    +import org.apache.spark.sql.expressions.UserDefinedFunction
    +import org.apache.spark.sql.functions._
    +import org.apache.spark.sql.types._
    +
    +/**
    + * Params for [[LSH]].
    + */
    +private[ml] trait LSHParams extends HasInputCol with HasOutputCol {
    +  /**
    +   * Param for output dimension.
    +   *
    +   * @group param
    +   */
    +  final val outputDim: IntParam = new IntParam(this, "outputDim", "output 
dimension",
    +    ParamValidators.gt(0))
    +
    +  /** @group getParam */
    +  final def getOutputDim: Int = $(outputDim)
    +
    +  setDefault(outputDim -> 1)
    +
    +  setDefault(outputCol -> "lsh_output")
    +
    +  /**
    +   * Transform the Schema for LSH
    +   * @param schema The schema of the input dataset without outputCol
    +   * @return A derived schema with outputCol added
    +   */
    +  final def transformLSHSchema(schema: StructType): StructType = {
    +    val outputFields = schema.fields :+
    +      StructField($(outputCol), new VectorUDT, nullable = false)
    +    StructType(outputFields)
    +  }
    +}
    +
    +/**
    + * Model produced by [[LSH]].
    + */
    +abstract class LSHModel[KeyType, T <: LSHModel[KeyType, T]] private[ml]
    +  extends Model[T] with LSHParams {
    +  override def copy(extra: ParamMap): T = defaultCopy(extra)
    +  /**
    +   * :: DeveloperApi ::
    +   *
    +   * The hash function of LSH, mapping a predefined KeyType to a Vector
    +   * @return The mapping of LSH function.
    +   */
    +  protected[this] val hashFunction: KeyType => Vector
    +
    +  /**
    +   * :: DeveloperApi ::
    +   *
    +   * Calculate the distance between two different keys using the distance 
metric corresponding
    +   * to the hashFunction
    +   * @param x One of the point in the metric space
    +   * @param y Another the point in the metric space
    +   * @return The distance between x and y in double
    +   */
    +  protected[ml] def keyDistance(x: KeyType, y: KeyType): Double
    +
    +  /**
    +   * :: DeveloperApi ::
    +   *
    +   * Calculate the distance between two different hash Vectors. By 
default, the distance is the
    +   * minimum distance of two hash values in any dimension.
    +   *
    +   * @param x One of the hash vector
    +   * @param y Another hash vector
    +   * @return The distance between hash vectors x and y in double
    +   */
    +  protected[ml] def hashDistance(x: Vector, y: Vector): Double = {
    +    // Since it's generated by hashing, it will be a pair of dense vectors.
    +    x.toDense.values.zip(y.toDense.values).map(x => math.abs(x._1 - 
x._2)).min
    --- End diff --
    
    If it's algorithm-specific, I'd recommend making it abstract here so it's 
more future bug-proof.


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to