sklearn.naive_bayes.ComplementNB

class sklearn.naive_bayes.ComplementNB(*,alpha = 1.0,fit_prior = Trueclass_prior = Nonenorm = 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=(6100))
>>> y = np.array([123456])
>>> 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
估计器实例

sklearn.naive_bayes.ComplementNB使用示例