24
24
from .preprocessing import binarize , LabelBinarizer
25
25
from .utils import array2d , atleast2d_or_csr
26
26
from .utils .extmath import safe_sparse_dot , logsumexp
27
+ from .utils import deprecated
27
28
28
29
29
30
class BaseNB (BaseEstimator , ClassifierMixin ):
@@ -35,7 +36,7 @@ class BaseNB(BaseEstimator, ClassifierMixin):
35
36
def _joint_log_likelihood (self , X ):
36
37
"""Compute the unnormalized posterior log probability of X
37
38
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
39
40
shape [n_classes, n_samples].
40
41
41
42
Input is passed to _joint_log_likelihood as-is by predict,
@@ -109,13 +110,13 @@ class GaussianNB(BaseNB):
109
110
110
111
Attributes
111
112
----------
112
- class_prior : array, shape = [n_classes]
113
+ `class_prior_` : array, shape = [n_classes]
113
114
probability of each class.
114
115
115
- theta : array, shape = [n_classes, n_features]
116
+ `theta_` : array, shape = [n_classes, n_features]
116
117
mean of each feature per class
117
118
118
10000
td>
- sigma : array, shape = [n_classes, n_features]
119
+ `sigma_` : array, shape = [n_classes, n_features]
119
120
variance of each feature per class
120
121
121
122
Examples
@@ -156,27 +157,48 @@ def fit(self, X, y):
156
157
n_classes = unique_y .shape [0 ]
157
158
_ , n_features = X .shape
158
159
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 )
162
163
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
166
167
return self
167
168
168
169
def _joint_log_likelihood (self , X ):
169
170
X = array2d (X )
170
171
joint_log_likelihood = []
171
172
for i in xrange (np .size (self ._classes )):
172
- jointi = np .log (self .class_prior [i ])
173
+ jointi = np .log (self .class_prior_ [i ])
173
174
n_ij = - 0.5 * np .sum (np .log (np .pi * self .sigma [i , :]))
174
175
n_ij -= 0.5 * np .sum (((X - self .theta [i , :]) ** 2 ) / \
175
176
(self .sigma [i , :]), 1 )
176
177
joint_log_likelihood .append (jointi + n_ij )
177
178
joint_log_likelihood = np .array (joint_log_likelihood ).T
178
179
return joint_log_likelihood
179
180
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
+
180
202
181
203
class BaseDiscreteNB (BaseNB ):
182
204
"""Abstract base class for naive Bayes on discrete/categorical data
0 commit comments