-
Notifications
You must be signed in to change notification settings - Fork 28.4k
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
[SPARK-37178][ML] Add Target Encoding to ml.feature #48347
Closed
Closed
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
5715092
[SPARK-37178][ML] Add Target Encoding to ml.feature
5f1902e
Merge branch 'master' of https://github.com/rebo16v/spark-target-enco…
0264264
[SPARK-37178][ML] handle null category, support all numeric types, im…
12cba08
Merge branch 'master' of https://github.com/rebo16v/spark-target-enco…
0221933
[SPARK-37178][ML] ignore null label observations
a329d16
Merge branch 'master' of https://github.com/rebo16v/spark-target-enco…
3f1f86d
[SPARK-37178][ML] improve doc & comments
cc3b78c
Merge branch 'master' of https://github.com/rebo16v/spark-target-enco…
229e5ed
[SPARK-37178][ML] allow different feature names in model
5a67c50
Merge branch 'master' of https://github.com/rebo16v/spark-target-enco…
bb95c8d
[SPARK-37178][ML] passing raw stats to model, building encodings in t…
8bd8527
Merge branch 'master' of https://github.com/rebo16v/spark-target-enco…
32adb85
[SPARK-37178][ML] disregard NaN-labeled observations
1796454
Merge branch 'master' of https://github.com/rebo16v/spark-target-enco…
fea4b6c
Merge branch 'master' of https://github.com/rebo16v/spark-target-enco…
7ca04f3
nits
HyukjinKwon 6236bd0
[SPARK-37178][ML] changed category datatype to Double
26410a1
Merge branch 'master' of https://github.com/rebo16v/spark-target-enco…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
90 changes: 90 additions & 0 deletions
90
examples/src/main/java/org/apache/spark/examples/ml/JavaTargetEncoderExample.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,90 @@ | ||
/* | ||
* 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.SparkSession; | ||
|
||
// $example on$ | ||
import org.apache.spark.ml.feature.TargetEncoder; | ||
import org.apache.spark.ml.feature.TargetEncoderModel; | ||
import org.apache.spark.sql.Dataset; | ||
import org.apache.spark.sql.Row; | ||
import org.apache.spark.sql.RowFactory; | ||
import org.apache.spark.sql.types.DataTypes; | ||
import org.apache.spark.sql.types.Metadata; | ||
import org.apache.spark.sql.types.StructField; | ||
import org.apache.spark.sql.types.StructType; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
// $example off$ | ||
|
||
public class JavaTargetEncoderExample { | ||
public static void main(String[] args) { | ||
SparkSession spark = SparkSession | ||
.builder() | ||
.appName("JavaTargetEncoderExample") | ||
.getOrCreate(); | ||
|
||
// Note: categorical features are usually first encoded with StringIndexer | ||
// $example on$ | ||
List<Row> data = Arrays.asList( | ||
RowFactory.create(0.0, 1.0, 0, 10.0), | ||
RowFactory.create(1.0, 0.0, 1, 20.0), | ||
RowFactory.create(2.0, 1.0, 0, 30.0), | ||
RowFactory.create(0.0, 2.0, 1, 40.0), | ||
RowFactory.create(0.0, 1.0, 0, 50.0), | ||
RowFactory.create(2.0, 0.0, 1, 60.0) | ||
); | ||
|
||
StructType schema = new StructType(new StructField[]{ | ||
new StructField("categoryIndex1", DataTypes.DoubleType, false, Metadata.empty()), | ||
new StructField("categoryIndex2", DataTypes.DoubleType, false, Metadata.empty()), | ||
new StructField("binaryLabel", DataTypes.DoubleType, false, Metadata.empty()), | ||
new StructField("continuousLabel", DataTypes.DoubleType, false, Metadata.empty()) | ||
}); | ||
|
||
Dataset<Row> df = spark.createDataFrame(data, schema); | ||
|
||
// binary target | ||
TargetEncoder bin_encoder = new TargetEncoder() | ||
.setInputCols(new String[] {"categoryIndex1", "categoryIndex2"}) | ||
.setOutputCols(new String[] {"categoryIndex1Target", "categoryIndex2Target"}) | ||
.setLabelCol("binaryLabel") | ||
.setTargetType("binary"); | ||
|
||
TargetEncoderModel bin_model = bin_encoder.fit(df); | ||
Dataset<Row> bin_encoded = bin_model.transform(df); | ||
bin_encoded.show(); | ||
|
||
// continuous target | ||
TargetEncoder cont_encoder = new TargetEncoder() | ||
.setInputCols(new String[] {"categoryIndex1", "categoryIndex2"}) | ||
.setOutputCols(new String[] {"categoryIndex1Target", "categoryIndex2Target"}) | ||
.setLabelCol("continuousLabel") | ||
.setTargetType("continuous"); | ||
HyukjinKwon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
TargetEncoderModel cont_model = cont_encoder.fit(df); | ||
Dataset<Row> cont_encoded = cont_model.transform(df); | ||
cont_encoded.show(); | ||
// $example off$ | ||
|
||
spark.stop(); | ||
} | ||
} | ||
|
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,65 @@ | ||
# | ||
# 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. | ||
# | ||
|
||
# $example on$ | ||
from pyspark.ml.feature import TargetEncoder | ||
|
||
# $example off$ | ||
from pyspark.sql import SparkSession | ||
|
||
if __name__ == "__main__": | ||
spark = SparkSession.builder.appName("TargetEncoderExample").getOrCreate() | ||
|
||
# Note: categorical features are usually first encoded with StringIndexer | ||
# $example on$ | ||
df = spark.createDataFrame( | ||
[ | ||
(0.0, 1.0, 0, 10.0), | ||
(1.0, 0.0, 1, 20.0), | ||
(2.0, 1.0, 0, 30.0), | ||
(0.0, 2.0, 1, 40.0), | ||
(0.0, 1.0, 0, 50.0), | ||
(2.0, 0.0, 1, 60.0), | ||
], | ||
["categoryIndex1", "categoryIndex2", "binaryLabel", "continuousLabel"], | ||
) | ||
|
||
# binary target | ||
encoder = TargetEncoder( | ||
inputCols=["categoryIndex1", "categoryIndex2"], | ||
outputCols=["categoryIndex1Target", "categoryIndex2Target"], | ||
labelCol="binaryLabel", | ||
targetType="binary" | ||
) | ||
model = encoder.fit(df) | ||
encoded = model.transform(df) | ||
encoded.show() | ||
|
||
# continuous target | ||
encoder = TargetEncoder( | ||
inputCols=["categoryIndex1", "categoryIndex2"], | ||
outputCols=["categoryIndex1Target", "categoryIndex2Target"], | ||
labelCol="continuousLabel", | ||
targetType="continuous" | ||
) | ||
|
||
model = encoder.fit(df) | ||
encoded = model.transform(df) | ||
encoded.show() | ||
# $example off$ | ||
|
||
spark.stop() |
71 changes: 71 additions & 0 deletions
71
examples/src/main/scala/org/apache/spark/examples/ml/TargetEncoderExample.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,71 @@ | ||
/* | ||
* 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.TargetEncoder | ||
// $example off$ | ||
import org.apache.spark.sql.SparkSession | ||
|
||
object TargetEncoderExample { | ||
def main(args: Array[String]): Unit = { | ||
val spark = SparkSession | ||
.builder() | ||
.appName("TargetEncoderExample") | ||
.getOrCreate() | ||
|
||
// Note: categorical features are usually first encoded with StringIndexer | ||
// $example on$ | ||
val df = spark.createDataFrame(Seq( | ||
(0.0, 1.0, 0, 10.0), | ||
(1.0, 0.0, 1, 20.0), | ||
(2.0, 1.0, 0, 30.0), | ||
(0.0, 2.0, 1, 40.0), | ||
(0.0, 1.0, 0, 50.0), | ||
(2.0, 0.0, 1, 60.0) | ||
)).toDF("categoryIndex1", "categoryIndex2", | ||
"binaryLabel", "continuousLabel") | ||
|
||
// binary target | ||
val bin_encoder = new TargetEncoder() | ||
.setInputCols(Array("categoryIndex1", "categoryIndex2")) | ||
.setOutputCols(Array("categoryIndex1Target", "categoryIndex2Target")) | ||
.setLabelCol("binaryLabel") | ||
.setTargetType("binary"); | ||
|
||
val bin_model = bin_encoder.fit(df) | ||
val bin_encoded = bin_model.transform(df) | ||
bin_encoded.show() | ||
|
||
// continuous target | ||
val cont_encoder = new TargetEncoder() | ||
.setInputCols(Array("categoryIndex1", "categoryIndex2")) | ||
.setOutputCols(Array("categoryIndex1Target", "categoryIndex2Target")) | ||
.setLabelCol("continuousLabel") | ||
.setTargetType("continuous"); | ||
|
||
val cont_model = cont_encoder.fit(df) | ||
val cont_encoded = cont_model.transform(df) | ||
cont_encoded.show() | ||
// $example off$ | ||
|
||
spark.stop() | ||
} | ||
} | ||
// scalastyle:on println |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Let's drop at least a link to information on what target encoding is here.
Also, the explanation you give in the PR about what this actually does to which types of input is valuable and should probably be here too, either here or below in discussion of what the parameters do in some detail.
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 it's ok now. what do you think?