-
-
Notifications
You must be signed in to change notification settings - Fork 135
Add TypedOneHotEncoder #322
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
Open
manuzhang
wants to merge
7
commits into
typelevel:master
Choose a base branch
from
manuzhang:onehot
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6a068e0
Add TypedOneHotEncoder
manuzhang b51b337
Fix testing TypedOneHotEncoder#apply
manuzhang 7569bff
Update TypedOneHotEncoder.scala
cchantep 9d980e6
Update TypedOneHotEncoderTests.scala
cchantep 9e3f78d
Update TypedOneHotEncoder.scala
cchantep 11bf7d6
Update TypedOneHotEncoder.scala
cchantep 347f9bc
Update TypedOneHotEncoderTests.scala
cchantep 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
47 changes: 47 additions & 0 deletions
47
ml/src/main/scala/frameless/ml/feature/TypedOneHotEncoder.scala
This file contains hidden or 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,47 @@ | ||||||
| package frameless | ||||||
| package ml | ||||||
| package feature | ||||||
|
|
||||||
| import frameless.ml.feature.TypedOneHotEncoder.HandleInvalid | ||||||
| import frameless.ml.internals.UnaryInputsChecker | ||||||
| import org.apache.spark.ml.Estimator | ||||||
| import org.apache.spark.ml.feature.{OneHotEncoderEstimator, OneHotEncoderModel} | ||||||
| import org.apache.spark.ml.linalg.Vector | ||||||
|
|
||||||
| /** | ||||||
| * A one-hot encoder that maps a column of category indices to a column of binary vectors, with | ||||||
| * at most a single one-value per row that indicates the input category index. | ||||||
| * | ||||||
| * @see `TypedStringIndexer` for converting categorical values into category indices | ||||||
| */ | ||||||
| class TypedOneHotEncoder[Inputs] private[ml](oneHotEncoder: OneHotEncoderEstimator, inputCol: String) | ||||||
| extends TypedEstimator[Inputs, TypedOneHotEncoder.Outputs, OneHotEncoderModel] { | ||||||
|
|
||||||
| override val estimator: Estimator[OneHotEncoderModel] = oneHotEncoder | ||||||
| .setInputCols(Array(inputCol)) | ||||||
| .setOutputCols(Array(AppendTransformer.tempColumnName)) | ||||||
|
|
||||||
| def setHandleInvalid(value: HandleInvalid): TypedOneHotEncoder[Inputs] = | ||||||
| copy(oneHotEncoder.setHandleInvalid(value.sparkValue)) | ||||||
|
|
||||||
| def setDropLast(value: Boolean): TypedOneHotEncoder[Inputs] = | ||||||
| copy(oneHotEncoder.setDropLast(value)) | ||||||
|
|
||||||
| private def copy(newOneHotEncoder: OneHotEncoderEstimator): TypedOneHotEncoder[Inputs] = | ||||||
| new TypedOneHotEncoder[Inputs](newOneHotEncoder, inputCol) | ||||||
| } | ||||||
|
|
||||||
| object TypedOneHotEncoder { | ||||||
|
|
||||||
| case class Outputs(output: Vector) | ||||||
|
|
||||||
| sealed abstract class HandleInvalid(val sparkValue: String) | ||||||
|
Collaborator
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.
Suggested change
|
||||||
| object HandleInvalid { | ||||||
| case object Error extends HandleInvalid("error") | ||||||
| case object Keep extends HandleInvalid("keep") | ||||||
| } | ||||||
|
|
||||||
| def apply[Inputs](implicit inputsChecker: UnaryInputsChecker[Inputs, Int]): TypedOneHotEncoder[Inputs] = { | ||||||
|
cchantep marked this conversation as resolved.
Outdated
|
||||||
| new TypedOneHotEncoder[Inputs](new OneHotEncoderEstimator(), inputsChecker.inputCol) | ||||||
| } | ||||||
| } | ||||||
57 changes: 57 additions & 0 deletions
57
ml/src/test/scala/frameless/ml/feature/TypedOneHotEncoderTests.scala
This file contains hidden or 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,57 @@ | ||
| package frameless | ||
|
cchantep marked this conversation as resolved.
Outdated
|
||
| package ml | ||
| package feature | ||
|
|
||
| import frameless.ml.feature.TypedOneHotEncoder.HandleInvalid | ||
| import org.apache.spark.ml.linalg._ | ||
| import org.scalacheck.{Arbitrary, Gen} | ||
| import org.scalacheck.Prop._ | ||
| import shapeless.test.illTyped | ||
|
|
||
| class TypedOneHotEncoderTests extends FramelessMlSuite { | ||
|
|
||
| test(".fit() returns a correct TypedTransformer") { | ||
| implicit val arbInt = Arbitrary(Gen.choose(0, 99)) | ||
| def prop[A: TypedEncoder : Arbitrary] = forAll { (x2: X2[Int, A], dropLast: Boolean) => | ||
| val encoder = TypedOneHotEncoder[X1[Int]].setDropLast(dropLast) | ||
| val inputs = 0.to(x2.a).map(i => X2(i, x2.b)) | ||
| val ds = TypedDataset.create(inputs) | ||
| val model = encoder.fit(ds).run() | ||
| val resultDs = model.transform(TypedDataset.create(Seq(x2))).as[X3[Int, A, Vector]] | ||
| val result = resultDs.collect.run() | ||
| if (dropLast) { | ||
| result == Seq (X3(x2.a, x2.b, | ||
| Vectors.sparse(x2.a, Array.emptyIntArray, Array.emptyDoubleArray))) | ||
| } else { | ||
| result == Seq (X3(x2.a, x2.b, | ||
| Vectors.sparse(x2.a + 1, Array(x2.a), Array(1.0)))) | ||
| } | ||
| } | ||
|
|
||
| check(prop[Double]) | ||
| check(prop[String]) | ||
| } | ||
|
|
||
| test("param setting is retained") { | ||
| implicit val arbHandleInvalid: Arbitrary[HandleInvalid] = Arbitrary { | ||
| Gen.oneOf(HandleInvalid.Keep, HandleInvalid.Error) | ||
| } | ||
|
|
||
| val prop = forAll { handleInvalid: HandleInvalid => | ||
| val encoder = TypedOneHotEncoder[X1[Int]] | ||
| .setHandleInvalid(handleInvalid) | ||
| val ds = TypedDataset.create(Seq(X1(1))) | ||
| val model = encoder.fit(ds).run() | ||
|
|
||
| model.transformer.getHandleInvalid == handleInvalid.sparkValue | ||
| } | ||
|
|
||
| check(prop) | ||
| } | ||
|
|
||
| test("create() compiles only with correct inputs") { | ||
| illTyped("TypedOneHotEncoder.create[Double]()") | ||
|
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. I guess you meant to use |
||
| illTyped("TypedOneHotEncoder.create[X1[Double]]()") | ||
| illTyped("TypedOneHotEncoder.create[X2[String, Long]]()") | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.