Leap Frame Operations
The basic operations of a leap frame are:
- Creating new columns and inserting data
- Dropping columns
- Reading row data
- Selecting a set of columns into a new leap frame
Insert
Generating new values from existing fields is a simple task. Justspecify the name of the output field, a list of input fields, and aScala function to generate the new output value.
// Insert some values into the leap frame
val leapFrame2 = leapFrame.withOutput("generated_field", "a_string", "an_int") {
(str: String, i: Int) => s"$str: $i"
}
// Extract our new data from the leap frame
val generatedStrings: Seq[String] = (for(lf <- leapFrame2;
lf2 <- lf.select("generated_field", "a_string", "an_int")) yield {
val str = lf2.dataset(0).getString(1) // get value of "a_string"
val i = lf2.dataset(0).getInt(2) // get value of "an_int"
assert(lf2.dataset(0).getString(0) == s"$str: $i")
lf2.dataset.map(_.getString(0))
}).get.toSeq
// Print out our generated strings
// > "Hello, MLeap!: 42"
// > "Another row: 43"
println(generatedStrings.mkString("\n"))
Insert Optional Value
Null values in MLeap are supported with the Scala Option
monad. Let’soutput some optionally null values in our leap frame.
// Insert some values into the leap frame
val leapFrame3 = leapFrame.withOutput("optional_int", "a_double", "an_int") {
(d: Double, i: Int) =>
if(i > 42) {
Some(777)
} else { None }
}
// Extract our new data from the leap frame
val optionalInts: Seq[Option[Int]] = (for(lf <- leapFrame3;
lf2 <- lf.select("optional_int")) yield {
lf2.dataset.map(_.optionInt(0))
}).get.toSeq
// Print out our optional ints
// > Some(777)
// > None
println(optionalInts.mkString("\n"))
Drop
Drop a field from the leap frame.
assert(leapFrame.schema.hasField("a_double"))
for(lf <- leapFrame.dropField("a_double")) {
assert(!lf.schema.hasField("a_double"))
}
Read
Gain access to the rows in the leap frame.
val data = leapFrame.dataset
assert(data.head == Row("Hello, MLeap!", 56.7d, 13.0f, 42, 67l))
assert(data(1) == Row("Another row", 23.4d, 11.0f, 43, 88l))
// Datasets are iterable over their rows
assert(data.toSeq.size == 2)
Select
Construct a new leap frame by selecting fields.
assert(leapFrame.schema.hasField("a_double"))
assert(leapFrame.schema.hasField("a_string"))
assert(leapFrame.schema.hasField("an_int"))
assert(leapFrame.schema.fields.size == 5)
for(lf <- leapFrame.select("a_double", "a_string")) {
assert(lf.schema.hasField("a_double"))
assert(lf.schema.hasField("a_string"))
assert(!lf.schema.hasField("an_int"))
assert(lf.schema.fields.size == 2)
}
当前内容版权归 combust.ml 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 combust.ml .