diff options
author | zero323 <zero323@users.noreply.github.com> | 2016-08-14 11:59:24 +0100 |
---|---|---|
committer | Sean Owen <sowen@cloudera.com> | 2016-08-14 11:59:24 +0100 |
commit | 0ebf7c1bff736cf54ec47957d71394d5b75b47a7 (patch) | |
tree | 4bc4aa60b1254db1af2aaa8981e4eac486fe0973 /mllib/src/main | |
parent | cdaa562c9a09e2e83e6df4e84d911ce1428a7a7c (diff) | |
download | spark-0ebf7c1bff736cf54ec47957d71394d5b75b47a7.tar.gz spark-0ebf7c1bff736cf54ec47957d71394d5b75b47a7.tar.bz2 spark-0ebf7c1bff736cf54ec47957d71394d5b75b47a7.zip |
[SPARK-17027][ML] Avoid integer overflow in PolynomialExpansion.getPolySize
## What changes were proposed in this pull request?
Replaces custom choose function with o.a.commons.math3.CombinatoricsUtils.binomialCoefficient
## How was this patch tested?
Spark unit tests
Author: zero323 <zero323@users.noreply.github.com>
Closes #14614 from zero323/SPARK-17027.
Diffstat (limited to 'mllib/src/main')
-rw-r--r-- | mllib/src/main/scala/org/apache/spark/ml/feature/PolynomialExpansion.scala | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/mllib/src/main/scala/org/apache/spark/ml/feature/PolynomialExpansion.scala b/mllib/src/main/scala/org/apache/spark/ml/feature/PolynomialExpansion.scala index 72fb35bd79..6e872c1f2c 100644 --- a/mllib/src/main/scala/org/apache/spark/ml/feature/PolynomialExpansion.scala +++ b/mllib/src/main/scala/org/apache/spark/ml/feature/PolynomialExpansion.scala @@ -19,6 +19,8 @@ package org.apache.spark.ml.feature import scala.collection.mutable +import org.apache.commons.math3.util.CombinatoricsUtils + import org.apache.spark.annotation.Since import org.apache.spark.ml.UnaryTransformer import org.apache.spark.ml.linalg._ @@ -84,12 +86,12 @@ class PolynomialExpansion @Since("1.4.0") (@Since("1.4.0") override val uid: Str @Since("1.6.0") object PolynomialExpansion extends DefaultParamsReadable[PolynomialExpansion] { - private def choose(n: Int, k: Int): Int = { - Range(n, n - k, -1).product / Range(k, 1, -1).product + private def getPolySize(numFeatures: Int, degree: Int): Int = { + val n = CombinatoricsUtils.binomialCoefficient(numFeatures + degree, degree) + require(n <= Integer.MAX_VALUE) + n.toInt } - private def getPolySize(numFeatures: Int, degree: Int): Int = choose(numFeatures + degree, degree) - private def expandDense( values: Array[Double], lastIdx: Int, |