[GitHub] flink pull request: [Flink-1727]Decision tree

2015-06-22 Thread thvasilo
Github user thvasilo commented on the pull request:

https://github.com/apache/flink/pull/710#issuecomment-114154438
  
Helo @sachingoel0101, I've started reviewing this PR. It would help a lot 
if you check out these initial comments I made and try to fix similar issues 
throughout the PR.

Then we can focus on the logic and design.

Also I would recommend you change the PR's name to [FLINK-1727] [ml] 
Decision tree
and add [WIP] if you consider this to be a work in progress.

Small question: I assume your are implementing the decision tree as 
described in the paper you linked? Could you motivate your decision to use that 
algorithm?


---
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.
---


[GitHub] flink pull request: [Flink-1727]Decision tree

2015-06-22 Thread thvasilo
Github user thvasilo commented on a diff in the pull request:

https://github.com/apache/flink/pull/710#discussion_r32941921
  
--- Diff: 
flink-staging/flink-ml/src/main/scala/org/apache/flink/ml/classification/DecisionTree.scala
 ---
@@ -0,0 +1,490 @@
+/*
+ * 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.classification
+
+import java.lang.Double.MAX_VALUE
+import java.util
+
+import org.apache.flink.api.common.functions.{RichFlatMapFunction, 
RichMapFunction}
+import org.apache.flink.api.scala._
+import org.apache.flink.configuration.Configuration
+import org.apache.flink.ml.common.FlinkMLTools.ModuloKeyPartitioner
+import org.apache.flink.ml.common._
+import org.apache.flink.ml.math.{CategoricalHistogram, 
ContinuousHistogram, OnlineHistogram, Vector}
+import org.apache.flink.ml.pipeline.{FitOperation, PredictOperation, 
Predictor}
+import org.apache.flink.ml.tree._
+import org.apache.flink.util.Collector
+
+import scala.collection.mutable
+
+class DecisionTree extends Predictor[DecisionTree] {
--- End diff --

Class docstring is required


---
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.
---


[GitHub] flink pull request: [Flink-1727]Decision tree

2015-06-22 Thread thvasilo
Github user thvasilo commented on a diff in the pull request:

https://github.com/apache/flink/pull/710#discussion_r32942116
  
--- Diff: 
flink-staging/flink-ml/src/main/scala/org/apache/flink/ml/classification/DecisionTree.scala
 ---
@@ -0,0 +1,490 @@
+/*
+ * 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.classification
+
+import java.lang.Double.MAX_VALUE
+import java.util
+
+import org.apache.flink.api.common.functions.{RichFlatMapFunction, 
RichMapFunction}
+import org.apache.flink.api.scala._
+import org.apache.flink.configuration.Configuration
+import org.apache.flink.ml.common.FlinkMLTools.ModuloKeyPartitioner
+import org.apache.flink.ml.common._
+import org.apache.flink.ml.math.{CategoricalHistogram, 
ContinuousHistogram, OnlineHistogram, Vector}
+import org.apache.flink.ml.pipeline.{FitOperation, PredictOperation, 
Predictor}
+import org.apache.flink.ml.tree._
+import org.apache.flink.util.Collector
+
+import scala.collection.mutable
+
+class DecisionTree extends Predictor[DecisionTree] {
+
+  import DecisionTree._
+
+  // store the decision tree learned after the fit operation
+  var treeOption: Option[DataSet[Tree]] = None
+
+  /** Sets the maximum allowed depth of the tree.
+* Currently only allowed values up to 30
+*
+* *@param depth
+* @return itself
+*/
+  def setDepth(depth: Int): DecisionTree = {
+require(depth = 30, Maximum depth allowed: 30)
+parameters.add(Depth, depth)
+this
+  }
+
+  /** Sets minimum number of instances that must be present at a node for 
its parent to split
+*
+* *@param minInstancesPerNode
+* @return itself
+*/
+  def setMinInstancePerNode(minInstancesPerNode: Int): DecisionTree = {
+require(minInstancesPerNode = 1,
+  Every node must have at least one instance associated with it)
+parameters.add(MinInstancesPerNode, minInstancesPerNode)
+this
+  }
+
+  /** Sets whether or not to prune the tree after building
+*
+* *@param prune
+* @return itself
+*/
+  def setPruning(prune: Boolean): DecisionTree = {
+parameters.add(Pruning, prune)
+this
+  }
+
+  /** Sets maximum number of bins to be used for calculating splits.
+*
+* *@param maxBins
+* @return itself
+*/
+  def setMaxBins(maxBins: Int): DecisionTree = {
+require(maxBins = 1, Maximum bins used must be at least one)
+parameters.add(MaxBins, maxBins)
+this
+  }
+
+  /** Sets the splitting strategy. Gini and Entropy supported.
+*
+* *@param splitStrategy
+* @return itself
+*/
+  def setSplitStrategy(splitStrategy: String): DecisionTree = {
+require(splitStrategy == Gini || splitStrategy == Entropy,
+  Algorithm  + splitStrategy +  not supported)
+parameters.add(SplitStrategy, splitStrategy)
+this
+  }
+
+  /** Sets the dimension of data. Will be cross checked with the data later
+*
+* *@param dimension
+* @return itself
+*/
+  def setDimension(dimension: Int): DecisionTree = {
+require(dimension = 1, Dimension cannot be less than one)
+parameters.add(Dimension, dimension)
+this
+  }
+
+  /** Sets which fields are to be considered categorical. Array of field 
indices
+*
+* *@param category
+* @return itself
+*/
+  def setCategory(category: Array[Int]): DecisionTree = {
+parameters.add(Category, category)
+this
+  }
+
+  /** Sets how many classes there are in the data [will be cross checked 
with the data later]
+*
+* *@param numClasses
+* @return itself
+*/
+  def setClasses(numClasses: Int): DecisionTree = {
+require(numClasses  1, There must be at least two classes in the 
data)
+

[GitHub] flink pull request: [Flink-1727]Decision tree

2015-06-22 Thread thvasilo
Github user thvasilo commented on a diff in the pull request:

https://github.com/apache/flink/pull/710#discussion_r32942958
  
--- 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(
--- End diff --

Formatting. We try to follow the rules as presented 
[here](https://github.com/databricks/scala-style-guide#indent)


---
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.
---


[GitHub] flink pull request: [Flink-1727]Decision tree

2015-06-22 Thread thvasilo
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 = sTree 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.
---


[GitHub] flink pull request: [Flink-1727]Decision tree

2015-06-22 Thread thvasilo
Github user thvasilo commented on a diff in the pull request:

https://github.com/apache/flink/pull/710#discussion_r32944309
  
--- Diff: 
flink-staging/flink-ml/src/main/scala/org/apache/flink/ml/classification/DecisionTree.scala
 ---
@@ -0,0 +1,490 @@
+/*
+ * 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.classification
+
+import java.lang.Double.MAX_VALUE
+import java.util
+
+import org.apache.flink.api.common.functions.{RichFlatMapFunction, 
RichMapFunction}
+import org.apache.flink.api.scala._
+import org.apache.flink.configuration.Configuration
+import org.apache.flink.ml.common.FlinkMLTools.ModuloKeyPartitioner
+import org.apache.flink.ml.common._
+import org.apache.flink.ml.math.{CategoricalHistogram, 
ContinuousHistogram, OnlineHistogram, Vector}
+import org.apache.flink.ml.pipeline.{FitOperation, PredictOperation, 
Predictor}
+import org.apache.flink.ml.tree._
+import org.apache.flink.util.Collector
+
+import scala.collection.mutable
+
+class DecisionTree extends Predictor[DecisionTree] {
+
+  import DecisionTree._
+
+  // store the decision tree learned after the fit operation
+  var treeOption: Option[DataSet[Tree]] = None
+
+  /** Sets the maximum allowed depth of the tree.
+* Currently only allowed values up to 30
+*
+* *@param depth
+* @return itself
+*/
+  def setDepth(depth: Int): DecisionTree = {
+require(depth = 30, Maximum depth allowed: 30)
+parameters.add(Depth, depth)
+this
+  }
+
+  /** Sets minimum number of instances that must be present at a node for 
its parent to split
+*
+* *@param minInstancesPerNode
+* @return itself
+*/
+  def setMinInstancePerNode(minInstancesPerNode: Int): DecisionTree = {
+require(minInstancesPerNode = 1,
+  Every node must have at least one instance associated with it)
+parameters.add(MinInstancesPerNode, minInstancesPerNode)
+this
+  }
+
+  /** Sets whether or not to prune the tree after building
+*
+* *@param prune
+* @return itself
+*/
+  def setPruning(prune: Boolean): DecisionTree = {
+parameters.add(Pruning, prune)
+this
+  }
+
+  /** Sets maximum number of bins to be used for calculating splits.
+*
+* *@param maxBins
+* @return itself
+*/
+  def setMaxBins(maxBins: Int): DecisionTree = {
+require(maxBins = 1, Maximum bins used must be at least one)
+parameters.add(MaxBins, maxBins)
+this
+  }
+
+  /** Sets the splitting strategy. Gini and Entropy supported.
+*
+* *@param splitStrategy
+* @return itself
+*/
+  def setSplitStrategy(splitStrategy: String): DecisionTree = {
+require(splitStrategy == Gini || splitStrategy == Entropy,
+  Algorithm  + splitStrategy +  not supported)
+parameters.add(SplitStrategy, splitStrategy)
+this
+  }
+
+  /** Sets the dimension of data. Will be cross checked with the data later
+*
+* *@param dimension
+* @return itself
+*/
+  def setDimension(dimension: Int): DecisionTree = {
+require(dimension = 1, Dimension cannot be less than one)
+parameters.add(Dimension, dimension)
+this
+  }
+
+  /** Sets which fields are to be considered categorical. Array of field 
indices
+*
+* *@param category
+* @return itself
+*/
+  def setCategory(category: Array[Int]): DecisionTree = {
+parameters.add(Category, category)
+this
+  }
+
+  /** Sets how many classes there are in the data [will be cross checked 
with the data later]
+*
+* *@param numClasses
+* @return itself
+*/
+  def setClasses(numClasses: Int): DecisionTree = {
+require(numClasses  1, There must be at least two classes in the 
data)
+

[GitHub] flink pull request: [Flink-1727]Decision tree

2015-06-22 Thread thvasilo
Github user thvasilo commented on a diff in the pull request:

https://github.com/apache/flink/pull/710#discussion_r32941719
  
--- Diff: 
flink-staging/flink-ml/src/main/scala/org/apache/flink/ml/classification/DecisionTree.scala
 ---
@@ -0,0 +1,490 @@
+/*
+ * 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.classification
+
+import java.lang.Double.MAX_VALUE
+import java.util
+
+import org.apache.flink.api.common.functions.{RichFlatMapFunction, 
RichMapFunction}
+import org.apache.flink.api.scala._
+import org.apache.flink.configuration.Configuration
+import org.apache.flink.ml.common.FlinkMLTools.ModuloKeyPartitioner
+import org.apache.flink.ml.common._
+import org.apache.flink.ml.math.{CategoricalHistogram, 
ContinuousHistogram, OnlineHistogram, Vector}
+import org.apache.flink.ml.pipeline.{FitOperation, PredictOperation, 
Predictor}
+import org.apache.flink.ml.tree._
+import org.apache.flink.util.Collector
+
+import scala.collection.mutable
+
+class DecisionTree extends Predictor[DecisionTree] {
+
+  import DecisionTree._
+
+  // store the decision tree learned after the fit operation
+  var treeOption: Option[DataSet[Tree]] = None
+
+  /** Sets the maximum allowed depth of the tree.
+* Currently only allowed values up to 30
+*
+* *@param depth
+* @return itself
+*/
+  def setDepth(depth: Int): DecisionTree = {
+require(depth = 30, Maximum depth allowed: 30)
+parameters.add(Depth, depth)
+this
+  }
+
+  /** Sets minimum number of instances that must be present at a node for 
its parent to split
+*
+* *@param minInstancesPerNode
+* @return itself
+*/
+  def setMinInstancePerNode(minInstancesPerNode: Int): DecisionTree = {
+require(minInstancesPerNode = 1,
+  Every node must have at least one instance associated with it)
+parameters.add(MinInstancesPerNode, minInstancesPerNode)
+this
+  }
+
+  /** Sets whether or not to prune the tree after building
+*
+* *@param prune
+* @return itself
+*/
+  def setPruning(prune: Boolean): DecisionTree = {
+parameters.add(Pruning, prune)
+this
+  }
+
+  /** Sets maximum number of bins to be used for calculating splits.
+*
+* *@param maxBins
+* @return itself
+*/
+  def setMaxBins(maxBins: Int): DecisionTree = {
+require(maxBins = 1, Maximum bins used must be at least one)
+parameters.add(MaxBins, maxBins)
+this
+  }
+
+  /** Sets the splitting strategy. Gini and Entropy supported.
+*
+* *@param splitStrategy
+* @return itself
+*/
+  def setSplitStrategy(splitStrategy: String): DecisionTree = {
+require(splitStrategy == Gini || splitStrategy == Entropy,
+  Algorithm  + splitStrategy +  not supported)
+parameters.add(SplitStrategy, splitStrategy)
+this
+  }
+
+  /** Sets the dimension of data. Will be cross checked with the data later
+*
+* *@param dimension
+* @return itself
+*/
+  def setDimension(dimension: Int): DecisionTree = {
+require(dimension = 1, Dimension cannot be less than one)
+parameters.add(Dimension, dimension)
+this
+  }
+
+  /** Sets which fields are to be considered categorical. Array of field 
indices
+*
+* *@param category
+* @return itself
+*/
+  def setCategory(category: Array[Int]): DecisionTree = {
+parameters.add(Category, category)
+this
+  }
+
+  /** Sets how many classes there are in the data [will be cross checked 
with the data later]
+*
+* *@param numClasses
+* @return itself
+*/
+  def setClasses(numClasses: Int): DecisionTree = {
+require(numClasses  1, There must be at least two classes in the 
data)
+

[GitHub] flink pull request: [Flink-1727]Decision tree

2015-06-22 Thread thvasilo
Github user thvasilo commented on a diff in the pull request:

https://github.com/apache/flink/pull/710#discussion_r32944368
  
--- Diff: 
flink-staging/flink-ml/src/main/scala/org/apache/flink/ml/classification/DecisionTree.scala
 ---
@@ -0,0 +1,490 @@
+/*
+ * 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.classification
+
+import java.lang.Double.MAX_VALUE
+import java.util
+
+import org.apache.flink.api.common.functions.{RichFlatMapFunction, 
RichMapFunction}
+import org.apache.flink.api.scala._
+import org.apache.flink.configuration.Configuration
+import org.apache.flink.ml.common.FlinkMLTools.ModuloKeyPartitioner
+import org.apache.flink.ml.common._
+import org.apache.flink.ml.math.{CategoricalHistogram, 
ContinuousHistogram, OnlineHistogram, Vector}
+import org.apache.flink.ml.pipeline.{FitOperation, PredictOperation, 
Predictor}
+import org.apache.flink.ml.tree._
+import org.apache.flink.util.Collector
+
+import scala.collection.mutable
+
+class DecisionTree extends Predictor[DecisionTree] {
+
+  import DecisionTree._
+
+  // store the decision tree learned after the fit operation
+  var treeOption: Option[DataSet[Tree]] = None
+
+  /** Sets the maximum allowed depth of the tree.
+* Currently only allowed values up to 30
+*
+* *@param depth
+* @return itself
+*/
+  def setDepth(depth: Int): DecisionTree = {
+require(depth = 30, Maximum depth allowed: 30)
+parameters.add(Depth, depth)
+this
+  }
+
+  /** Sets minimum number of instances that must be present at a node for 
its parent to split
+*
+* *@param minInstancesPerNode
+* @return itself
+*/
+  def setMinInstancePerNode(minInstancesPerNode: Int): DecisionTree = {
+require(minInstancesPerNode = 1,
+  Every node must have at least one instance associated with it)
+parameters.add(MinInstancesPerNode, minInstancesPerNode)
+this
+  }
+
+  /** Sets whether or not to prune the tree after building
+*
+* *@param prune
+* @return itself
+*/
+  def setPruning(prune: Boolean): DecisionTree = {
+parameters.add(Pruning, prune)
+this
+  }
+
+  /** Sets maximum number of bins to be used for calculating splits.
+*
+* *@param maxBins
+* @return itself
+*/
+  def setMaxBins(maxBins: Int): DecisionTree = {
+require(maxBins = 1, Maximum bins used must be at least one)
+parameters.add(MaxBins, maxBins)
+this
+  }
+
+  /** Sets the splitting strategy. Gini and Entropy supported.
+*
+* *@param splitStrategy
+* @return itself
+*/
+  def setSplitStrategy(splitStrategy: String): DecisionTree = {
+require(splitStrategy == Gini || splitStrategy == Entropy,
+  Algorithm  + splitStrategy +  not supported)
+parameters.add(SplitStrategy, splitStrategy)
+this
+  }
+
+  /** Sets the dimension of data. Will be cross checked with the data later
+*
+* *@param dimension
+* @return itself
+*/
+  def setDimension(dimension: Int): DecisionTree = {
+require(dimension = 1, Dimension cannot be less than one)
+parameters.add(Dimension, dimension)
+this
+  }
+
+  /** Sets which fields are to be considered categorical. Array of field 
indices
+*
+* *@param category
+* @return itself
+*/
+  def setCategory(category: Array[Int]): DecisionTree = {
+parameters.add(Category, category)
+this
+  }
+
+  /** Sets how many classes there are in the data [will be cross checked 
with the data later]
+*
+* *@param numClasses
+* @return itself
+*/
+  def setClasses(numClasses: Int): DecisionTree = {
+require(numClasses  1, There must be at least two classes in the 
data)
+

[GitHub] flink pull request: [Flink-1727]Decision tree

2015-06-22 Thread thvasilo
Github user thvasilo commented on a diff in the pull request:

https://github.com/apache/flink/pull/710#discussion_r32944538
  
--- Diff: 
flink-staging/flink-ml/src/main/scala/org/apache/flink/ml/classification/DecisionTree.scala
 ---
@@ -0,0 +1,490 @@
+/*
+ * 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.classification
+
+import java.lang.Double.MAX_VALUE
+import java.util
+
+import org.apache.flink.api.common.functions.{RichFlatMapFunction, 
RichMapFunction}
+import org.apache.flink.api.scala._
+import org.apache.flink.configuration.Configuration
+import org.apache.flink.ml.common.FlinkMLTools.ModuloKeyPartitioner
+import org.apache.flink.ml.common._
+import org.apache.flink.ml.math.{CategoricalHistogram, 
ContinuousHistogram, OnlineHistogram, Vector}
+import org.apache.flink.ml.pipeline.{FitOperation, PredictOperation, 
Predictor}
+import org.apache.flink.ml.tree._
+import org.apache.flink.util.Collector
+
+import scala.collection.mutable
+
+class DecisionTree extends Predictor[DecisionTree] {
+
+  import DecisionTree._
+
+  // store the decision tree learned after the fit operation
+  var treeOption: Option[DataSet[Tree]] = None
+
+  /** Sets the maximum allowed depth of the tree.
+* Currently only allowed values up to 30
+*
+* *@param depth
+* @return itself
+*/
+  def setDepth(depth: Int): DecisionTree = {
+require(depth = 30, Maximum depth allowed: 30)
+parameters.add(Depth, depth)
+this
+  }
+
+  /** Sets minimum number of instances that must be present at a node for 
its parent to split
+*
+* *@param minInstancesPerNode
+* @return itself
+*/
+  def setMinInstancePerNode(minInstancesPerNode: Int): DecisionTree = {
+require(minInstancesPerNode = 1,
+  Every node must have at least one instance associated with it)
+parameters.add(MinInstancesPerNode, minInstancesPerNode)
+this
+  }
+
+  /** Sets whether or not to prune the tree after building
+*
+* *@param prune
+* @return itself
+*/
+  def setPruning(prune: Boolean): DecisionTree = {
+parameters.add(Pruning, prune)
+this
+  }
+
+  /** Sets maximum number of bins to be used for calculating splits.
+*
+* *@param maxBins
+* @return itself
+*/
+  def setMaxBins(maxBins: Int): DecisionTree = {
+require(maxBins = 1, Maximum bins used must be at least one)
+parameters.add(MaxBins, maxBins)
+this
+  }
+
+  /** Sets the splitting strategy. Gini and Entropy supported.
+*
+* *@param splitStrategy
+* @return itself
+*/
+  def setSplitStrategy(splitStrategy: String): DecisionTree = {
+require(splitStrategy == Gini || splitStrategy == Entropy,
+  Algorithm  + splitStrategy +  not supported)
+parameters.add(SplitStrategy, splitStrategy)
+this
+  }
+
+  /** Sets the dimension of data. Will be cross checked with the data later
+*
+* *@param dimension
+* @return itself
+*/
+  def setDimension(dimension: Int): DecisionTree = {
+require(dimension = 1, Dimension cannot be less than one)
+parameters.add(Dimension, dimension)
+this
+  }
+
+  /** Sets which fields are to be considered categorical. Array of field 
indices
+*
+* *@param category
+* @return itself
+*/
+  def setCategory(category: Array[Int]): DecisionTree = {
+parameters.add(Category, category)
+this
+  }
+
+  /** Sets how many classes there are in the data [will be cross checked 
with the data later]
+*
+* *@param numClasses
+* @return itself
+*/
+  def setClasses(numClasses: Int): DecisionTree = {
+require(numClasses  1, There must be at least two classes in the 
data)
+

[GitHub] flink pull request: [Flink-1727]Decision tree

2015-06-22 Thread thvasilo
Github user thvasilo commented on a diff in the pull request:

https://github.com/apache/flink/pull/710#discussion_r32944486
  
--- Diff: 
flink-staging/flink-ml/src/main/scala/org/apache/flink/ml/classification/DecisionTree.scala
 ---
@@ -0,0 +1,490 @@
+/*
+ * 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.classification
+
+import java.lang.Double.MAX_VALUE
+import java.util
+
+import org.apache.flink.api.common.functions.{RichFlatMapFunction, 
RichMapFunction}
+import org.apache.flink.api.scala._
+import org.apache.flink.configuration.Configuration
+import org.apache.flink.ml.common.FlinkMLTools.ModuloKeyPartitioner
+import org.apache.flink.ml.common._
+import org.apache.flink.ml.math.{CategoricalHistogram, 
ContinuousHistogram, OnlineHistogram, Vector}
+import org.apache.flink.ml.pipeline.{FitOperation, PredictOperation, 
Predictor}
+import org.apache.flink.ml.tree._
+import org.apache.flink.util.Collector
+
+import scala.collection.mutable
+
+class DecisionTree extends Predictor[DecisionTree] {
+
+  import DecisionTree._
+
+  // store the decision tree learned after the fit operation
+  var treeOption: Option[DataSet[Tree]] = None
+
+  /** Sets the maximum allowed depth of the tree.
+* Currently only allowed values up to 30
+*
+* *@param depth
+* @return itself
+*/
+  def setDepth(depth: Int): DecisionTree = {
+require(depth = 30, Maximum depth allowed: 30)
+parameters.add(Depth, depth)
+this
+  }
+
+  /** Sets minimum number of instances that must be present at a node for 
its parent to split
+*
+* *@param minInstancesPerNode
+* @return itself
+*/
+  def setMinInstancePerNode(minInstancesPerNode: Int): DecisionTree = {
+require(minInstancesPerNode = 1,
+  Every node must have at least one instance associated with it)
+parameters.add(MinInstancesPerNode, minInstancesPerNode)
+this
+  }
+
+  /** Sets whether or not to prune the tree after building
+*
+* *@param prune
+* @return itself
+*/
+  def setPruning(prune: Boolean): DecisionTree = {
+parameters.add(Pruning, prune)
+this
+  }
+
+  /** Sets maximum number of bins to be used for calculating splits.
+*
+* *@param maxBins
+* @return itself
+*/
+  def setMaxBins(maxBins: Int): DecisionTree = {
+require(maxBins = 1, Maximum bins used must be at least one)
+parameters.add(MaxBins, maxBins)
+this
+  }
+
+  /** Sets the splitting strategy. Gini and Entropy supported.
+*
+* *@param splitStrategy
+* @return itself
+*/
+  def setSplitStrategy(splitStrategy: String): DecisionTree = {
+require(splitStrategy == Gini || splitStrategy == Entropy,
+  Algorithm  + splitStrategy +  not supported)
+parameters.add(SplitStrategy, splitStrategy)
+this
+  }
+
+  /** Sets the dimension of data. Will be cross checked with the data later
+*
+* *@param dimension
+* @return itself
+*/
+  def setDimension(dimension: Int): DecisionTree = {
+require(dimension = 1, Dimension cannot be less than one)
+parameters.add(Dimension, dimension)
+this
+  }
+
+  /** Sets which fields are to be considered categorical. Array of field 
indices
+*
+* *@param category
+* @return itself
+*/
+  def setCategory(category: Array[Int]): DecisionTree = {
+parameters.add(Category, category)
+this
+  }
+
+  /** Sets how many classes there are in the data [will be cross checked 
with the data later]
+*
+* *@param numClasses
+* @return itself
+*/
+  def setClasses(numClasses: Int): DecisionTree = {
+require(numClasses  1, There must be at least two classes in the 
data)
+