8000 Merge pull request #537 from amueller/gaussian_nb_underscore · seckcoder/scikit-learn@d869bf3 · GitHub
[go: up one dir, main page]

Skip to content

Commit d869bf3

Browse files
committed
Merge pull request scikit-learn#537 from amueller/gaussian_nb_underscore
MRG ENH in GaussianNB, let estimated parameters have underscores.
2 parents a24486b + 2882b97 commit d869bf3

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

sklearn/naive_bayes.py

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from .preprocessing import binarize, LabelBinarizer
2525
from .utils import array2d, atleast2d_or_csr
2626
from .utils.extmath import safe_sparse_dot, logsumexp
27+
from .utils import deprecated
2728

2829

2930
class BaseNB(BaseEstimator, ClassifierMixin):
@@ -35,7 +36,7 @@ class BaseNB(BaseEstimator, ClassifierMixin):
3536
def _joint_log_likelihood(self, X):
3637
"""Compute the unnormalized posterior log probability of X
3738
38-
I.e. log P(c) + log P(x|c) for all rows x of X, as an array-like of
39+
I.e. ``log P(c) + log P(x|c)`` for all rows x of X, as an array-like of
3940
shape [n_classes, n_samples].
4041
4142
Input is passed to _joint_log_likelihood as-is by predict,
@@ -109,13 +110,13 @@ class GaussianNB(BaseNB):
109110
110111
Attributes
111112
----------
112-
class_prior : array, shape = [n_classes]
113+
`class_prior_` : array, shape = [n_classes]
113114
probability of each class.
114115
115-
theta : array, shape = [n_classes, n_features]
116+
`theta_` : array, shape = [n_classes, n_features]
116117
mean of each feature per class
117118
118-
sigma : array, shape = [n_classes, n_features]
119+
`sigma_` : array, shape = [n_classes, n_features]
119120
variance of each feature per class
120121
121122
Examples
@@ -156,27 +157,48 @@ def fit(self, X, y):
156157
n_classes = unique_y.shape[0]
157158
_, n_features = X.shape
158159

159-
self.theta = np.empty((n_classes, n_features))
160-
self.sigma = np.empty((n_classes, n_features))
161-
self.class_prior = np.empty(n_classes)
160+
self.theta_ = np.empty((n_classes, n_features))
161+
self.sigma_ = np.empty((n_classes, n_features))
162+
self.class_prior_ = np.empty(n_classes)
162163
for i, y_i in enumerate(unique_y):
163-
self.theta[i, :] = np.mean(X[y == y_i, :], axis=0)
164-
self.sigma[i, :] = np.var(X[y == y_i, :], axis=0)
165-
self.class_prior[i] = np.float(np.sum(y == y_i)) / n_classes
164+
self.theta_[i, :] = np.mean(X[y == y_i, :], axis=0)
165+
self.sigma_[i, :] = np.var(X[y == y_i, :], axis=0)
166+
self.class_prior_[i] = np.float(np.sum(y == y_i)) / n_classes
166167
return self
167168

168169
def _joint_log_likelihood(self, X):
169170
X = array2d(X)
170171
joint_log_likelihood = []
171172
for i in xrange(np.size(self._classes)):
172-
jointi = np.log(self.class_prior[i])
173+
jointi = np.log(self.class_prior_[i])
173174
n_ij = - 0.5 * np.sum(np.log(np.pi * self.sigma[i, :]))
174175
n_ij -= 0.5 * np.sum(((X - self.theta[i, :]) ** 2) / \
175176
(self.sigma[i, :]), 1)
176177
joint_log_likelihood.append(jointi + n_ij)
177178
joint_log_likelihood = np.array(joint_log_likelihood).T
178179
return joint_log_likelihood
179180

181+
@property
182+
@deprecated('GaussianNB.class_prior is deprecated'
183+
' and will be removed in version 0.12.'
184+
' Please use GaussianNB.class_prior_ instead.')
185+
def class_prior(self):
186+
return self.class_prior_
187+
188+
@property
189+
@deprecated('GaussianNB.theta is deprecated'
190+
' and will be removed in version 0.12.'
191+
' Please use GaussianNB.theta_ instead.')
192+
def theta(self):
193+
return self.theta_
194+
195+
@property
196+
@deprecated('GaussianNB.sigma is deprecated'
197+
' and will be removed in version 0.12.'
198+
' Please use GaussianNB.sigma_ instead.')
199+
def sigma(self):
200+
return self.sigma_
201+
180202

181203
class BaseDiscreteNB(BaseNB):
182204
"""Abstract base class for naive Bayes on discrete/categorical data

0 commit comments

Comments
 (0)
0