使用java在apache spark中的决策树实现问题

car*_*uso 6 java machine-learning decision-tree bigdata apache-spark

我正在尝试使用java和apache spark 1.0.0版本为决策树分类器实现简单的演示.我的基础是http://spark.apache.org/docs/1.0.0/mllib-decision-tree.html.到目前为止,我已经编写了下面列出的代码.

符合以下代码我得到错误:

org.apache.spark.mllib.tree.impurity.Impurity impurity = new org.apache.spark.mllib.tree.impurity.Entropy();
Run Code Online (Sandbox Code Playgroud)

类型不匹配:无法从Entropy转换为Impurity.这对我来说很奇怪,而类Entropy实现了Impurity接口:

https://spark.apache.org/docs/1.0.0/api/java/org/apache/spark/mllib/tree/impurity/Entropy.html

我正在寻找问题的答案为什么我不能做这个任务?

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<String, LabeledPoint> {
        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 < 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 < 1) {
            System.err.println("Usage:DecisionTreeDemo <file>");
            System.exit(1);
        }

        JavaSparkContext ctx = new JavaSparkContext("local[4]", "Log Analizer",
                System.getenv("SPARK_HOME"),
                JavaSparkContext.jarOfClass(DecisionTreeDemo.class));

        JavaRDD<String> lines = ctx.textFile(args[0]);
        JavaRDD<LabeledPoint> 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();
    }
}   
Run Code Online (Sandbox Code Playgroud)

@samthebest如果我删除杂质变量并改为以下形式:

Strategy strategy = new Strategy(Algo.Classification(), new org.apache.spark.mllib.tree.impurity.Entropy(), maxDepth, maxBins, null, null, maxMemory);
Run Code Online (Sandbox Code Playgroud)

错误更改为:构造函数Entropy()未定义.

[编辑]我发现我认为适当的方法调用(https://issues.apache.org/jira/browse/SPARK-2197):

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);
Run Code Online (Sandbox Code Playgroud)

不幸的是我遇到了bug :(

eme*_*cas 0

Bug 2197 的 Java 解决方案现已通过此拉取请求提供:

对决策树的其他改进使得 Java 易于使用: * 杂质类:添加了 instance() 方法以帮助使用 Java 接口。* 策略:添加了 Java 友好的构造函数 --> 注意:我从 Java 友好的构造函数中删除了 quantileCalculationStrategy,因为 (a) 它是一个特殊的类,并且 (b) 目前只有 1 个选项。我怀疑我们会在包含其他选项之前重做 API。

您可以在此处查看完整的示例,即使用基尼杂质的 intance() 方法来解决您的问题

Strategy strategy = new Strategy(Algo.Classification(), Gini.instance(), maxDepth, numClasses,maxBins, categoricalFeaturesInfo);
DecisionTreeModel model = DecisionTree$.MODULE$.train(rdd.rdd(), strategy);
Run Code Online (Sandbox Code Playgroud)