/usr/lib/python3/dist-packages/astroML/classification/gmm_bayes.py is in python3-astroml 0.3-6.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | """
GMM Bayes
---------
This implements generative classification based on mixtures of gaussians
to model the probability density of each class.
"""
import numpy as np
from sklearn.mixture import GMM
from sklearn.naive_bayes import BaseNB
class GMMBayes(BaseNB):
"""GMM Bayes Classifier
This is a generalization to the Naive Bayes classifier: rather than
modeling the distribution of each class with axis-aligned gaussians,
GMMBayes models the distribution of each class with mixtures of
gaussians. This can lead to better classification in some cases.
Parameters
----------
n_components : int or list
number of components to use in the gmm. If specified as a list, it
must match the number of class labels
other keywords are passed directly to GMM
"""
def __init__(self, n_components=1, **kwargs):
self.n_components = np.atleast_1d(n_components)
self.kwargs = kwargs
def fit(self, X, y):
X = np.asarray(X)
y = np.asarray(y)
n_samples, n_features = X.shape
if n_samples != y.shape[0]:
raise ValueError("X and y have incompatible shapes")
self.classes_ = np.unique(y)
self.classes_.sort()
unique_y = self.classes_
n_classes = unique_y.shape[0]
if self.n_components.size not in (1, len(unique_y)):
raise ValueError("n_components must be compatible with "
"the number of classes")
self.gmms_ = [None for i in range(n_classes)]
self.class_prior_ = np.zeros(n_classes)
n_comp = np.zeros(len(self.classes_), dtype=int) + self.n_components
for i, y_i in enumerate(unique_y):
self.gmms_[i] = GMM(n_comp[i], **self.kwargs).fit(X[y == y_i])
self.class_prior_[i] = np.float(np.sum(y == y_i)) / n_samples
return self
def _joint_log_likelihood(self, X):
X = np.asarray(np.atleast_2d(X))
logprobs = np.array([g.score(X) for g in self.gmms_]).T
return logprobs + np.log(self.class_prior_)
|