# 4.1 API : MultinomialNB、GaussianNB、BernoulliNB

1147-柳同学

## 热门标签

, , ,

### 1.MultinomialNB

from sklearn.naive_bayes import MultinomialNB

MultinomialNB(*, alpha=1.0, fit_prior=True, class_prior=None)


Parameters

alpha : float, default=1.0

fit_prior : bool, default=True

class_prior : array-like of shape (n_classes,), default=None

Attributes

class_count_ : ndarray of shape (n_classes,)

class_log_prior_ ：ndarray of shape (n_classes, )

classes_ : ndarray of shape (n_classes,)

feature_count_ ： ndarray of shape (n_classes, n_features)

feature_log_prob_ : ndarray of shape (n_classes, n_features)

n_features_ : int

Methods

fit(X, y[, sample_weight])

get_params([deep])

partial_fit(X, y[, classes, sample_weight])

predict(X)

predict_log_proba(X)

predict_proba(X)

score(X, y[, sample_weight])

set_params(**params)

#### 实例

>>> 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 MultinomialNB
>>> clf = MultinomialNB()
>>> clf.fit(X, y)
MultinomialNB()
>>> print(clf.predict(X[2:3]))
[3]


### 2.GaussianNB

from sklearn.naive_bayes import GaussianNB

GaussianNB(*, priors=None, var_smoothing=1e-09)


Parameters

priors : array-like of shape (n_classes,)

var_smoothing : float, default=1e-9

Attributes

class_count_ : ndarray of shape (n_classes,)

class_prior_ : ndarray of shape (n_classes,)

classes_ : ndarray of shape (n_classes,)

epsilon_ : float

sigma_ : ndarray of shape (n_classes, n_features)

theta_ : ndarray of shape (n_classes, n_features)

Methods

#### 实例

>>> import numpy as np
>>> X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
>>> Y = np.array([1, 1, 1, 2, 2, 2])
>>> from sklearn.naive_bayes import GaussianNB
>>> clf = GaussianNB()
>>> clf.fit(X, Y)
GaussianNB()
>>> print(clf.predict([[-0.8, -1]]))
[1]
>>> clf_pf = GaussianNB()
>>> clf_pf.partial_fit(X, Y, np.unique(Y))
GaussianNB()
>>> print(clf_pf.predict([[-0.8, -1]]))
[1]


### 3.BernoulliNB

from sklearn.naive_bayes import BernoulliNB

BernoulliNB(*, alpha=1.0, binarize=0.0, fit_prior=True, class_prior=None)


Parameters

alpha : float, default=1.0

binarize : float or None, default=0.0

fit_prior : bool, default=True

class_prior : array-like of shape (n_classes,), default=None

Attributes

coef_ : ndarray of shape (n_classes, n_features)

intercept_ : ndarray of shape (n_classes,)

Methods

#### 实例

>>> import numpy as np
>>> rng = np.random.RandomState(1)
>>> X = rng.randint(5, size=(6, 100))
>>> Y = np.array([1, 2, 3, 4, 4, 5])
>>> from sklearn.naive_bayes import BernoulliNB
>>> clf = BernoulliNB()
>>> clf.fit(X, Y)
BernoulliNB()
>>> print(clf.predict(X[2:3]))
[3]


Vieu3.3主题

Q Q 登 录