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-7581][ml][doc] User guide for spark.ml ...
Date Mon, 18 May 2015 16:45:05 GMT
Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/6113#discussion_r30525096
  
    --- Diff: docs/ml-features.md ---
    @@ -183,6 +183,88 @@ for words_label in wordsDataFrame.select("words", "label").take(3):
     </div>
     </div>
     
    +## PolynomialExpansion
    +
    +[Polynomial expansion](http://en.wikipedia.org/wiki/Polynomial_expansion) is the process
of expanding your features into a polynomial space, which is formulated by an n-degree combination
of original dimensions. A [PolynomialExpansion](api/scala/index.html#org.apache.spark.ml.feature.PolynomialExpansion)
class provides this functionality.  The example below shows how to expand your features into
a 3-degree polynomial space.
    +
    +<div class="codetabs">
    +<div data-lang="scala" markdown="1">
    +{% highlight scala %}
    +import org.apache.spark.ml.feature.PolynomialExpansion
    +import org.apache.spark.mllib.linalg.Vectors
    +
    +val data = Array(
    +  Vectors.dense(-2.0, 2.3),
    +  Vectors.dense(0.0, 0.0),
    +  Vectors.dense(0.6, -1.1)
    +)
    +val df = sqlContext.createDataFrame(data.map(Tuple1.apply)).toDF("features")
    +val polynomialExpansion = new PolynomialExpansion()
    +  .setInputCol("features")
    +  .setOutputCol("polyFeatures")
    +  .setDegree(3)
    +val polyDF = polynomialExpansion.transform(df)
    +polyDF.select("polyFeatures").take(3).foreach(println)
    +{% endhighlight %}
    +</div>
    +
    +<div data-lang="java" markdown="1">
    +{% highlight java %}
    +import com.google.common.collect.Lists;
    +
    +import org.apache.spark.api.java.JavaRDD;
    +import org.apache.spark.api.java.JavaSparkContext;
    +import org.apache.spark.mllib.linalg.Vector;
    +import org.apache.spark.mllib.linalg.VectorUDT;
    +import org.apache.spark.mllib.linalg.Vectors;
    +import org.apache.spark.sql.DataFrame;
    +import org.apache.spark.sql.Row;
    +import org.apache.spark.sql.RowFactory;
    +import org.apache.spark.sql.SQLContext;
    +import org.apache.spark.sql.types.Metadata;
    +import org.apache.spark.sql.types.StructField;
    +import org.apache.spark.sql.types.StructType;
    +
    +JavaSparkContext jsc = ...
    +SQLContext jsql = ...
    +PolynomialExpansion polyExpansion = new PolynomialExpansion()
    +  .setInputCol("features")
    +  .setOutputCol("polyFeatures")
    +  .setDegree(3);
    +JavaRDD<Row> data = jsc.parallelize(Lists.newArrayList(
    +  RowFactory.create(Vectors.dense(-2.0, 2.3)),
    +  RowFactory.create(Vectors.dense(0.0, 0.0)),
    +  RowFactory.create(Vectors.dense(0.6, -1.1))
    +));
    +StructType schema = new StructType(new StructField[] {
    +  new StructField("features", new VectorUDT(), false, Metadata.empty()),
    +});
    +DataFrame df = jsql.createDataFrame(data, schema);
    +DataFrame polyDF = polyExpansion.transform(df);
    +Row[] row = polyDF.select("polyFeatures").take(3);
    +for (Row r : row) {
    +  System.out.println(r.get(0));
    +}
    +{% endhighlight %}
    +</div>
    +
    +<div data-lang="python" markdown="1">
    +{% highlight python %}
    +from pyspark.ml.feature import PolynomialExpansion
    +from pyspark.mllib.linalg import Vectors
    +
    +df = sqlContext.createDataFrame(
    +  [(Vectors.dense([-2.0, 2.3]), ),
    +  (Vectors.dense([0.0, 0.0]), ),
    +  (Vectors.dense([0.6, -1.1]), )],
    +  ["features"])
    +px = PolynomialExpansion(degree=2, inputCol="features", outputCol="polyFeatures")
    +polyDF = px.transform(df)
    +for expanded in polyDF.select("polyFeatures").take(3):
    +  print expanded
    --- End diff --
    
    `print expanded` -> `print(expanded)` (for Python 3 compatibility)


---
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