Auto-generate polynomial features for machine learning
Generating polynomial features for machine learning might look complicated at the first glance but it's really not:
def af[T](features: List[T], power: Int, combine: (T, T) => T) = {
def af0(acc: List[T], p: Int) : List[T] =
if( p == 0 ) acc else af0( acc ++ (for {
s1 <- acc
s2 <- features
} yield combine(s1,s2)), p-1)
af0(features, power-1)
}
Usage:
println(af[Int](List(2, 3), 2, (a: Int, b: Int) => a * b))
List(2, 3, 4, 6, 6, 9)
println(af[String](List("a", "b"), 2, (a: String, b: String) => (a + b)))
List(a, b, aa, ab, ba, bb)
println(af[String](List("a", "b"), 3, (a: String, b: String) => (a + b)))
List(a, b, aa, ab, ba, bb, aa, ab, ba, bb, aaa, aab, aba, abb, baa, bab, bba, bbb)
Written by Bela Ezsias
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Scala
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#