Github user mengxr commented on a diff in the pull request:
https://github.com/apache/spark/pull/6127#discussion_r30862747
--- Diff: docs/ml-features.md ---
@@ -618,5 +634,157 @@ indexedData = indexerModel.transform(data)
</div>
</div>
+
+## Normalizer
+
+`Normalizer` is a `Transformer` which transforms a dataset of `Vector`
rows, normalizing each `Vector` to have unit norm. It takes parameter `p`,
which specifies the
[p-norm](http://en.wikipedia.org/wiki/Norm_%28mathematics%29#p-norm) used for
normalization. ($p = 2$ by default.) This normalization can help standardize
your input data and improve the behavior of learning algorithms.
+
+The following example demonstrates how to load a dataset in libsvm format
and then normalize each row to have unit $L^2$ norm and unit $L^\infty$ norm.
+
+<div class="codetabs">
+<div data-lang="scala">
+{% highlight scala %}
+import org.apache.spark.ml.feature.Normalizer
+import org.apache.spark.mllib.util.MLUtils
+
+val data = MLUtils.loadLibSVMFile(sc, "data/mllib/sample_libsvm_data.txt")
+val dataFrame = sqlContext.createDataFrame(data)
+val normalizer = new
Normalizer().setInputCol("features").setOutputCol("normFeatures")
+
+// Normalize each Vector using $L^2$ norm.
+val l2NormData = normalizer.transform(dataFrame, normalizer.p -> 2)
+
+// Normalize each Vector using $L^\infty$ norm.
+val lInfNormData = normalizer.transform(dataFrame, normalizer.p ->
Double.PositiveInfinity)
+{% endhighlight %}
+</div>
+
+<div data-lang="java">
+{% highlight java %}
+import org.apache.spark.api.java.JavaRDD;
+import org.apache.spark.ml.feature.Normalizer;
+import org.apache.spark.mllib.regression.LabeledPoint;
+import org.apache.spark.mllib.util.MLUtils;
+import org.apache.spark.sql.DataFrame;
+
+JavaRDD<LabeledPoint> data =
+ MLUtils.loadLibSVMFile(jsc.sc(),
"data/mllib/sample_libsvm_data.txt").toJavaRDD();
+DataFrame dataFrame = jsql.createDataFrame(data, LabeledPoint.class);
+Normalizer normalizer = new Normalizer()
+ .setInputCol("features")
+ .setOutputCol("normFeatures");
+
+// Normalize each Vector using $L^2$ norm.
+DataFrame l2NormData = normalizer.transform(dataFrame,
normalizer.p().w(2));
+
+// Normalize each Vector using $L^\infty$ norm.
+DataFrame lInfNormData =
+ normalizer.transform(dataFrame,
normalizer.p().w(Double.POSITIVE_INFINITY));
+{% endhighlight %}
+</div>
+
+<div data-lang="python">
+{% highlight python %}
+from pyspark.mllib.util import MLUtils
+from pyspark.ml.feature import Normalizer
+
+data = MLUtils.loadLibSVMFile(sc, "data/mllib/sample_libsvm_data.txt")
+dataFrame = sqlContext.createDataFrame(data)
+normalizer = Normalizer(inputCol="features", outputCol="normFeatures")
+
+# Normalize each Vector using $L^2$ norm.
+l2NormData = normalizer.transform(dataFrame, {normalizer.p:2.0})
--- End diff --
space after `:`
---
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]