|
| 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.ml.feature |
| 19 | + |
| 20 | +import org.apache.spark.SparkFunSuite |
| 21 | +import org.apache.spark.ml.attribute.AttributeGroup |
| 22 | +import org.apache.spark.ml.linalg.{Vector, Vectors} |
| 23 | +import org.apache.spark.ml.param.ParamsSuite |
| 24 | +import org.apache.spark.ml.util.DefaultReadWriteTest |
| 25 | +import org.apache.spark.ml.util.TestingUtils._ |
| 26 | +import org.apache.spark.mllib.util.MLlibTestSparkContext |
| 27 | +import org.apache.spark.sql.catalyst.encoders.ExpressionEncoder |
| 28 | +import org.apache.spark.sql.functions.col |
| 29 | +import org.apache.spark.sql.types._ |
| 30 | + |
| 31 | +class FeatureHasherSuite extends SparkFunSuite |
| 32 | + with MLlibTestSparkContext |
| 33 | + with DefaultReadWriteTest { |
| 34 | + |
| 35 | + import testImplicits._ |
| 36 | + |
| 37 | + import HashingTFSuite.murmur3FeatureIdx |
| 38 | + |
| 39 | + implicit private val vectorEncoder = ExpressionEncoder[Vector]() |
| 40 | + |
| 41 | + test("params") { |
| 42 | + ParamsSuite.checkParams(new FeatureHasher) |
| 43 | + } |
| 44 | + |
| 45 | + test("specify input cols using varargs or array") { |
| 46 | + val featureHasher1 = new FeatureHasher() |
| 47 | + .setInputCols("int", "double", "float", "stringNum", "string") |
| 48 | + val featureHasher2 = new FeatureHasher() |
| 49 | + .setInputCols(Array("int", "double", "float", "stringNum", "string")) |
| 50 | + assert(featureHasher1.getInputCols === featureHasher2.getInputCols) |
| 51 | + } |
| 52 | + |
| 53 | + test("feature hashing") { |
| 54 | + val df = Seq( |
| 55 | + (2.0, true, "1", "foo"), |
| 56 | + (3.0, false, "2", "bar") |
| 57 | + ).toDF("real", "bool", "stringNum", "string") |
| 58 | + |
| 59 | + val n = 100 |
| 60 | + val hasher = new FeatureHasher() |
| 61 | + .setInputCols("real", "bool", "stringNum", "string") |
| 62 | + .setOutputCol("features") |
| 63 | + .setNumFeatures(n) |
| 64 | + val output = hasher.transform(df) |
| 65 | + val attrGroup = AttributeGroup.fromStructField(output.schema("features")) |
| 66 | + assert(attrGroup.numAttributes === Some(n)) |
| 67 | + |
| 68 | + val features = output.select("features").as[Vector].collect() |
| 69 | + // Assume perfect hash on field names |
| 70 | + def idx: Any => Int = murmur3FeatureIdx(n) |
| 71 | + // check expected indices |
| 72 | + val expected = Seq( |
| 73 | + Vectors.sparse(n, Seq((idx("real"), 2.0), (idx("bool=true"), 1.0), |
| 74 | + (idx("stringNum=1"), 1.0), (idx("string=foo"), 1.0))), |
| 75 | + Vectors.sparse(n, Seq((idx("real"), 3.0), (idx("bool=false"), 1.0), |
| 76 | + (idx("stringNum=2"), 1.0), (idx("string=bar"), 1.0))) |
| 77 | + ) |
| 78 | + assert(features.zip(expected).forall { case (e, a) => e ~== a absTol 1e-14 }) |
| 79 | + } |
| 80 | + |
| 81 | + test("hashing works for all numeric types") { |
| 82 | + val df = Seq(5.0, 10.0, 15.0).toDF("real") |
| 83 | + |
| 84 | + val hasher = new FeatureHasher() |
| 85 | + .setInputCols("real") |
| 86 | + .setOutputCol("features") |
| 87 | + |
| 88 | + val expectedResult = hasher.transform(df).select("features").as[Vector].collect() |
| 89 | + // check all numeric types work as expected. String & boolean types are tested in default case |
| 90 | + val types = |
| 91 | + Seq(ShortType, LongType, IntegerType, FloatType, ByteType, DoubleType, DecimalType(10, 0)) |
| 92 | + types.foreach { t => |
| 93 | + val castDF = df.select(col("real").cast(t)) |
| 94 | + val castResult = hasher.transform(castDF).select("features").as[Vector].collect() |
| 95 | + withClue(s"FeatureHasher works for all numeric types (testing $t): ") { |
| 96 | + assert(castResult.zip(expectedResult).forall { case (actual, expected) => |
| 97 | + actual ~== expected absTol 1e-14 |
| 98 | + }) |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + test("invalid input type should fail") { |
| 104 | + val df = Seq( |
| 105 | + Vectors.dense(1), |
| 106 | + Vectors.dense(2) |
| 107 | + ).toDF("vec") |
| 108 | + |
| 109 | + intercept[IllegalArgumentException] { |
| 110 | + new FeatureHasher().setInputCols("vec").transform(df) |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + test("hash collisions sum feature values") { |
| 115 | + val df = Seq( |
| 116 | + (1.0, "foo", "foo"), |
| 117 | + (2.0, "bar", "baz") |
| 118 | + ).toDF("real", "string1", "string2") |
| 119 | + |
| 120 | + val n = 1 |
| 121 | + val hasher = new FeatureHasher() |
| 122 | + .setInputCols("real", "string1", "string2") |
| 123 | + .setOutputCol("features") |
| 124 | + .setNumFeatures(n) |
| 125 | + |
| 126 | + val features = hasher.transform(df).select("features").as[Vector].collect() |
| 127 | + def idx: Any => Int = murmur3FeatureIdx(n) |
| 128 | + // everything should hash into one field |
| 129 | + assert(idx("real") === idx("string1=foo")) |
| 130 | + assert(idx("string1=foo") === idx("string2=foo")) |
| 131 | + assert(idx("string2=foo") === idx("string1=bar")) |
| 132 | + assert(idx("string1=bar") === idx("string2=baz")) |
| 133 | + val expected = Seq( |
| 134 | + Vectors.sparse(n, Seq((idx("string1=foo"), 3.0))), |
| 135 | + Vectors.sparse(n, Seq((idx("string2=bar"), 4.0))) |
| 136 | + ) |
| 137 | + assert(features.zip(expected).forall { case (e, a) => e ~== a absTol 1e-14 }) |
| 138 | + } |
| 139 | + |
| 140 | + test("ignores null values in feature hashing") { |
| 141 | + import org.apache.spark.sql.functions._ |
| 142 | + |
| 143 | + val df = Seq( |
| 144 | + (2.0, "foo", null), |
| 145 | + (3.0, "bar", "baz") |
| 146 | + ).toDF("real", "string1", "string2").select( |
| 147 | + when(col("real") === 3.0, null).otherwise(col("real")).alias("real"), |
| 148 | + col("string1"), |
| 149 | + col("string2") |
| 150 | + ) |
| 151 | + |
| 152 | + val n = 100 |
| 153 | + val hasher = new FeatureHasher() |
| 154 | + .setInputCols("real", "string1", "string2") |
| 155 | + .setOutputCol("features") |
| 156 | + .setNumFeatures(n) |
| 157 | + |
| 158 | + val features = hasher.transform(df).select("features").as[Vector].collect() |
| 159 | + def idx: Any => Int = murmur3FeatureIdx(n) |
| 160 | + val expected = Seq( |
| 161 | + Vectors.sparse(n, Seq((idx("real"), 2.0), (idx("string1=foo"), 1.0))), |
| 162 | + Vectors.sparse(n, Seq((idx("string1=bar"), 1.0), (idx("string2=baz"), 1.0))) |
| 163 | + ) |
| 164 | + assert(features.zip(expected).forall { case (e, a) => e ~== a absTol 1e-14 }) |
| 165 | + } |
| 166 | + |
| 167 | + test("unicode column names and values") { |
| 168 | + // scalastyle:off nonascii |
| 169 | + val df = Seq((2.0, "中文")).toDF("中文", "unicode") |
| 170 | + |
| 171 | + val n = 100 |
| 172 | + val hasher = new FeatureHasher() |
| 173 | + .setInputCols("中文", "unicode") |
| 174 | + .setOutputCol("features") |
| 175 | + .setNumFeatures(n) |
| 176 | + |
| 177 | + val features = hasher.transform(df).select("features").as[Vector].collect() |
| 178 | + def idx: Any => Int = murmur3FeatureIdx(n) |
| 179 | + val expected = Seq( |
| 180 | + Vectors.sparse(n, Seq((idx("中文"), 2.0), (idx("unicode=中文"), 1.0))) |
| 181 | + ) |
| 182 | + assert(features.zip(expected).forall { case (e, a) => e ~== a absTol 1e-14 }) |
| 183 | + // scalastyle:on nonascii |
| 184 | + } |
| 185 | + |
| 186 | + test("read/write") { |
| 187 | + val t = new FeatureHasher() |
| 188 | + .setInputCols(Array("myCol1", "myCol2", "myCol3")) |
| 189 | + .setOutputCol("myOutputCol") |
| 190 | + .setNumFeatures(10) |
| 191 | + testDefaultReadWrite(t) |
| 192 | + } |
| 193 | +} |
0 commit comments