本文整理汇总了Python中sklearn.mixture.GaussianMixture方法的典型用法代码示例。如果您正苦于以下问题:Python mixture.GaussianMixture方法的具体用法?Python mixture.GaussianMixture怎么用?Python mixture.GaussianMixture使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.mixture
的用法示例。
在下文中一共展示了mixture.GaussianMixture方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: detection_with_gaussian_mixture
# 需要导入模块: from sklearn import mixture [as 别名]
# 或者: from sklearn.mixture import GaussianMixture [as 别名]
def detection_with_gaussian_mixture(image_set):
"""
:param image_set: The bottleneck values of the relevant images.
:return: Predictions vector
"""
# Might achieve, better results by initializing weights, or means, given we know when we introduce noisy labels
clf = mixture.GaussianMixture(n_components=2)
clf.fit(image_set)
predictions = clf.predict(image_set)
predictions = normalize_predictions(predictions)
return predictions
示例2: _evaluate_gmm_likelihood
# 需要导入模块: from sklearn import mixture [as 别名]
# 或者: from sklearn.mixture import GaussianMixture [as 别名]
def _evaluate_gmm_likelihood(train, test, metadata, components=[10, 30]):
results = list()
for n_components in components:
gmm = GaussianMixture(n_components, covariance_type='diag')
LOGGER.info('Evaluating using %s', gmm)
gmm.fit(test)
l1 = gmm.score(train)
gmm.fit(train)
l2 = gmm.score(test)
results.append({
"name": repr(gmm),
"syn_likelihood": l1,
"test_likelihood": l2,
})
return pd.DataFrame(results)
示例3: gen_exp_name
# 需要导入模块: from sklearn import mixture [as 别名]
# 或者: from sklearn.mixture import GaussianMixture [as 别名]
def gen_exp_name(model_class, model_kwargs):
"""Generates experiment name from model class and parameters.
:param model_class: (type) the class, one of GaussianMixture, PCAPreDensity or KernelDensity.
:param model_kwargs: (dict) constructor arguments to the class.
:return A string succinctly encoding the class and parameters."""
if model_class == GaussianMixture:
n_components = model_kwargs.get("n_components", 1)
covariance_type = model_kwargs.get("covariance_type", "full")
return f"gmm_{n_components}_components_{covariance_type}"
elif model_class == PCAPreDensity:
if model_kwargs["density_class"] == KernelDensity:
return "pca_kde"
elif model_kwargs["density_class"] == GaussianMixture:
return "pca_gmm"
else:
return "pca_unknown"
elif model_class == KernelDensity:
return "kde"
else:
return "default"
示例4: clustering_scores
# 需要导入模块: from sklearn import mixture [as 别名]
# 或者: from sklearn.mixture import GaussianMixture [as 别名]
def clustering_scores(self, prediction_algorithm: str = "knn") -> Tuple:
if self.gene_dataset.n_labels > 1:
latent, _, labels = self.get_latent()
if prediction_algorithm == "knn":
labels_pred = KMeans(
self.gene_dataset.n_labels, n_init=200
).fit_predict(
latent
) # n_jobs>1 ?
elif prediction_algorithm == "gmm":
gmm = GMM(self.gene_dataset.n_labels)
gmm.fit(latent)
labels_pred = gmm.predict(latent)
asw_score = silhouette_score(latent, labels)
nmi_score = NMI(labels, labels_pred)
ari_score = ARI(labels, labels_pred)
uca_score = unsupervised_clustering_accuracy(labels, labels_pred)[0]
logger.debug(
"Clustering Scores:\nSilhouette: %.4f\nNMI: %.4f\nARI: %.4f\nUCA: %.4f"
% (asw_score, nmi_score, ari_score, uca_score)
)
return asw_score, nmi_score, ari_score, uca_score
示例5: differential_entropies
# 需要导入模块: from sklearn import mixture [as 别名]
# 或者: from sklearn.mixture import GaussianMixture [as 别名]
def differential_entropies(X, labels):
n_samples, n_features = X.shape
labels = np.array(labels)
names = sorted(set(labels))
entropies = []
for name in names:
name_idx = np.where(labels == name)[0]
gm = GaussianMixture().fit(X[name_idx, :])
mn = multivariate_normal(
mean=gm.means_.flatten(),
cov=gm.covariances_.reshape(n_features, n_features)
)
entropies.append(mn.entropy())
probs = softmax(entropies)
for name, entropy, prob in zip(names, entropies, probs):
#print('{}\t{}\t{}'.format(name, entropy, prob))
print('{}\t{}'.format(name, entropy))
示例6: determineComponents
# 需要导入模块: from sklearn import mixture [as 别名]
# 或者: from sklearn.mixture import GaussianMixture [as 别名]
def determineComponents(data):
X,Y = preparingData(data)
n_components = np.arange(1,10)
bic = np.zeros(n_components.shape)
for i,n in enumerate(n_components):
#fit gmm to data for each value of components
gmm = GaussianMixture(n_components=n,max_iter=200, covariance_type='diag' ,n_init=3)
gmm.fit(X)
#store BIC scores
bic[i] = gmm.bic(X)
#Therefore, Bayesian Information Criteria (BIC) is introduced as a cost function composing of 2 terms;
#1) minus of log-likelihood and 2) model complexity. Please see my old post. You will see that BIC prefers model
#that gives good result while the complexity remains small. In other words, the model whose BIC is smallest is the winner
#plot the results
plt.plot(bic)
plt.show()
示例7: __init__
# 需要导入模块: from sklearn import mixture [as 别名]
# 或者: from sklearn.mixture import GaussianMixture [as 别名]
def __init__(self, data, max_mixture=10, threshold=0.1):
"""
Class constructor, arguments include:
data - data to build GMM model
max_mixture - max number of Gaussian mixtures
threshold - probability threhold to determine fense
"""
self.data = data
self.thresh = threshold
lowest_bic = np.infty
components = 1
bic = []
n_components_range = range(1, max_mixture + 1)
for n_components in n_components_range:
# Fit a Gaussian mixture with EM
gmm = mixture.GaussianMixture(n_components=n_components,
random_state=1005)
gmm.fit(data)
bic.append(gmm.bic(data))
if bic[-1] < lowest_bic:
lowest_bic = bic[-1]
best_gmm = gmm
components = n_components
log.debug('best gmm components number: %d, bic %f ', components, lowest_bic)
self.gmm = best_gmm
示例8: _fit
# 需要导入模块: from sklearn import mixture [as 别名]
# 或者: from sklearn.mixture import GaussianMixture [as 别名]
def _fit(self, X):
self.estimator_ = GaussianMixture(
covariance_type = self.covariance_type,
init_params = self.init_params,
max_iter = self.max_iter,
means_init = self.means_init,
n_components = self.n_components,
n_init = self.n_init,
precisions_init = self.precisions_init,
random_state = self.random_state,
reg_covar = self.reg_covar,
tol = self.tol,
warm_start = self.warm_start,
weights_init = self.weights_init
).fit(X)
return self
示例9: eval_train
# 需要导入模块: from sklearn import mixture [as 别名]
# 或者: from sklearn.mixture import GaussianMixture [as 别名]
def eval_train(eval_loader,model,device,whichnet,queue):
CE = nn.CrossEntropyLoss(reduction='none')
model.eval()
num_iter = (len(eval_loader.dataset)//eval_loader.batch_size)+1
losses = torch.zeros(len(eval_loader.dataset))
with torch.no_grad():
for batch_idx, (inputs, targets, index) in enumerate(eval_loader):
inputs, targets = inputs.to(device), targets.to(device,non_blocking=True)
outputs = model(inputs)
loss = CE(outputs, targets)
for b in range(inputs.size(0)):
losses[index[b]]=loss[b]
sys.stdout.write('\n')
sys.stdout.write('|%s Evaluating loss Iter[%3d/%3d]\t' %(whichnet,batch_idx,num_iter))
sys.stdout.flush()
losses = (losses-losses.min())/(losses.max()-losses.min())
# fit a two-component GMM to the loss
input_loss = losses.reshape(-1,1)
gmm = GaussianMixture(n_components=2,max_iter=10,tol=1e-2,reg_covar=1e-3)
gmm.fit(input_loss)
prob = gmm.predict_proba(input_loss)
prob = prob[:,gmm.means_.argmin()]
queue.put(prob)
示例10: eval_train
# 需要导入模块: from sklearn import mixture [as 别名]
# 或者: from sklearn.mixture import GaussianMixture [as 别名]
def eval_train(model,all_loss):
model.eval()
num_iter = (len(eval_loader.dataset)//eval_loader.batch_size)+1
losses = torch.zeros(len(eval_loader.dataset))
with torch.no_grad():
for batch_idx, (inputs, targets, index) in enumerate(eval_loader):
inputs, targets = inputs.cuda(), targets.cuda()
outputs = model(inputs)
loss = CE(outputs, targets)
for b in range(inputs.size(0)):
losses[index[b]]=loss[b]
sys.stdout.write('\r')
sys.stdout.write('| Evaluating loss Iter[%3d/%3d]\t' %(batch_idx,num_iter))
sys.stdout.flush()
losses = (losses-losses.min())/(losses.max()-losses.min())
all_loss.append(losses)
# fit a two-component GMM to the loss
input_loss = losses.reshape(-1,1)
gmm = GaussianMixture(n_components=2,max_iter=10,tol=1e-2,reg_covar=5e-4)
gmm.fit(input_loss)
prob = gmm.predict_proba(input_loss)
prob = prob[:,gmm.means_.argmin()]
return prob,all_loss
示例11: clusterize
# 需要导入模块: from sklearn import mixture [as 别名]
# 或者: from sklearn.mixture import GaussianMixture [as 别名]
def clusterize(points, n_components=2, covariance_type='tied',
centers=None, weights=None, output=None, random_state=1000):
if centers is not None:
n_components = len(centers)
if output is None:
output = points
if len(points) < 2:
return [list(output)]
gmm = GaussianMixture(n_components=n_components,
covariance_type=covariance_type,
means_init=centers,
weights_init=weights,
random_state=random_state)
gmm.fit(points)
labels = gmm.predict(points)
clusters = defaultdict(list)
for label, point in zip(labels, output):
clusters[label].append(point)
return sorted(clusters.values(), key=lambda x: len(x), reverse=True)
示例12: initialize_gmm
# 需要导入模块: from sklearn import mixture [as 别名]
# 或者: from sklearn.mixture import GaussianMixture [as 别名]
def initialize_gmm(self, dataloader):
use_cuda = torch.cuda.is_available()
if use_cuda:
self.cuda()
self.eval()
data = []
for batch_idx, (inputs, _) in enumerate(dataloader):
inputs = inputs.view(inputs.size(0), -1).float()
if use_cuda:
inputs = inputs.cuda()
inputs = Variable(inputs)
z, outputs, mu, logvar = self.forward(inputs)
data.append(z.data.cpu().numpy())
data = np.concatenate(data)
gmm = GaussianMixture(n_components=self.n_centroids,covariance_type='diag')
gmm.fit(data)
self.u_p.data.copy_(torch.from_numpy(gmm.means_.T.astype(np.float32)))
self.lambda_p.data.copy_(torch.from_numpy(gmm.covariances_.T.astype(np.float32)))
示例13: cluster_GMM
# 需要导入模块: from sklearn import mixture [as 别名]
# 或者: from sklearn.mixture import GaussianMixture [as 别名]
def cluster_GMM(num_clusters, word_vectors):
# Initalize a GMM object and use it for clustering.
clf = GaussianMixture(n_components=num_clusters,
covariance_type="tied", init_params='kmeans', max_iter=50)
# Get cluster assignments.
clf.fit(word_vectors)
idx = clf.predict(word_vectors)
print("Clustering Done...", time.time() - start, "seconds")
# Get probabilities of cluster assignments.
idx_proba = clf.predict_proba(word_vectors)
# Dump cluster assignments and probability of cluster assignments.
joblib.dump(idx, 'gmm_latestclusmodel_len2alldata.pkl')
print("Cluster Assignments Saved...")
joblib.dump(idx_proba, 'gmm_prob_latestclusmodel_len2alldata.pkl')
print("Probabilities of Cluster Assignments Saved...")
return (idx, idx_proba)
示例14: cluster_GMM
# 需要导入模块: from sklearn import mixture [as 别名]
# 或者: from sklearn.mixture import GaussianMixture [as 别名]
def cluster_GMM(num_clusters, word_vectors):
# Initalize a GMM object and use it for clustering.
clf = GaussianMixture(n_components=num_clusters,
covariance_type="tied", init_params='kmeans', max_iter=50)
# Get cluster assignments.
clf.fit(word_vectors)
idx = clf.predict(word_vectors)
print("Clustering Done...", time.time() - start, "seconds")
# Get probabilities of cluster assignments.
idx_proba = clf.predict_proba(word_vectors)
# Dump cluster assignments and probability of cluster assignments.
joblib.dump(idx, 'gmm_latestclusmodel_len2alldata.pkl')
print("Cluster Assignments Saved...")
joblib.dump(idx_proba, 'gmm_prob_latestclusmodel_len2alldata.pkl')
print("Probabilities of Cluster Assignments Saved...")
return (idx, idx_proba)
示例15: gmm
# 需要导入模块: from sklearn import mixture [as 别名]
# 或者: from sklearn.mixture import GaussianMixture [as 别名]
def gmm(n_clusters, samples):
"""
Run GMM clustering on vertex coordinates.
Parameters:
- - - - -
n_clusters : int
number of clusters to generate
samples : array
Euclidean-space coordinates of vertices
"""
# Fit Gaussian Mixture Model
gmm = mixture.GaussianMixture(
n_components=n_clusters, covariance_type='tied', max_iter=1000,
init_params='kmeans', verbose=0)
gmm.fit(samples)
labels = gmm.predict(samples)
labels = labels.astype(np.int32)+1
return labels