|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.mllib.feature |
| 19 | + |
| 20 | +import org.scalatest.FunSuite |
| 21 | + |
| 22 | +import org.apache.spark.mllib.linalg.{DenseVector, SparseVector, Vectors} |
| 23 | +import org.apache.spark.mllib.util.LocalSparkContext |
| 24 | +import org.apache.spark.mllib.util.TestingUtils._ |
| 25 | + |
| 26 | +class NormalizerSuite extends FunSuite with LocalSparkContext { |
| 27 | + |
| 28 | + val data = Array( |
| 29 | + Vectors.sparse(3, Seq((0, -2.0), (1, 2.3))), |
| 30 | + Vectors.dense(0.0, 0.0, 0.0), |
| 31 | + Vectors.dense(0.6, -1.1, -3.0), |
| 32 | + Vectors.sparse(3, Seq((1, 0.91), (2, 3.2))), |
| 33 | + Vectors.sparse(3, Seq((0, 5.7), (1, 0.72), (2, 2.7))), |
| 34 | + Vectors.sparse(3, Seq()) |
| 35 | + ) |
| 36 | + |
| 37 | + lazy val dataRDD = sc.parallelize(data, 3) |
| 38 | + |
| 39 | + test("Normalization using L1 distance") { |
| 40 | + val l1Normalizer = new Normalizer(1) |
| 41 | + |
| 42 | + val data1 = data.map(l1Normalizer.transform) |
| 43 | + val data1RDD = l1Normalizer.transform(dataRDD) |
| 44 | + |
| 45 | + assert((data, data1, data1RDD.collect()).zipped.forall { |
| 46 | + case (v1: DenseVector, v2: DenseVector, v3: DenseVector) => true |
| 47 | + case (v1: SparseVector, v2: SparseVector, v3: SparseVector) => true |
| 48 | + case _ => false |
| 49 | + }, "The vector type should be preserved after normalization.") |
| 50 | + |
| 51 | + assert((data1, data1RDD.collect()).zipped.forall((v1, v2) => v1 ~== v2 absTol 1E-5)) |
| 52 | + |
| 53 | + assert(data1(0).toBreeze.norm(1) ~== 1.0 absTol 1E-5) |
| 54 | + assert(data1(2).toBreeze.norm(1) ~== 1.0 absTol 1E-5) |
| 55 | + assert(data1(3).toBreeze.norm(1) ~== 1.0 absTol 1E-5) |
| 56 | + assert(data1(4).toBreeze.norm(1) ~== 1.0 absTol 1E-5) |
| 57 | + |
| 58 | + assert(data1(0) ~== Vectors.sparse(3, Seq((0, -0.465116279), (1, 0.53488372))) absTol 1E-5) |
| 59 | + assert(data1(1) ~== Vectors.dense(0.0, 0.0, 0.0) absTol 1E-5) |
| 60 | + assert(data1(2) ~== Vectors.dense(0.12765957, -0.23404255, -0.63829787) absTol 1E-5) |
| 61 | + assert(data1(3) ~== Vectors.sparse(3, Seq((1, 0.22141119), (2, 0.7785888))) absTol 1E-5) |
| 62 | + assert(data1(4) ~== Vectors.dense(0.625, 0.07894737, 0.29605263) absTol 1E-5) |
| 63 | + assert(data1(5) ~== Vectors.sparse(3, Seq()) absTol 1E-5) |
| 64 | + } |
| 65 | + |
| 66 | + test("Normalization using L2 distance") { |
| 67 | + val l2Normalizer = new Normalizer() |
| 68 | + |
| 69 | + val data2 = data.map(l2Normalizer.transform) |
| 70 | + val data2RDD = l2Normalizer.transform(dataRDD) |
| 71 | + |
| 72 | + assert((data, data2, data2RDD.collect()).zipped.forall { |
| 73 | + case (v1: DenseVector, v2: DenseVector, v3: DenseVector) => true |
| 74 | + case (v1: SparseVector, v2: SparseVector, v3: SparseVector) => true |
| 75 | + case _ => false |
| 76 | + }, "The vector type should be preserved after normalization.") |
| 77 | + |
| 78 | + assert((data2, data2RDD.collect()).zipped.forall((v1, v2) => v1 ~== v2 absTol 1E-5)) |
| 79 | + |
| 80 | + assert(data2(0).toBreeze.norm(2) ~== 1.0 absTol 1E-5) |
| 81 | + assert(data2(2).toBreeze.norm(2) ~== 1.0 absTol 1E-5) |
| 82 | + assert(data2(3).toBreeze.norm(2) ~== 1.0 absTol 1E-5) |
| 83 | + assert(data2(4).toBreeze.norm(2) ~== 1.0 absTol 1E-5) |
| 84 | + |
| 85 | + assert(data2(0) ~== Vectors.sparse(3, Seq((0, -0.65617871), (1, 0.75460552))) absTol 1E-5) |
| 86 | + assert(data2(1) ~== Vectors.dense(0.0, 0.0, 0.0) absTol 1E-5) |
| 87 | + assert(data2(2) ~== Vectors.dense(0.184549876, -0.3383414, -0.922749378) absTol 1E-5) |
| 88 | + assert(data2(3) ~== Vectors.sparse(3, Seq((1, 0.27352993), (2, 0.96186349))) absTol 1E-5) |
| 89 | + assert(data2(4) ~== Vectors.dense(0.897906166, 0.113419726, 0.42532397) absTol 1E-5) |
| 90 | + assert(data2(5) ~== Vectors.sparse(3, Seq()) absTol 1E-5) |
| 91 | + } |
| 92 | + |
| 93 | + test("Normalization using L^Inf distance.") { |
| 94 | + val lInfNormalizer = new Normalizer(Double.PositiveInfinity) |
| 95 | + |
| 96 | + val dataInf = data.map(lInfNormalizer.transform) |
| 97 | + val dataInfRDD = lInfNormalizer.transform(dataRDD) |
| 98 | + |
| 99 | + assert((data, dataInf, dataInfRDD.collect()).zipped.forall { |
| 100 | + case (v1: DenseVector, v2: DenseVector, v3: DenseVector) => true |
| 101 | + case (v1: SparseVector, v2: SparseVector, v3: SparseVector) => true |
| 102 | + case _ => false |
| 103 | + }, "The vector type should be preserved after normalization.") |
| 104 | + |
| 105 | + assert((dataInf, dataInfRDD.collect()).zipped.forall((v1, v2) => v1 ~== v2 absTol 1E-5)) |
| 106 | + |
| 107 | + assert(dataInf(0).toArray.map(Math.abs).max ~== 1.0 absTol 1E-5) |
| 108 | + assert(dataInf(2).toArray.map(Math.abs).max ~== 1.0 absTol 1E-5) |
| 109 | + assert(dataInf(3).toArray.map(Math.abs).max ~== 1.0 absTol 1E-5) |
| 110 | + assert(dataInf(4).toArray.map(Math.abs).max ~== 1.0 absTol 1E-5) |
| 111 | + |
| 112 | + assert(dataInf(0) ~== Vectors.sparse(3, Seq((0, -0.86956522), (1, 1.0))) absTol 1E-5) |
| 113 | + assert(dataInf(1) ~== Vectors.dense(0.0, 0.0, 0.0) absTol 1E-5) |
| 114 | + assert(dataInf(2) ~== Vectors.dense(0.2, -0.36666667, -1.0) absTol 1E-5) |
| 115 | + assert(dataInf(3) ~== Vectors.sparse(3, Seq((1, 0.284375), (2, 1.0))) absTol 1E-5) |
| 116 | + assert(dataInf(4) ~== Vectors.dense(1.0, 0.12631579, 0.473684211) absTol 1E-5) |
| 117 | + assert(dataInf(5) ~== Vectors.sparse(3, Seq()) absTol 1E-5) |
| 118 | + } |
| 119 | + |
| 120 | +} |
0 commit comments