1. 程式人生 > >Spark-MLlib的快速使用之二(樸素貝葉斯分類)

Spark-MLlib的快速使用之二(樸素貝葉斯分類)

(1)演算法描述

演算法介紹:

樸素貝葉斯法是基於貝葉斯定理與特徵條件獨立假設的分類方法。

樸素貝葉斯的思想基礎是這樣的:對於給出的待分類項,求解在此項出現的條件下各個類別出現的概率,在沒有其它可用資訊下,我們會選擇條件概率最大的類別作為此待分類項應屬的類別。

(2)測試資料

1 125:145 126:255 127:211 128:31 152:32 153:237 154:253 155:252 156:71 180:11 181:175 182:253 183:252 184:71 209:144 210:253 211:252 212:71 236:16 237:191 238:253 239:252 240:71 264:26 265:221 266:253 267:252 268:124 269:31 293:125 294:253 295:252 296:252 297:108 322:253 323:252 324:252 325:108 350:255 351:253 352:253 353:108 378:253 379:252 380:252 381:108 406:253 407:252 408:252 409:108 434:253 435:252 436:252 437:108 462:255 463:253 464:253 465:170 490:253 491:252 492:252 493:252 494:42 518:149 519:252 520:252 521:252 522:144 546:109 547:252 548:252 549:252 550:144 575:218 576:253 577:253 578:255 579:35 603:175 604:252 605:252 606:253 607:35 631:73 632:252 633:252 634:253 635:35 659:31 660:211 661:252 662:253 663:35

(3)測試程式

 

public class JavaNaiveBayesExample {

public static void main(String[] args) {

SparkConf sparkConf = new SparkConf().setAppName("JavaNaiveBayesExample").setMaster("local");

JavaSparkContext jsc = new JavaSparkContext(sparkConf);

// $example on$

String path = "sample_libsvm_data.txt";

JavaRDD<LabeledPoint> inputData = MLUtils.loadLibSVMFile(jsc.sc(), path).toJavaRDD();

JavaRDD<LabeledPoint>[] tmp = inputData.randomSplit(new double[]{0.6, 0.4}, 12345);

JavaRDD<LabeledPoint> training = tmp[0]; // training set

JavaRDD<LabeledPoint> test = tmp[1]; // test set

final NaiveBayesModel model = NaiveBayes.train(training.rdd(), 1.0);

JavaPairRDD<Double, Double> predictionAndLabel =

test.mapToPair(new PairFunction<LabeledPoint, Double, Double>() {

@Override

public Tuple2<Double, Double> call(LabeledPoint p) {

return new Tuple2<Double, Double>(model.predict(p.features()), p.label());

}

});

System.out.println("----------------->"+predictionAndLabel.take(10));

double accuracy = predictionAndLabel.filter(new Function<Tuple2<Double, Double>, Boolean>() {

@Override

public Boolean call(Tuple2<Double, Double> pl) {

return pl._1().equals(pl._2());

}

}).count() / (double) test.count();

 

// Save and load model

model.save(jsc.sc(), "target/tmp/myNaiveBayesModel");

NaiveBayesModel sameModel = NaiveBayesModel.load(jsc.sc(), "target/tmp/myNaiveBayesModel");

// $example off$

}

}

(4)測試結果

 

[(1.0,1.0), (0.0,0.0), (1.0,1.0), (0.0,0.0), (0.0,0.0), (1.0,1.0), (0.0,0.0), (0.0,0.0), (0.0,0.0), (0.0,0.0)]