樸貝氏分類器 - 基於 RDD 的 API

樸貝氏分類器 是一種簡單的多類別分類演算法,假設每一對特徵之間是獨立的。樸貝氏分類器可以非常有效率地訓練。在訓練資料中執行一次傳遞時,它會計算每個特徵在給定標籤下的條件機率分配,然後套用貝氏定理來計算在給定觀察值下的標籤條件機率分配,並用於預測。

spark.mllib 支援 多項式樸貝氏分類器伯努利樸貝氏分類器。這些模型通常用於 文件分類。在該脈絡中,每個觀察值都是一個文件,每個特徵代表一個詞彙,其值是詞彙的頻率(在多項式樸貝氏分類器中)或一個零或一,表示是否在文件中找到該詞彙(在伯努利樸貝氏分類器中)。特徵值必須是非負數。模型類型會使用選用參數「multinomial」或「bernoulli」來選取,預設為「multinomial」。加法平滑 可透過設定參數 $\lambda$(預設為 $1.0$)來使用。對於文件分類,輸入特徵向量通常是稀疏的,應提供稀疏向量作為輸入,以利用稀疏性。由於訓練資料只使用一次,因此不需要快取它。

範例

NaiveBayes 實作多項式樸貝氏分類器。它會輸入一個 LabeledPoint 的 RDD 和一個選用的平滑參數 lambda,並輸出一個 NaiveBayesModel,可用於評估和預測。

請注意,Python API 目前不支援模型儲存/載入,但未來會支援。

請參閱 NaiveBayes Python 文件NaiveBayesModel Python 文件,以取得更多有關 API 的詳細資料。

from pyspark.mllib.classification import NaiveBayes, NaiveBayesModel
from pyspark.mllib.util import MLUtils



# Load and parse the data file.
data = MLUtils.loadLibSVMFile(sc, "data/mllib/sample_libsvm_data.txt")

# Split data approximately into training (60%) and test (40%)
training, test = data.randomSplit([0.6, 0.4])

# Train a naive Bayes model.
model = NaiveBayes.train(training, 1.0)

# Make prediction and test accuracy.
predictionAndLabel = test.map(lambda p: (model.predict(p.features), p.label))
accuracy = 1.0 * predictionAndLabel.filter(lambda pl: pl[0] == pl[1]).count() / test.count()
print('model accuracy {}'.format(accuracy))

# Save and load model
output_dir = 'target/tmp/myNaiveBayesModel'
shutil.rmtree(output_dir, ignore_errors=True)
model.save(sc, output_dir)
sameModel = NaiveBayesModel.load(sc, output_dir)
predictionAndLabel = test.map(lambda p: (sameModel.predict(p.features), p.label))
accuracy = 1.0 * predictionAndLabel.filter(lambda pl: pl[0] == pl[1]).count() / test.count()
print('sameModel accuracy {}'.format(accuracy))
在 Spark 儲存庫中「examples/src/main/python/mllib/naive_bayes_example.py」中找到完整的範例程式碼。

NaiveBayes 實作多項式樸素貝氏分類法。它會將 LabeledPoint 的 RDD 和一個選用的平滑參數 lambda 作為輸入,一個選用的模型類型參數(預設為「多項式」),並輸出一個 NaiveBayesModel,可用於評估和預測。

請參閱 NaiveBayes Scala 文件NaiveBayesModel Scala 文件 以取得 API 的詳細資訊。

import org.apache.spark.mllib.classification.{NaiveBayes, NaiveBayesModel}
import org.apache.spark.mllib.util.MLUtils

// Load and parse the data file.
val data = MLUtils.loadLibSVMFile(sc, "data/mllib/sample_libsvm_data.txt")

// Split data into training (60%) and test (40%).
val Array(training, test) = data.randomSplit(Array(0.6, 0.4))

val model = NaiveBayes.train(training, lambda = 1.0, modelType = "multinomial")

val predictionAndLabel = test.map(p => (model.predict(p.features), p.label))
val accuracy = 1.0 * predictionAndLabel.filter(x => x._1 == x._2).count() / test.count()

// Save and load model
model.save(sc, "target/tmp/myNaiveBayesModel")
val sameModel = NaiveBayesModel.load(sc, "target/tmp/myNaiveBayesModel")
在 Spark 儲存庫的「examples/src/main/scala/org/apache/spark/examples/mllib/NaiveBayesExample.scala」中,尋找完整的範例程式碼。

NaiveBayes 實作多項式樸素貝氏分類法。它會將 LabeledPoint 的 Scala RDD 和一個選用的平滑參數 lambda 作為輸入,並輸出一個 NaiveBayesModel,可用於評估和預測。

請參閱 NaiveBayes Java 文件NaiveBayesModel Java 文件 以取得 API 的詳細資訊。

import scala.Tuple2;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.mllib.classification.NaiveBayes;
import org.apache.spark.mllib.classification.NaiveBayesModel;
import org.apache.spark.mllib.regression.LabeledPoint;
import org.apache.spark.mllib.util.MLUtils;

String path = "data/mllib/sample_libsvm_data.txt";
JavaRDD<LabeledPoint> inputData = MLUtils.loadLibSVMFile(jsc.sc(), path).toJavaRDD();
JavaRDD<LabeledPoint>[] tmp = inputData.randomSplit(new double[]{0.6, 0.4});
JavaRDD<LabeledPoint> training = tmp[0]; // training set
JavaRDD<LabeledPoint> test = tmp[1]; // test set
NaiveBayesModel model = NaiveBayes.train(training.rdd(), 1.0);
JavaPairRDD<Double, Double> predictionAndLabel =
  test.mapToPair(p -> new Tuple2<>(model.predict(p.features()), p.label()));
double accuracy =
  predictionAndLabel.filter(pl -> 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");
在 Spark 儲存庫的「examples/src/main/java/org/apache/spark/examples/mllib/JavaNaiveBayesExample.java」中,尋找完整的範例程式碼。