今天來介紹scikit-learn的貝式分類器裡面的多項單純貝氏分類器Multinomial Naive Bayes(官方文件)
這個分類器適用於離散數據,雖然想換換數據庫XD但是iris是離散數據XD只好繼續用
MultinomialNB()這裡可以使用平滑參數alpha
from sklearn.model_selection import train_test_split
from sklearn import datasets
from sklearn.metrics import accuracy_score
from sklearn.naive_bayes import MultinomialNB
iris = datasets.load_iris() #load進iris的資料庫
features = iris.feature_names
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
clf = MultinomialNB()
clf.fit(X_train, y_train) #MultinomialNB(alpha=1.0, class_prior=None, fit_prior=True) #alpha->smoothing parameter(0 for no smoothing)
y_result = clf.predict(X_test)
accuracy_score(y_test, y_result)
print("Number of mislabeled points out of a total %d points : %d"% (iris.data.shape[0],(y_test != y_result).sum()))