Github user thvasilo commented on a diff in the pull request:

    https://github.com/apache/flink/pull/710#discussion_r32943140
  
    --- Diff: 
flink-staging/flink-ml/src/main/scala/org/apache/flink/ml/tree/Tree.scala ---
    @@ -0,0 +1,327 @@
    +/*
    + * 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.flink.ml.tree
    +
    +import java.util
    +
    +import org.apache.flink.ml.math.{ContinuousHistogram, OnlineHistogram, 
Vector}
    +
    +import scala.collection.mutable
    +
    +/** Tree structure. This is kind of maintained in an unconventional way.
    +  * We provide direct access to all nodes
    +  * The obvious assumption is that child of i are 2*i and 2*i+1, while 
parent of i is i/2
    +  *
    +  */
    +class Tree(
    +            val treeID: Int,
    +            val nodes: mutable.HashMap[Int, Node],
    +            val config: TreeConfiguration
    +            ) extends Serializable {
    +
    +  override def toString: String = {
    +    var ret = s"Tree ID=$treeID\nConfiguration:\n$config \nTree 
Structure:\n"
    +    for (i <- 1 to Math.pow(2, 20).toInt) {
    +      if (nodes.get(i).nonEmpty) {
    +        ret = ret + nodes.get(i).get.toString + "\n"
    +      }
    +    }
    +    ret
    +  }
    +
    +  /** Determines which node of the tree this vector will go to
    +    * If predict at any node is -1 and it has a split, we'll go down 
recursively
    +    *
    +    */
    +  def filter(vector: Vector): (Int, Double) = {
    +    var node: Node = nodes.get(1).get
    +    while (node.predict.round.toInt == -1 && node.split.nonEmpty) {
    +      if (node.split.get.getSplitDirection(vector)) {
    +        node = nodes.get(2 * node.id).get
    +      }
    +      else {
    +        node = nodes.get(2 * node.id + 1).get
    +      }
    +    }
    +    (node.id, node.predict)
    +  }
    +
    +  /** Splits the tree based on the histograms provided.
    +    * Returns an array of node ids which were split in the process
    +    *
    +    */
    +  def split(histogram: util.List[(Int, Int, Double, OnlineHistogram)]): 
Array[Int] = {
    +    val h = new mutable.HashMap[(Int, Int, Double), OnlineHistogram]()
    +    for (i <- 0 to histogram.size() - 1) {
    +      val ele = histogram.get(i)
    +      h.put((ele._1, ele._2, ele._3), ele._4)
    +    }
    +    val (splits, activeNodes) = calculateSplits(h)
    +
    +
    +    // first, every unlabeled leaf node must have sent something.
    +    // If not, its sibling will be stuck forever
    +    val finalNodes = new mutable.HashMap[Int, Int]()
    +    activeNodes.keysIterator.foreach(
    +      x => {
    +        // since this node received instances, it's sibling also must have 
done so
    +        // for the root node, which happens only at level zero, there is 
no sibling. So just add it
    +        if (x != 1) {
    +          val y = 2 * (x / 2) + 1 - x % 2
    +          if (activeNodes.get(y).isEmpty) {
    +            finalNodes.put(x, 1)
    +          } else {
    +            finalNodes.put(x, -1)
    +          }
    +        } else {
    +          finalNodes.put(x, -1)
    +        }
    +      }
    +    )
    +    evaluateNodes(finalNodes, splits, h)
    +  }
    +
    +  private def evaluateNodes(
    --- End diff --
    
    Formatting here as well


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to