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

    https://github.com/apache/spark/pull/15148#discussion_r86719955
  
    --- Diff: mllib/src/main/scala/org/apache/spark/ml/feature/MinHash.scala ---
    @@ -0,0 +1,194 @@
    +/*
    + * 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
    +
    +import scala.util.Random
    +
    +import org.apache.hadoop.fs.Path
    +
    +import org.apache.spark.annotation.{Experimental, Since}
    +import org.apache.spark.ml.linalg.{Vector, Vectors, VectorUDT}
    +import org.apache.spark.ml.param.ParamMap
    +import org.apache.spark.ml.param.shared.HasSeed
    +import org.apache.spark.ml.util._
    +import org.apache.spark.sql.types.StructType
    +
    +/**
    + * :: Experimental ::
    + *
    + * Model produced by [[MinHash]], where multiple hash functions are 
stored. Each hash function is
    + * a perfect hash function:
    + *    `h_i(x) = (x * k_i mod prime) mod numEntries`
    + * where `k_i` is the i-th coefficient, and both `x` and `k_i` are from 
`Z_prime^*`
    + *
    + * Reference:
    + * [[https://en.wikipedia.org/wiki/Perfect_hash_function Wikipedia on 
Perfect Hash Function]]
    + *
    + * @param numEntries The number of entries of the hash functions.
    + * @param randCoefficients An array of random coefficients, each used by 
one hash function.
    + */
    +@Experimental
    +@Since("2.1.0")
    +class MinHashModel private[ml] (
    +    override val uid: String,
    +    @Since("2.1.0") val numEntries: Int,
    +    @Since("2.1.0") val randCoefficients: Array[Int])
    +  extends LSHModel[MinHashModel] {
    +
    +  @Since("2.1.0")
    +  override protected[ml] val hashFunction: Vector => Vector = {
    +    elems: Vector =>
    +      require(elems.numNonzeros > 0, "Must have at least 1 non zero 
entry.")
    +      val elemsList = elems.toSparse.indices.toList
    +      val hashValues = randCoefficients.map({ randCoefficient: Int =>
    +          elemsList.map({elem: Int =>
    +            (1 + elem) * randCoefficient.toLong % MinHash.prime % 
numEntries
    +          }).min.toDouble
    +      })
    +      Vectors.dense(hashValues)
    +  }
    +
    +  @Since("2.1.0")
    +  override protected[ml] def keyDistance(x: Vector, y: Vector): Double = {
    +    val xSet = x.toSparse.indices.toSet
    +    val ySet = y.toSparse.indices.toSet
    +    val intersectionSize = xSet.intersect(ySet).size.toDouble
    +    val unionSize = xSet.size + ySet.size - intersectionSize
    +    assert(unionSize > 0, "The union of two input sets must have at least 
1 elements")
    +    1 - intersectionSize / unionSize
    +  }
    +
    +  @Since("2.1.0")
    +  override 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(pair => math.abs(pair._1 - 
pair._2)).min
    --- End diff --
    
    Does this even make sense for `MinHash`? For the `RandomProjection` class I 
understand that the absolute difference between their hash values is a measure 
of their similarity, but for `MinHash` I don't think it is. It is true that 
dissimilar items have a lower likelihood of hash collisions, but it should not 
be true that they have a low likelihood to hash to buckets near each other. We 
use this `hashDistance` to ensure that we get enough near-neighbor candidates, 
but I don't see how this `hashDistance` corresponds to similarity in the case 
where there are no zero distance elements.


---
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]

Reply via email to