【问题标题】:how to predict the values in mllib如何预测 mllib 中的值
【发布时间】:2015-12-28 04:44:47
【问题描述】:

您好,我是 spark mllib 的新手。我已经有一个 r 模型。我正在尝试与 spark mllib 相同的模型。这里是 R 模型代码。

R 代码。

delhi <- read.delim("UItrain.txt", na.strings = "")  
delhi$lnprice <- log(delhi$price)
heddel <- lm(lnprice ~ bedrooms+ bathrooms+ area)
deltest <- read.delim("UItest.txt", na.strings = "") 
predict (heddel, deltest)

我正在使用 java 在 spark mllib 中尝试相同的 R 代码。

SparkConf conf = new SparkConf().setAppName("Linear Regression Example");
JavaSparkContext sc = new JavaSparkContext(conf);
String path = "UItrain.txt";
JavaRDD<String> data = sc.textFile(path);
JavaRDD<LabeledPoint> parsedData = data.map(
  new Function<String, LabeledPoint>() {
    public LabeledPoint call(String line) {
      String[] parts = line.split("\t");
      String[] features = parts[1].split("\t");
      double[] v = new double[features.length];
      for (int i = 0; i < features.length - 1; i++)
        v[i] = Double.parseDouble(features[i]);
      return new LabeledPoint(Double.parseDouble(parts[0]), Vectors.dense(v));
    }
  }
  );
 parsedData.cache();

// Building the model
 String input = "UItrain.txt";
 int data2 = "UItest.txt";
int numIterations = 100;
final LinearRegressionModel model =
  LinearRegressionWithSGD.train(JavaRDD.toRDD(parsedData), data2);

// Evaluate model on training examples and compute training error
JavaRDD<Tuple2<Double, Double>> valuesAndPreds = parsedData.map(
  new Function<LabeledPoint, Tuple2<Double, Double>>() {
    public Tuple2<Double, Double> call(LabeledPoint point) {
      double prediction = model.predict(point.features());
      return new Tuple2<Double, Double>(prediction, point.label());
    }
  }
);
double MSE = new JavaDoubleRDD(valuesAndPreds.map(
  new Function<Tuple2<Double, Double>, Object>() {
    public Object call(Tuple2<Double, Double> pair) {
      return Math.pow(pair._1() - pair._2(), 2.0);
    }
  }
).rdd()).mean();
System.out.println("training Mean Squared Error = " + MSE);

我在构建模型时遇到错误。任何帮助将不胜感激。

【问题讨论】:

    标签: r hadoop prediction apache-spark-mllib


    【解决方案1】:

    我认为您的错误在 data2 中 这里:

    final LinearRegressionModelmodel=LinearRegressionWithSGD.train(JavaRDD.toRDD(parsedData), data2)
    

    回归期望迭代次数,而是接收文本,

     int data2 = "UItest.txt";
    

    如果这不是错误,则编辑并打印错误。

    【讨论】:

      猜你喜欢
      • 2018-02-14
      • 2016-12-14
      • 1970-01-01
      • 2015-10-05
      • 2016-05-29
      • 2017-05-06
      • 1970-01-01
      • 1970-01-01
      • 2016-01-19
      相关资源
      最近更新 更多