spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mengxr <...@git.apache.org>
Subject [GitHub] spark pull request: SPARK-2272 [MLlib] Feature scaling which stand...
Date Mon, 04 Aug 2014 02:43:29 GMT
Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/1207#discussion_r15739002
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/feature/Normalizer.scala ---
    @@ -0,0 +1,108 @@
    +/*
    + * 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.mllib.feature
    +
    +import breeze.linalg.{DenseVector => BDV, SparseVector => BSV}
    +
    +import org.apache.spark.annotation.DeveloperApi
    +import org.apache.spark.mllib.linalg.{Vector, Vectors}
    +
    +/**
    + * :: DeveloperApi ::
    + * Normalizes samples individually to unit L^p norm
    + *
    + * For any 1 <= p < Double.Infinity, normalizes samples using sum(abs(vector).^p)^(1/p)
as norm.
    + * For p = Double.Infinity, max(abs(vector)) will be used as norm for normalization.
    + * For p = Double.NegativeInfinity, min(abs(vector)) will be used as norm for normalization.
    + *
    + * @param p Normalization in L^p space, p = 2 by default.
    + */
    +@DeveloperApi
    +class Normalizer(p: Double) extends VectorTransformer {
    +
    +  def this() = this(2)
    +
    +  require(p >= 1.0 || p == Double.NegativeInfinity)
    +
    +  /**
    +   * Applies unit length normalization on a vector.
    +   *
    +   * @param vector vector to be normalized.
    +   * @return normalized vector. If the norm of the input is zero, it will return the
input vector.
    +   */
    +  override def transform(vector: Vector): Vector = {
    +    var distance = 0.0
    +
    +    if (p >= 1.0) {
    +      distance = vector.toBreeze.norm(p)
    +    } else if (p == Double.NegativeInfinity) {
    +      // Breeze norm doesn't work when p = NegativeInfinity
    +      var min = Double.PositiveInfinity
    +      vector.toBreeze match {
    +        case dv: BDV[Double] => {
    +          var i = 0
    +          while (i < dv.length) {
    +            if (Math.abs(dv(i)) < min) min = Math.abs(Math.abs(dv(i)))
    +            i += 1
    +          }
    +        }
    +        case sv: BSV[Double] => {
    +          if (sv.index.length != sv.length) {
    +            // Find a zero element in sparse vector, don't need going into the loop.
    +            min = 0.0
    +          } else {
    +            var i = 0
    +            while (i < sv.index.length) {
    +              if (Math.abs(sv.data(i)) < min) min = Math.abs(Math.abs(sv.data(i)))
    +              i += 1
    +            }
    +          }
    +        }
    +        case v: Any =>
    +          throw new IllegalArgumentException("Do not support vector type " + v.getClass)
    +      }
    +      distance = if (min != Double.PositiveInfinity) min else 0.0
    +    }
    +
    +    if (distance != 0.0) {
    +      // For dense vector, we've to allocate new memory for new output vector.
    +      // However, for sparse vector, the `index` array will not be changed,
    +      // so we can re-use it to save memory.
    +      vector.toBreeze match {
    +        case dv: BDV[Double] => Vectors.fromBreeze(dv :/ distance)
    +        case sv: BSV[Double] => {
    --- End diff --
    
    `=> {` -> `=>` and remove the closing `}` below


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

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


Mime
View raw message