Github user mengxr commented on a diff in the pull request:
https://github.com/apache/spark/pull/5245#discussion_r28846227
--- Diff:
mllib/src/main/scala/org/apache/spark/ml/feature/PolynomialMapper.scala ---
@@ -0,0 +1,240 @@
+/*
+ * 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.annotation.tailrec
+import scala.collection.mutable.ArrayBuffer
+
+import org.apache.spark.annotation.AlphaComponent
+import org.apache.spark.ml.UnaryTransformer
+import org.apache.spark.ml.param.{IntParam, ParamMap}
+import org.apache.spark.mllib.linalg._
+import org.apache.spark.sql.types.DataType
+
+/**
+ * :: AlphaComponent ::
+ * Perform feature expansion in a polynomial space. As said in wikipedia
of Polynomial Expansion,
+ * which is available at
[[http://en.wikipedia.org/wiki/Polynomial_expansion]], "In mathematics, an
+ * expansion of a product of sums expresses it as a sum of products by
using the fact that
+ * multiplication distributes over addition". Take a 2-variable feature
vector as an example:
+ * `(x, y)`, if we want to expand it with degree 2, then we get `(x, y, x
* x, x * y, y * y)`.
+ */
+@AlphaComponent
+class PolynomialMapper extends UnaryTransformer[Vector, Vector,
PolynomialMapper] {
+
+ /**
+ * The polynomial degree to expand, which should be larger than 1.
+ * @group param
+ */
+ val degree = new IntParam(this, "degree", "the polynomial degree to
expand", Some(2))
+
+ /** @group getParam */
+ def getDegree: Int = get(degree)
+
+ /** @group setParam */
+ def setDegree(value: Int): this.type = set(degree, value)
+
+ override protected def createTransformFunc(paramMap: ParamMap): Vector
=> Vector = {
+ PolynomialMapper.transform(getDegree)
+ }
+
+ override protected def outputDataType: DataType = new VectorUDT()
+}
+
+object PolynomialMapper {
+ /**
+ * The number that combines k items from N items without repeat, i.e.
the binomial coefficient.
+ */
+ private def binomialCoefficient(N: Int, k: Int): Int = {
+ (N - k + 1 to N).product / (1 to k).product
+ }
+
+ /**
+ * The number of monomials of a `numVariables` vector after expanding at
a specific polynomial
+ * degree `degree`.
+ */
+ private def numMonomials(degree: Int, numVariables: Int): Int = {
+ binomialCoefficient(numVariables + degree - 1, degree)
+ }
+
+ /**
+ * The number of monomials of a `numVariables` vector after expanding
from polynomial degree 1 to
+ * polynomial degree `degree`.
+ */
+ private def numExpandedDims(degree: Int, numVariables: Int): Int = {
+ binomialCoefficient(numVariables + degree, numVariables) - 1
--- End diff --
`choose(n + 1, k) - 1`?
---
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]