Naive Bayes classifiers
A family of simple probabilistic, multiclass classifiers based on applying Bayes’ theorem with strong (naive) independence assumptions between every pair of features.
Naive Bayes can be trained very efficiently. With a single pass over the training data, it computes the conditional probability distribution of each feature given each label. For prediction, it applies Bayes’ theorem to compute the conditional probability distribution of each label given an observation.
1
import org.apache.spark.ml.classification.NaiveBayes
2
import org.apache.spark.ml.evaluation.MulticlassClassificationEvaluator
3
// Load the data stored in LIBSVM format as a DataFrame.
4
val data = spark.read.format("libsvm").load("file:///opt/spark/data/mllib/sample_libsvm_data.txt")
5
// Split the data into training and test sets (30% held out for testing)
6
val Array(trainingData, testData) = data.randomSplit(Array(0.7, 0.3), seed = 1234L)
7
// Train a NaiveBayes model.
8
val model = new NaiveBayes()
9
.fit(trainingData)
10
// Select example rows to display.
11
val predictions = model.transform(testData)
12
predictions.show()
13
// Select (prediction, true label) and compute test error
14
val evaluator = new MulticlassClassificationEvaluator()
15
.setLabelCol("label")
16
.setPredictionCol("prediction")
17
.setMetricName("accuracy")
18
val accuracy = evaluator.evaluate(predictions)
19
println(s"Test set accuracy = $accuracy")
20
​
21
/*
22
Output:
23
​
24
+-----+--------------------+--------------------+-----------+----------+
25
|label| features| rawPrediction|probability|prediction|
26
+-----+--------------------+--------------------+-----------+----------+
27
| 0.0|(692,[95,96,97,12...|[-173678.60946628...| [1.0,0.0]| 0.0|
28
| 0.0|(692,[98,99,100,1...|[-178107.24302988...| [1.0,0.0]| 0.0|
29
| 0.0|(692,[100,101,102...|[-100020.80519087...| [1.0,0.0]| 0.0|
30
| 0.0|(692,[124,125,126...|[-183521.85526462...| [1.0,0.0]| 0.0|
31
| 0.0|(692,[127,128,129...|[-183004.12461660...| [1.0,0.0]| 0.0|
32
| 0.0|(692,[128,129,130...|[-246722.96394714...| [1.0,0.0]| 0.0|
33
| 0.0|(692,[152,153,154...|[-208696.01108598...| [1.0,0.0]| 0.0|
34
| 0.0|(692,[153,154,155...|[-261509.59951302...| [1.0,0.0]| 0.0|
35
| 0.0|(692,[154,155,156...|[-217654.71748256...| [1.0,0.0]| 0.0|
36
| 0.0|(692,[181,182,183...|[-155287.07585335...| [1.0,0.0]| 0.0|
37
| 1.0|(692,[99,100,101,...|[-145981.83877498...| [0.0,1.0]| 1.0|
38
| 1.0|(692,[100,101,102...|[-147685.13694275...| [0.0,1.0]| 1.0|
39
| 1.0|(692,[123,124,125...|[-139521.98499849...| [0.0,1.0]| 1.0|
40
| 1.0|(692,[124,125,126...|[-129375.46702012...| [0.0,1.0]| 1.0|
41
| 1.0|(692,[126,127,128...|[-145809.08230799...| [0.0,1.0]| 1.0|
42
| 1.0|(692,[127,128,129...|[-132670.15737290...| [0.0,1.0]| 1.0|
43
| 1.0|(692,[128,129,130...|[-100206.72054749...| [0.0,1.0]| 1.0|
44
| 1.0|(692,[129,130,131...|[-129639.09694930...| [0.0,1.0]| 1.0|
45
| 1.0|(692,[129,130,131...|[-143628.65574273...| [0.0,1.0]| 1.0|
46
| 1.0|(692,[129,130,131...|[-129238.74023248...| [0.0,1.0]| 1.0|
47
+-----+--------------------+--------------------+-----------+----------+
48
only showing top 20 rows
49
​
50
Test set accuracy = 1.0
51
​
52
​
53
*/
Copied!
Last modified 1yr ago
Copy link