我對此很陌生。如何組合多個樸素貝葉斯分類器的輸出?
我有一套在Sklearn工具包中使用樸素貝葉斯分類器(NBC)構建的弱分類器。
我的問題是如何結合每個NBC的輸出做出最終決定。我想我的決定是在概率而不是標籤。
我在python中做了下面的程序。我假設sklean中的虹膜數據集有2類問題。對於演示/學習,我說我做了一個4 NBC如下。
from sklearn import datasets
from sklearn.naive_bayes import GaussianNB
import numpy as np
import cPickle
import math
iris = datasets.load_iris()
gnb1 = GaussianNB()
gnb2 = GaussianNB()
gnb3 = GaussianNB()
gnb4 = GaussianNB()
#Actual dataset is of 3 class I just made it into 2 class for this demo
target = np.where(iris.target, 2, 1)
gnb1.fit(iris.data[:, 0].reshape(150,1), target)
gnb2.fit(iris.data[:, 1].reshape(150,1), target)
gnb3.fit(iris.data[:, 2].reshape(150,1), target)
gnb4.fit(iris.data[:, 3].reshape(150,1), target)
#y_pred = gnb.predict(iris.data)
index = 0
y_prob1 = gnb1.predict_proba(iris.data[index,0].reshape(1,1))
y_prob2 = gnb2.predict_proba(iris.data[index,1].reshape(1,1))
y_prob3 = gnb3.predict_proba(iris.data[index,2].reshape(1,1))
y_prob4 = gnb4.predict_proba(iris.data[index,3].reshape(1,1))
#print y_prob1, "\n", y_prob2, "\n", y_prob3, "\n", y_prob4
# I just added it over all for each class
pos = y_prob1[:,1] + y_prob2[:,1] + y_prob3[:,1] + y_prob4[:,1]
neg = y_prob1[:,0] + y_prob2[:,0] + y_prob3[:,0] + y_prob4[:,0]
print pos
print neg
正如您會注意到的,我只是簡單地將每個NBC的概率添加爲最終得分。我想知道這是否正確?
如果我沒有錯,你可以請建議一些想法,這樣我可以糾正自己。
感謝您的回覆。實際上,我在我的程序中創建了多個GNB,因爲我很困惑,所以我決定檢查理解這個概念。感謝您指導正確的道路。除此之外,我很困惑,就像你說的我們可以添加日誌或多次響應一樣。我如何確定查詢矢量屬於哪個類?因爲我會添加或多個響應,我會得到標量值,所以如何獲得類信息。 –
你可以用更大的概率對班級進行分類,這就是我所看到的全部 – lejlot
。只是爲了檢查我是否有這個想法。正如你在解決方案中提到的兩點。如果我選擇使用(1)單個NB,而不是我不必添加或多個,而是我可以在sklearn中使用predict_log_proba()?我假設這個函數完成你在(2)中所說的內部。它是否正確?我很抱歉我缺乏理解。 –