Advertisement
Guest User

Logistic

a guest
Sep 7th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1.  SparkConf configuration = new SparkConf().setAppName("logisApp");
  2.                     SparkContext sparkContext = new SparkContext(configuration);
  3.                     Logger rootLogger = Logger.getRootLogger();
  4.                     rootLogger.setLevel(Level.ERROR);
  5.  
  6.                                        
  7.                     JavaRDD<LabeledPoint> allData = dataHelper.readTextFromfile(sparkContext, "path/to/all/data");;
  8.  
  9.                     // Split initial RDD into two... [60% training data, 40%
  10.                     // testing data].
  11.  
  12.                     JavaRDD<LabeledPoint>[] parts = allData.randomSplit(new double[] { 0.6, 0.4 }, 11L);
  13.                     JavaRDD<LabeledPoint> trainingData = parts[0];
  14.                     JavaRDD<LabeledPoint> valdidationData = parts[1];
  15.  
  16.                     //
  17.                     // Run training algorithm to build the model.
  18.                     LogisticRegressionWithLBFGS logisticRegression = new LogisticRegressionWithLBFGS().setNumClasses(2);
  19.                     logisticRegression.optimizer().setUpdater(new L1Updater());
  20.                     final LogisticRegressionModel model = logisticRegression.run(trainingData.rdd());
  21.                  
  22.                    
  23.                     //
  24.                     // Compute raw scores on the test set.
  25.                     JavaRDD<Tuple2<Object, Object>> predict = valdidationData
  26.                             .map(new Function<LabeledPoint, Tuple2<Object, Object>>() {
  27.                                 public Tuple2<Object, Object> call(LabeledPoint point) {
  28.                                     Double prediction = model.predict(point.features());
  29.                                     return new Tuple2<Object, Object>(prediction, point.label());
  30.                                 }
  31.                             });
  32.                     //
  33.                     // Get evaluation metrics.
  34.                     BinaryClassificationMetrics metrics = new BinaryClassificationMetrics(predict.rdd());
  35.                    
  36.                     // Precision
  37.                     JavaRDD<Tuple2<Object, Object>> precision = metrics.precisionByThreshold().toJavaRDD();
  38.  
  39.                     // F Score
  40.                     JavaRDD<Tuple2<Object, Object>> f1Score = metrics.fMeasureByThreshold().toJavaRDD();
  41.                     System.out.println("    F1 Score : " + f1Score.collect());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement