-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathOneHotEncoderExample.scala
More file actions
26 lines (22 loc) · 1020 Bytes
/
OneHotEncoderExample.scala
File metadata and controls
26 lines (22 loc) · 1020 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package io.picnicml.doddlemodel.examples.preprocessing
import breeze.linalg.DenseMatrix
import io.picnicml.doddlemodel.data.Feature.{CategoricalFeature, FeatureIndex, NumericalFeature}
import io.picnicml.doddlemodel.preprocessing.OneHotEncoder
import io.picnicml.doddlemodel.syntax.TransformerSyntax._
object OneHotEncoderExample extends App {
val x = DenseMatrix(
List(1.0, 1.0, 1.0),
List(3.0, 0.0, 1.0),
List(6.0, 2.0, 0.0)
)
val featureIndex = FeatureIndex(List(CategoricalFeature, NumericalFeature, CategoricalFeature))
println(s"features: $featureIndex")
// encode all categorical features
val oneHotEncoder = OneHotEncoder(featureIndex)
val fittedOneHotEncoder = oneHotEncoder.fit(x)
println(s"encoded data:\n${fittedOneHotEncoder.transform(x)}")
// only encode the last feature
val oneHotEncoderSubset = OneHotEncoder(featureIndex.subset(2))
val fittedOneHotEncoderSubset = oneHotEncoderSubset.fit(x)
println(s"encoded data:\n${fittedOneHotEncoderSubset.transform(x)}")
}