sklearn.naive_bayes.ComplementNB?
class sklearn.naive_bayes.ComplementNB(*,alpha = 1.0,fit_prior = True,class_prior = None,norm = False )
[源碼]
在Rennie et al. (2003)中描述的補充樸素貝葉斯分類器。
補充樸素貝葉斯分類器旨在糾正標準多項樸素貝葉斯分類器所做的“嚴重假設”。它特別適合用于不平衡的數據集。
在用戶指南中閱讀更多內容。
0.20版中的新功能。
參數 | 說明 |
---|---|
alpha | float, default=1.0 附加的(Laplace/Lidstone)平滑參數(0表示不平滑) |
fit_prior | bool, default=True 是否學習類別先驗概率。如果為False,將使用統一的先驗。 |
class_prior | array-like of shape (n_classes,), default=None 類別的先驗概率。一經指定先驗概率不能隨著數據而調整。 |
norm | bool, default=False 是否對權重進行第二次標準化。默認行為反映了Mahout和Weka中的實現,這些實現未遵循本文表9中描述的完整算法。 |
屬性 | 說明 |
---|---|
class_count_ | ndarray of shape (n_classes,) 擬合期間每個類別遇到的樣本數。此值由提供的樣本權重加權。 |
class_log_prior_ | ndarray of shape (n_classes,) 每個類別的對數概率(平滑)。 |
classes_ | ndarray of shape (n_classes,) 分類器已知的類別標簽 |
feature_all_ | ndarray of shape (n_features,) 擬合期間每個特征遇到的樣本數。此值由提供的樣本權重加權。 |
feature_count_ | ndarray of shape (n_classes, n_features) 擬合期間每個(類別,特征)遇到的樣本數。此值由提供的樣本權重加權。 |
feature_log_prob_ | ndarray of shape (n_classes, n_features) 類別補充的經驗權重 |
n_features_ | int 每個樣本的特征數量。 |
參考文獻
Rennie, J. D., Shih, L., Teevan, J., & Karger, D. R. (2003). Tackling the poor assumptions of naive bayes text classifiers. In ICML (Vol. 3, pp. 616-623). https://people.csail.mit.edu/jrennie/papers/icml03-nb.pdf
示例
>>> import numpy as np
>>> rng = np.random.RandomState(1)
>>> X = rng.randint(5, size=(6, 100))
>>> y = np.array([1, 2, 3, 4, 5, 6])
>>> from sklearn.naive_bayes import ComplementNB
>>> clf = ComplementNB()
>>> clf.fit(X, y)
ComplementNB()
>>> print(clf.predict(X[2:3]))
[3]
方法
方法 | 說明 |
---|---|
fit (X, y[, sample_weight]) |
根據X,y擬合樸素貝葉斯分類器 |
get_params ([deep]) |
獲取這個估計器的參數 |
partial_fit (X, y[, classes, sample_weight]) |
對一批樣本進行增量擬合 |
predict (X) |
對測試向量X進行分類。 |
predict_log_proba (X) |
返回針對測試向量X的對數概率估計 |
predict_proba (X) |
返回針對測試向量X的概率估計 |
score (X, y[, sample_weight]) |
返回給定測試數據和標簽上的平均準確率。 |
set_params (**params) |
為這個估計器設置參數 |
__init__(*, alpha=1.0, fit_prior=True, class_prior=None, norm=False)
[源碼]
初始化self。詳情可參閱 type(self)的幫助。
fit(X, y, sample_weight=None)
[源碼]
根據X,y擬合樸素貝葉斯分類器
參數 | 說明 |
---|---|
X | {array-like, sparse matrix} of shape (n_samples, n_features) 用于訓練的向量,其中n_samples是樣本數量,n_features是特征數量。 |
y | array-like of shape (n_samples,) 目標值 |
sample_weight | array-like of shape (n_samples,), default=None 應用于單個樣本的權重(1.未加權)。 |
返回值 | 說明 |
---|---|
self | object |
get_params(deep=True)
[源碼]
獲取這個估計器的參數
參數 | 說明 |
---|---|
deep | bool, default=True 如果為True,則將返回這個估計器的參數和所包含的估計器子對象。 |
返回值 | 說明 |
---|---|
params | mapping of string to any 參數名稱映射到其值。 |
partial_fit(X, y, classes=None, sample_weight=None)
[源碼]
對一批樣本進行增量擬合
方法需要對數據集的不同塊連續調用多次,以實現核外學習或在線學習。
當整個數據集太大而無法立即放入內存擬合時,這個功能特別有用。
這個方法具有一些性能消耗,因此最好對盡可能大的數據塊(只要適合內存預算)調用partial_fit以隱藏消耗。
參數 | 說明 |
---|---|
X | {array-like, sparse matrix} of shape (n_samples, n_features) 用于訓練的向量,其中n_samples是樣本數量,n_features是特征數量。 |
y | array-like of shape (n_samples,) 目標值。 |
classes | array-like of shape (n_classes), default=None y向量中可能出現的所有類別的列表。 必須在第一次調用partial_fit時提供,在隨后的調用中可以省略。 |
sample_weight | array-like of shape (n_samples,), default=None 應用于單個樣本的權重(1.未加權)。 |
返回值 | 說明 |
---|---|
self | object |
predict(X)
[源碼]
對測試向量X進行分類。
參數 | 說明 |
---|---|
X | array-like of shape (n_samples, n_features) |
返回值 | 說明 |
---|---|
C | ndarray of shape (n_samples,) X的預測目標值 |
predict_log_proba(X)
[源碼]
返回測試向量X的對數概率估計。
參數 | 說明 |
---|---|
X | array-like of shape (n_samples, n_features) |
返回值 | 說明 |
---|---|
C | array-like of shape (n_samples, n_classes) 返回模型中每個類別的樣本的對數概率。這些列按照排序順序對應于類別,就像它們出現在屬性classes_中一樣。 |
predict_proba(X)
[源碼]
參數 | 說明 |
---|---|
X | array-like of shape (n_samples, n_features) |
返回值 | 說明 |
---|---|
C | array-like of shape (n_samples, n_classes) 返回模型中每個類別的樣本概率。這些列按照排序順序對應于類別,就像它們出現在屬性classes_中一樣。 |
score(X, y, sample_weight=None)
[源碼]
返回給定測試數據和標簽上的平均準確度。
在多標簽分類中,這是子集準確性,這是一個嚴格的指標,因為您需要為每個樣本正確預測每個標簽集。
參數 | 說明 |
---|---|
X | array-like of shape (n_samples, n_features) 測試樣本 |
y | array-like of shape (n_samples,) or (n_samples, n_outputs) X的真實值標簽 |
sample_weight | array-like of shape (n_samples,), default=None 樣本權重 |
返回值 | 說明 |
---|---|
score | float self.predict(X)關于y的平均準確率 |
set_params(**params)
[源碼]
參數 | 說明 |
---|---|
**params | dict 估計器參數 |
返回值 | 說明 |
---|---|
self | object 估計器實例 |