def __init__(self):
self.ni = []
self.prop = []
self.mean = []
self.cov =[]
self.Q = []
self.L = []
self.classnum = [] # to keep right labels
self.tau = 0.0
python类cov()的实例源码
def learn(self,x,y):
'''
Function that learns the GMM with ridge regularizationb from training samples
Input:
x : the training samples
y : the labels
Output:
the mean, covariance and proportion of each class, as well as the spectral decomposition of the covariance matrix
'''
## Get information from the data
C = sp.unique(y).shape[0]
#C = int(y.max(0)) # Number of classes
n = x.shape[0] # Number of samples
d = x.shape[1] # Number of variables
eps = sp.finfo(sp.float64).eps
## Initialization
self.ni = sp.empty((C,1)) # Vector of number of samples for each class
self.prop = sp.empty((C,1)) # Vector of proportion
self.mean = sp.empty((C,d)) # Vector of means
self.cov = sp.empty((C,d,d)) # Matrix of covariance
self.Q = sp.empty((C,d,d)) # Matrix of eigenvectors
self.L = sp.empty((C,d)) # Vector of eigenvalues
self.classnum = sp.empty(C).astype('uint8')
## Learn the parameter of the model for each class
for c,cR in enumerate(sp.unique(y)):
j = sp.where(y==(cR))[0]
self.classnum[c] = cR # Save the right label
self.ni[c] = float(j.size)
self.prop[c] = self.ni[c]/n
self.mean[c,:] = sp.mean(x[j,:],axis=0)
self.cov[c,:,:] = sp.cov(x[j,:],bias=1,rowvar=0) # Normalize by ni to be consistent with the update formulae
# Spectral decomposition
L,Q = linalg.eigh(self.cov[c,:,:])
idx = L.argsort()[::-1]
self.L[c,:] = L[idx]
self.Q[c,:,:]=Q[:,idx]
def cluster(self, X):
self.fit(X)
cluster = [X[sp.argmax(self.responsibility, axis=1) == k] for k in range(self.n_classes)]
mean = self.center
cov = [sp.cov(c, rowvar=0, ddof=0) for c in cluster]
return cluster, mean, cov
def _init_params(self, X):
init = self.init
n_samples, n_features = X.shape
n_components = self.n_components
if (init == 'kmeans'):
km = Kmeans(n_components)
clusters, mean, cov = km.cluster(X)
coef = sp.array([c.shape[0] / n_samples for c in clusters])
comps = [multivariate_normal(mean[i], cov[i], allow_singular=True)
for i in range(n_components)]
elif (init == 'rand'):
coef = sp.absolute(sprand.randn(n_components))
coef = coef / coef.sum()
means = X[sprand.permutation(n_samples)[0: n_components]]
clusters = [[] for i in range(n_components)]
for x in X:
idx = sp.argmin([spla.norm(x - mean) for mean in means])
clusters[idx].append(x)
comps = []
for k in range(n_components):
mean = means[k]
cov = sp.cov(clusters[k], rowvar=0, ddof=0)
comps.append(multivariate_normal(mean, cov, allow_singular=True))
self.coef = coef
self.comps = comps
def fit(self, X):
cov = sp.cov(X, rowvar=0)
eigvals, eigvecs = self._eig_decomposition(cov)
self.eigvals = eigvals
self.eigvecs = eigvecs
self.mean = X.mean(axis=0)
return sp.dot(X, eigvecs)
def _maximum_likelihood(self, X):
n_samples, n_features = X.shape if X.ndim > 1 else (1, X.shape[0])
n_components = self.n_components
# Predict mean
mu = X.mean(axis=0)
# Predict covariance
cov = sp.cov(X, rowvar=0)
eigvals, eigvecs = self._eig_decomposition(cov)
sigma2 = ((sp.sum(cov.diagonal()) - sp.sum(eigvals.sum())) /
(n_features - n_components)) # FIXME: M < D?
weight = sp.dot(eigvecs, sp.diag(sp.sqrt(eigvals - sigma2)))
M = sp.dot(weight.T, weight) + sigma2 * sp.eye(n_components)
inv_M = spla.inv(M)
self.eigvals = eigvals
self.eigvecs = eigvecs
self.predict_mean = mu
self.predict_cov = sp.dot(weight, weight.T) + sigma2 * sp.eye(n_features)
self.latent_mean = sp.transpose(sp.dot(inv_M, sp.dot(weight.T, X.T - mu[:, sp.newaxis])))
self.latent_cov = sigma2 * inv_M
self.sigma2 = sigma2 # FIXME!
self.weight = weight
self.inv_M = inv_M
return self.latent_mean
def __init__(self):
self.ni = []
self.prop = []
self.mean = []
self.cov =[]
self.Q = []
self.L = []
self.classnum = [] # to keep right labels
self.tau = 0.0
def fit(self, X):
# Constants
max_iter = self.max_iter
tol = self.tol
n_samples, n_features = X.shape
n_components = self.n_components
# Initialize parameters
self._init_params(X)
# Initialize
responsibility = sp.empty((n_samples, n_components))
log_likelihood = self.log_likelihood(X)
for iter in range(max_iter):
# E step
for n in range(n_samples):
for k in range(n_components):
responsibility[n][k] = self.pdf(X[n], k) / self.pdf(X[n])
# M step
eff = sp.sum(responsibility, axis=0)
for k in range(n_components):
# Update mean
mean = sp.dot(responsibility[:, k], X) / eff[k]
# Update covariance
cov = sp.zeros((n_features, n_features))
for n in range(n_samples):
cov += responsibility[n][k] * sp.outer(X[n] - mean, X[n] - mean)
cov /= eff[k]
# Update the k component
self.comps[k] = multivariate_normal(mean, cov, allow_singular=True)
# Update mixture coefficient
self.coef[k] = eff[k] / n_samples
# Convergent test
log_likelihood_new = self.log_likelihood(X)
diff = log_likelihood_new - log_likelihood
print('GMM: {0:5d}: {1:10.5e} {2:10.5e}'.format(
iter, log_likelihood_new, spla.norm(diff) / spla.norm(log_likelihood)))
if (spla.norm(diff) < tol * spla.norm(log_likelihood)):
break
log_likelihood = log_likelihood_new
return self
def learn(self,x,y):
'''
Function that learns the GMM with ridge regularizationb from training samples
Input:
x : the training samples
y : the labels
Output:
the mean, covariance and proportion of each class, as well as the spectral decomposition of the covariance matrix
'''
## Get information from the data
C = sp.unique(y).shape[0]
#C = int(y.max(0)) # Number of classes
n = x.shape[0] # Number of samples
d = x.shape[1] # Number of variables
eps = sp.finfo(sp.float64).eps
## Initialization
self.ni = sp.empty((C,1)) # Vector of number of samples for each class
self.prop = sp.empty((C,1)) # Vector of proportion
self.mean = sp.empty((C,d)) # Vector of means
self.cov = sp.empty((C,d,d)) # Matrix of covariance
self.Q = sp.empty((C,d,d)) # Matrix of eigenvectors
self.L = sp.empty((C,d)) # Vector of eigenvalues
self.classnum = sp.empty(C).astype('uint8')
## Learn the parameter of the model for each class
for c,cR in enumerate(sp.unique(y)):
j = sp.where(y==(cR))[0]
self.classnum[c] = cR # Save the right label
self.ni[c] = float(j.size)
self.prop[c] = self.ni[c]/n
self.mean[c,:] = sp.mean(x[j,:],axis=0)
self.cov[c,:,:] = sp.cov(x[j,:],bias=1,rowvar=0) # Normalize by ni to be consistent with the update formulae
# Spectral decomposition
L,Q = linalg.eigh(self.cov[c,:,:])
idx = L.argsort()[::-1]
self.L[c,:] = L[idx]
self.Q[c,:,:]=Q[:,idx]