-
Notifications
You must be signed in to change notification settings - Fork 28.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-31659][ML][DOCS] Add VarianceThresholdSelector examples and doc
### What changes were proposed in this pull request? Add VarianceThresholdSelector examples and doc ### Why are the changes needed? VarianceThresholdSelector is a new feature selector in 3.1.0. We need to add examples and doc ### Does this PR introduce _any_ user-facing change? Yes. add Scala, Python and Java examples for VarianceThresholdSelector. Also add doc <img width="860" alt="Screen Shot 2020-05-07 at 9 20 01 AM" src="https://user-images.githubusercontent.com/13592258/81321791-e3f84d80-9047-11ea-837b-e39c193bd437.png"> <img width="860" alt="Screen Shot 2020-05-07 at 9 20 44 AM" src="https://user-images.githubusercontent.com/13592258/81321806-e8246b00-9047-11ea-8f35-206e330a92ab.png"> <img width="860" alt="Screen Shot 2020-05-07 at 9 21 27 AM" src="https://user-images.githubusercontent.com/13592258/81321822-ea86c500-9047-11ea-8743-99adec7f502b.png"> <img width="860" alt="Screen Shot 2020-05-07 at 9 21 43 AM" src="https://user-images.githubusercontent.com/13592258/81321826-ec508880-9047-11ea-9e7a-22ee5e13f495.png"> ### How was this patch tested? Manually checked Closes #28478 from huaxingao/variance_doc. Authored-by: Huaxin Gao <huaxing@us.ibm.com> Signed-off-by: zhengruifeng <ruifengz@foxmail.com>
- Loading branch information
1 parent
97332f2
commit 08335b6
Showing
4 changed files
with
267 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...ples/src/main/java/org/apache/spark/examples/ml/JavaVarianceThresholdSelectorExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* 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.examples.ml; | ||
|
||
import org.apache.spark.sql.Dataset; | ||
import org.apache.spark.sql.SparkSession; | ||
|
||
// $example on$ | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.apache.spark.ml.feature.VarianceThresholdSelector; | ||
import org.apache.spark.ml.linalg.VectorUDT; | ||
import org.apache.spark.ml.linalg.Vectors; | ||
import org.apache.spark.sql.Row; | ||
import org.apache.spark.sql.RowFactory; | ||
import org.apache.spark.sql.types.*; | ||
// $example off$ | ||
|
||
/** | ||
* An example for VarianceThresholdSelector. | ||
* Run with | ||
* <pre> | ||
* bin/run-example ml.JavaVarianceThresholdSelectorExample | ||
* </pre> | ||
*/ | ||
public class JavaVarianceThresholdSelectorExample { | ||
public static void main(String[] args) { | ||
SparkSession spark = SparkSession | ||
.builder() | ||
.appName("JavaVarianceThresholdSelectorExample") | ||
.getOrCreate(); | ||
|
||
// $example on$ | ||
List<Row> data = Arrays.asList( | ||
RowFactory.create(1, Vectors.dense(6.0, 7.0, 0.0, 7.0, 6.0, 0.0)), | ||
RowFactory.create(2, Vectors.dense(0.0, 9.0, 6.0, 0.0, 5.0, 9.0)), | ||
RowFactory.create(3, Vectors.dense(0.0, 9.0, 3.0, 0.0, 5.0, 5.0)), | ||
RowFactory.create(4, Vectors.dense(0.0, 9.0, 8.0, 5.0, 6.0, 4.0)), | ||
RowFactory.create(5, Vectors.dense(8.0, 9.0, 6.0, 5.0, 4.0, 4.0)), | ||
RowFactory.create(6, Vectors.dense(8.0, 9.0, 6.0, 0.0, 0.0, 0.0)) | ||
); | ||
StructType schema = new StructType(new StructField[]{ | ||
new StructField("id", DataTypes.IntegerType, false, Metadata.empty()), | ||
new StructField("features", new VectorUDT(), false, Metadata.empty()) | ||
}); | ||
|
||
Dataset<Row> df = spark.createDataFrame(data, schema); | ||
|
||
VarianceThresholdSelector selector = new VarianceThresholdSelector() | ||
.setVarianceThreshold(8.0) | ||
.setFeaturesCol("features") | ||
.setOutputCol("selectedFeatures"); | ||
|
||
Dataset<Row> result = selector.fit(df).transform(df); | ||
|
||
System.out.println("Output: Features with variance lower than " | ||
+ selector.getVarianceThreshold() + " are removed."); | ||
result.show(); | ||
|
||
// $example off$ | ||
spark.stop(); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
examples/src/main/python/ml/variance_threshold_selector_example.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# | ||
# 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. | ||
# | ||
|
||
""" | ||
An example for VarianceThresholdSelector. | ||
Run with: | ||
bin/spark-submit examples/src/main/python/ml/variance_threshold_selector_example.py | ||
""" | ||
from __future__ import print_function | ||
|
||
from pyspark.sql import SparkSession | ||
# $example on$ | ||
from pyspark.ml.feature import VarianceThresholdSelector | ||
from pyspark.ml.linalg import Vectors | ||
# $example off$ | ||
|
||
if __name__ == "__main__": | ||
spark = SparkSession\ | ||
.builder\ | ||
.appName("VarianceThresholdSelectorExample")\ | ||
.getOrCreate() | ||
|
||
# $example on$ | ||
df = spark.createDataFrame([ | ||
(1, Vectors.dense([6.0, 7.0, 0.0, 7.0, 6.0, 0.0])), | ||
(2, Vectors.dense([0.0, 9.0, 6.0, 0.0, 5.0, 9.0])), | ||
(3, Vectors.dense([0.0, 9.0, 3.0, 0.0, 5.0, 5.0])), | ||
(4, Vectors.dense([0.0, 9.0, 8.0, 5.0, 6.0, 4.0])), | ||
(5, Vectors.dense([8.0, 9.0, 6.0, 5.0, 4.0, 4.0])), | ||
(6, Vectors.dense([8.0, 9.0, 6.0, 0.0, 0.0, 0.0]))], ["id", "features"]) | ||
|
||
selector = VarianceThresholdSelector(varianceThreshold=8.0, outputCol="selectedFeatures") | ||
|
||
result = selector.fit(df).transform(df) | ||
|
||
print("Output: Features with variance lower than %f are removed." % | ||
selector.getVarianceThreshold()) | ||
result.show() | ||
# $example off$ | ||
|
||
spark.stop() |
69 changes: 69 additions & 0 deletions
69
examples/src/main/scala/org/apache/spark/examples/ml/VarianceThresholdSelectorExample.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
// scalastyle:off println | ||
package org.apache.spark.examples.ml | ||
|
||
// $example on$ | ||
import org.apache.spark.ml.feature.VarianceThresholdSelector | ||
import org.apache.spark.ml.linalg.Vectors | ||
// $example off$ | ||
import org.apache.spark.sql.SparkSession | ||
|
||
/** | ||
* An example for VarianceThresholdSelector. | ||
* Run with | ||
* {{{ | ||
* bin/run-example ml.VarianceThresholdSelectorExample | ||
* }}} | ||
*/ | ||
object VarianceThresholdSelectorExample { | ||
def main(args: Array[String]): Unit = { | ||
val spark = SparkSession | ||
.builder | ||
.appName("VarianceThresholdSelectorExample") | ||
.getOrCreate() | ||
import spark.implicits._ | ||
|
||
// $example on$ | ||
val data = Seq( | ||
(1, Vectors.dense(6.0, 7.0, 0.0, 7.0, 6.0, 0.0)), | ||
(2, Vectors.dense(0.0, 9.0, 6.0, 0.0, 5.0, 9.0)), | ||
(3, Vectors.dense(0.0, 9.0, 3.0, 0.0, 5.0, 5.0)), | ||
(4, Vectors.dense(0.0, 9.0, 8.0, 5.0, 6.0, 4.0)), | ||
(5, Vectors.dense(8.0, 9.0, 6.0, 5.0, 4.0, 4.0)), | ||
(6, Vectors.dense(8.0, 9.0, 6.0, 0.0, 0.0, 0.0)) | ||
) | ||
|
||
val df = spark.createDataset(data).toDF("id", "features") | ||
|
||
val selector = new VarianceThresholdSelector() | ||
.setVarianceThreshold(8.0) | ||
.setFeaturesCol("features") | ||
.setOutputCol("selectedFeatures") | ||
|
||
val result = selector.fit(df).transform(df) | ||
|
||
println(s"Output: Features with variance lower than" + | ||
s" ${selector.getVarianceThreshold} are removed.") | ||
result.show() | ||
// $example off$ | ||
|
||
spark.stop() | ||
} | ||
} | ||
// scalastyle:on println |