title: Decision tree implementation issue in apache spark with java
body: <p>I'm trying to implement simple demo for decision tree classifier using java and apache spark 1.0.0 version. I base on <a href="http://spark.apache.org/docs/1.0.0/mllib-decision-tree.html" rel="nofollow">http://spark.apache.org/docs/1.0.0/mllib-decision-tree.html</a>. So far I've wrote code listed below.     </p>
  
  <p>in line with following code I get error:</p>
  
  <pre><code>org.apache.spark.mllib.tree.impurity.Impurity impurity = new org.apache.spark.mllib.tree.impurity.Entropy();
  </code></pre>
  
  <p>Type mismatch: cannot convert from Entropy to Impurity.
  It's strange for me, while class Entropy implements Impurity interface:</p>
  
  <p><a href="https://spark.apache.org/docs/1.0.0/api/java/org/apache/spark/mllib/tree/impurity/Entropy.html" rel="nofollow">https://spark.apache.org/docs/1.0.0/api/java/org/apache/spark/mllib/tree/impurity/Entropy.html</a></p>
  
  <p>I'm looking answer for question why I can't make this assignment?</p>
  
  <pre><code>package decisionTree;
  
  import java.util.regex.Pattern;
  
  import org.apache.spark.api.java.JavaRDD;
  import org.apache.spark.api.java.JavaSparkContext;
  import org.apache.spark.api.java.function.Function;
  import org.apache.spark.mllib.linalg.Vectors;
  import org.apache.spark.mllib.regression.LabeledPoint;
  import org.apache.spark.mllib.tree.DecisionTree;
  import org.apache.spark.mllib.tree.configuration.Algo;
  import org.apache.spark.mllib.tree.configuration.Strategy;
  import org.apache.spark.mllib.tree.impurity.Gini;
  import org.apache.spark.mllib.tree.impurity.Impurity;
  
  import scala.Enumeration.Value;
  
  public final class DecisionTreeDemo {
  
      static class ParsePoint implements Function&lt;String, LabeledPoint&gt; {
          private static final Pattern COMMA = Pattern.compile(",");
          private static final Pattern SPACE = Pattern.compile(" ");
  
          @Override
          public LabeledPoint call(String line) {
              String[] parts = COMMA.split(line);
              double y = Double.parseDouble(parts[0]);
              String[] tok = SPACE.split(parts[1]);
              double[] x = new double[tok.length];
              for (int i = 0; i &lt; tok.length; ++i) {
                  x[i] = Double.parseDouble(tok[i]);
              }
              return new LabeledPoint(y, Vectors.dense(x));
          }
      }
  
      public static void main(String[] args) throws Exception {
  
          if (args.length &lt; 1) {
              System.err.println("Usage:DecisionTreeDemo &lt;file&gt;");
              System.exit(1);
          }
  
          JavaSparkContext ctx = new JavaSparkContext("local[4]", "Log Analizer",
                  System.getenv("SPARK_HOME"),
                  JavaSparkContext.jarOfClass(DecisionTreeDemo.class));
  
          JavaRDD&lt;String&gt; lines = ctx.textFile(args[0]);
          JavaRDD&lt;LabeledPoint&gt; points = lines.map(new ParsePoint()).cache();
  
          int iterations = 100;
  
          int maxBins = 2;
          int maxMemory = 512;
          int maxDepth = 1;
  
          org.apache.spark.mllib.tree.impurity.Impurity impurity = new org.apache.spark.mllib.tree.impurity.Entropy();
  
          Strategy strategy = new Strategy(Algo.Classification(), impurity, maxDepth,
                  maxBins, null, null, maxMemory);
  
          ctx.stop();
      }
  }   
  </code></pre>
  
  <p>@samthebest if I remove impurity variable and change to following form: </p>
  
  <pre><code>Strategy strategy = new Strategy(Algo.Classification(), new org.apache.spark.mllib.tree.impurity.Entropy(), maxDepth, maxBins, null, null, maxMemory);
  </code></pre>
  
  <p>error changed to: The constructor Entropy() is undefined.</p>
  
  <p>[edited]
  I found I think proper invocation of method (<a href="https://issues.apache.org/jira/browse/SPARK-2197" rel="nofollow">https://issues.apache.org/jira/browse/SPARK-2197</a>):</p>
  
  <pre><code>Strategy strategy = new Strategy(Algo.Classification(), new Impurity() {
  @Override
  public double calculate(double arg0, double arg1, double arg2)
  { return Gini.calculate(arg0, arg1, arg2); }
  
  @Override
  public double calculate(double arg0, double arg1)
  { return Gini.calculate(arg0, arg1); }
  
  }, 5, 100, QuantileStrategy.Sort(), null, 256);
  </code></pre>
  
  <p>Unfortunately I run into bug :(</p>