-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-23048][ML] Add OneHotEncoderEstimator document and examples #20257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
21cb7d3
13a7b90
262c046
e57d9ee
18cf226
3c697bd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,9 +23,8 @@ | |
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| import org.apache.spark.ml.feature.OneHotEncoder; | ||
| import org.apache.spark.ml.feature.StringIndexer; | ||
| import org.apache.spark.ml.feature.StringIndexerModel; | ||
| import org.apache.spark.ml.feature.OneHotEncoderEstimator; | ||
| import org.apache.spark.ml.feature.OneHotEncoderModel; | ||
| import org.apache.spark.sql.Dataset; | ||
| import org.apache.spark.sql.Row; | ||
| import org.apache.spark.sql.RowFactory; | ||
|
|
@@ -35,41 +34,37 @@ | |
| import org.apache.spark.sql.types.StructType; | ||
| // $example off$ | ||
|
|
||
| public class JavaOneHotEncoderExample { | ||
| public class JavaOneHotEncoderEstimatorExample { | ||
| public static void main(String[] args) { | ||
| SparkSession spark = SparkSession | ||
| .builder() | ||
| .appName("JavaOneHotEncoderExample") | ||
| .appName("JavaOneHotEncoderEstimatorExample") | ||
| .getOrCreate(); | ||
|
|
||
| // Note: categorical features are usually first encoded with StringIndexer | ||
| // $example on$ | ||
| List<Row> data = Arrays.asList( | ||
| RowFactory.create(0, "a"), | ||
| RowFactory.create(1, "b"), | ||
| RowFactory.create(2, "c"), | ||
| RowFactory.create(3, "a"), | ||
| RowFactory.create(4, "a"), | ||
| RowFactory.create(5, "c") | ||
| RowFactory.create(0.0, 1.0), | ||
| RowFactory.create(1.0, 0.0), | ||
| RowFactory.create(2.0, 1.0), | ||
| RowFactory.create(0.0, 2.0), | ||
| RowFactory.create(0.0, 1.0), | ||
| RowFactory.create(2.0, 0.0) | ||
| ); | ||
|
|
||
| StructType schema = new StructType(new StructField[]{ | ||
| new StructField("id", DataTypes.IntegerType, false, Metadata.empty()), | ||
| new StructField("category", DataTypes.StringType, false, Metadata.empty()) | ||
| new StructField("categoryIndex1", DataTypes.DoubleType, false, Metadata.empty()), | ||
| new StructField("categoryIndex2", DataTypes.DoubleType, false, Metadata.empty()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't need to pass
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is java example, the default param seems don't work: error: no suitable constructor found for StructField(String,DataType,boolean)
[error] new StructField("categoryIndex1", DataTypes.DoubleType, false),
[error] ^
[error] /root/repos/spark-1/constructor StructField.StructField(String,DataType,boolean,Metadata) is not applicable
[error] (actual and formal argument lists differ in length)
[error] constructor StructField.StructField() is not applicable |
||
| }); | ||
|
|
||
| Dataset<Row> df = spark.createDataFrame(data, schema); | ||
|
|
||
| StringIndexerModel indexer = new StringIndexer() | ||
| .setInputCol("category") | ||
| .setOutputCol("categoryIndex") | ||
| .fit(df); | ||
| Dataset<Row> indexed = indexer.transform(df); | ||
| OneHotEncoderEstimator encoder = new OneHotEncoderEstimator() | ||
| .setInputCols(new String[] {"categoryIndex1", "categoryIndex2"}) | ||
| .setOutputCols(new String[] {"categoryVec1", "categoryVec2"}); | ||
|
|
||
| OneHotEncoder encoder = new OneHotEncoder() | ||
| .setInputCol("categoryIndex") | ||
| .setOutputCol("categoryVec"); | ||
|
|
||
| Dataset<Row> encoded = encoder.transform(indexed); | ||
| OneHotEncoderModel model = encoder.fit(df); | ||
| Dataset<Row> encoded = model.transform(df); | ||
| encoded.show(); | ||
| // $example off$ | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should add a little more detail about why it's deprecated.
The reason is that because the existing
OneHotEncoderis a stateless transformer, it is not usable on new data where the number of categories may differ from the training data. In order to fix this, a newOneHotEncoderEstimatorwas created that produces aOneHotEncoderModelwhen fit. Add a link to the JIRA ticket for more detail (https://issues.apache.org/jira/browse/SPARK-13030).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Added.