如何预测mllib中的值

qnyhuwrf  于 2021-05-29  发布在  Hadoop
关注(0)|答案(1)|浏览(305)

嗨,我是新的Sparkmllib。我已经有一个r模型。我正在尝试与Sparkmllib相同的模型。这里是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);

我得到错误,而建立模型。任何帮助将不胜感激。

cu6pst1q

cu6pst1q1#

我认为你的错误在这里的数据2中:

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

回归需要迭代次数,而不是接收文本,

int data2 = "UItest.txt";

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

相关问题