本文整理汇总了Python中sklearn.mixture.GaussianMixture.predict_proba方法的典型用法代码示例。如果您正苦于以下问题:Python GaussianMixture.predict_proba方法的具体用法?Python GaussianMixture.predict_proba怎么用?Python GaussianMixture.predict_proba使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.mixture.GaussianMixture
的用法示例。
在下文中一共展示了GaussianMixture.predict_proba方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from sklearn.mixture import GaussianMixture [as 别名]
# 或者: from sklearn.mixture.GaussianMixture import predict_proba [as 别名]
def main():
Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST()
dae = DeepAutoEncoder([500, 300, 2])
dae.fit(Xtrain)
mapping = dae.map2center(Xtrain)
plt.scatter(mapping[:,0], mapping[:,1], c=Ytrain, s=100, alpha=0.5)
plt.show()
# purity measure from unsupervised machine learning pt 1
gmm = GaussianMixture(n_components=10)
gmm.fit(Xtrain)
responsibilities_full = gmm.predict_proba(Xtrain)
print "full purity:", purity(Ytrain, responsibilities_full)
gmm.fit(mapping)
responsibilities_reduced = gmm.predict_proba(mapping)
print "reduced purity:", purity(Ytrain, responsibilities_reduced)
示例2: main
# 需要导入模块: from sklearn.mixture import GaussianMixture [as 别名]
# 或者: from sklearn.mixture.GaussianMixture import predict_proba [as 别名]
def main():
Xtrain, Ytrain, Xtest, Ytest = getKaggleMNIST()
dae = DeepAutoEncoder([500, 300, 2])
dae.fit(Xtrain)
mapping = dae.map2center(Xtrain)
plt.scatter(mapping[:,0], mapping[:,1], c=Ytrain, s=100, alpha=0.5)
plt.show()
# purity measure from unsupervised machine learning pt 1
# NOTE: this will take a long time (i.e. just leave it overnight)
gmm = GaussianMixture(n_components=10)
gmm.fit(Xtrain)
print("Finished GMM training")
responsibilities_full = gmm.predict_proba(Xtrain)
print("full purity:", purity(Ytrain, responsibilities_full))
gmm.fit(mapping)
responsibilities_reduced = gmm.predict_proba(mapping)
print("reduced purity:", purity(Ytrain, responsibilities_reduced))
示例3: main
# 需要导入模块: from sklearn.mixture import GaussianMixture [as 别名]
# 或者: from sklearn.mixture.GaussianMixture import predict_proba [as 别名]
def main():
X, Y = get_data(10000)
print("Number of data points:", len(Y))
model = GaussianMixture(n_components=10)
model.fit(X)
M = model.means_
R = model.predict_proba(X)
print("Purity:", purity(Y, R)) # max is 1, higher is better
print("DBI:", DBI(X, M, R)) # lower is better
示例4: GaussianMixture1D
# 需要导入模块: from sklearn.mixture import GaussianMixture [as 别名]
# 或者: from sklearn.mixture.GaussianMixture import predict_proba [as 别名]
class GaussianMixture1D(object):
"""
Simple class to work with 1D mixtures of Gaussians
Parameters
----------
means : array_like
means of component distributions (default = 0)
sigmas : array_like
standard deviations of component distributions (default = 1)
weights : array_like
weight of component distributions (default = 1)
"""
def __init__(self, means=0, sigmas=1, weights=1):
data = np.array([t for t in np.broadcast(means, sigmas, weights)])
components = data.shape[0]
self._gmm = GaussianMixture(components, covariance_type='spherical')
self._gmm.means_ = data[:, :1]
self._gmm.weights_ = data[:, 2] / data[:, 2].sum()
self._gmm.covariances_ = data[:, 1] ** 2
self._gmm.precisions_cholesky_ = 1 / np.sqrt(self._gmm.covariances_)
self._gmm.fit = None # disable fit method for safety
def sample(self, size):
"""Random sample"""
return self._gmm.sample(size)
def pdf(self, x):
"""Compute probability distribution"""
if x.ndim == 1:
x = x[:, np.newaxis]
logprob = self._gmm.score_samples(x)
return np.exp(logprob)
def pdf_individual(self, x):
"""Compute probability distribution of each component"""
if x.ndim == 1:
x = x[:, np.newaxis]
logprob = self._gmm.score_samples(x)
responsibilities = self._gmm.predict_proba(x)
return responsibilities * np.exp(logprob[:, np.newaxis])
示例5: fit
# 需要导入模块: from sklearn.mixture import GaussianMixture [as 别名]
# 或者: from sklearn.mixture.GaussianMixture import predict_proba [as 别名]
def fit(self, X, Y=None):
if self.method == 'random':
N = len(X)
idx = np.random.randint(N, size=self.M)
self.samples = X[idx]
elif self.method == 'normal':
# just sample from N(0,1)
D = X.shape[1]
self.samples = np.random.randn(self.M, D) / np.sqrt(D)
elif self.method == 'kmeans':
X, Y = self._subsample_data(X, Y)
print("Fitting kmeans...")
t0 = datetime.now()
kmeans = KMeans(n_clusters=len(set(Y)))
kmeans.fit(X)
print("Finished fitting kmeans, duration:", datetime.now() - t0)
# calculate the most ambiguous points
# we will do this by finding the distance between each point
# and all cluster centers
# and return which points have the smallest variance
dists = kmeans.transform(X) # returns an N x K matrix
variances = dists.var(axis=1)
idx = np.argsort(variances) # smallest to largest
idx = idx[:self.M]
self.samples = X[idx]
elif self.method == 'gmm':
X, Y = self._subsample_data(X, Y)
print("Fitting GMM")
t0 = datetime.now()
gmm = GaussianMixture(
n_components=len(set(Y)),
covariance_type='spherical',
reg_covar=1e-6)
gmm.fit(X)
print("Finished fitting GMM, duration:", datetime.now() - t0)
# calculate the most ambiguous points
probs = gmm.predict_proba(X)
ent = stats.entropy(probs.T) # N-length vector of entropies
idx = np.argsort(-ent) # negate since we want biggest first
idx = idx[:self.M]
self.samples = X[idx]
return self
示例6: expand
# 需要导入模块: from sklearn.mixture import GaussianMixture [as 别名]
# 或者: from sklearn.mixture.GaussianMixture import predict_proba [as 别名]
x1_min, x1_max = expand(x1_min, x1_max)
x2_min, x2_max = expand(x2_min, x2_max)
x1, x2 = np.mgrid[x1_min:x1_max:500j, x2_min:x2_max:500j]
grid_test = np.stack((x1.flat, x2.flat), axis=1)
grid_hat = gmm.predict(grid_test)
grid_hat = grid_hat.reshape(x1.shape)
if change:
z = grid_hat == 0
grid_hat[z] = 1
grid_hat[~z] = 0
plt.figure(figsize=(7, 6), facecolor='w')
plt.pcolormesh(x1, x2, grid_hat, cmap=cm_light)
plt.scatter(x[:, 0], x[:, 1], s=50, c=y, marker='o', cmap=cm_dark, edgecolors='k')
plt.scatter(x_test[:, 0], x_test[:, 1], s=60, c=y_test, marker='^', cmap=cm_dark, edgecolors='k')
p = gmm.predict_proba(grid_test)
print(p)
p = p[:, 0].reshape(x1.shape)
CS = plt.contour(x1, x2, p, levels=(0.1, 0.5, 0.8), colors=list('rgb'), linewidths=2)
plt.clabel(CS, fontsize=12, fmt='%.1f', inline=True)
ax1_min, ax1_max, ax2_min, ax2_max = plt.axis()
xx = 0.95*ax1_min + 0.05*ax1_max
yy = 0.05*ax2_min + 0.95*ax2_max
plt.text(xx, yy, acc_str, fontsize=12)
yy = 0.1*ax2_min + 0.9*ax2_max
plt.text(xx, yy, acc_test_str, fontsize=12)
plt.xlim((x1_min, x1_max))
plt.ylim((x2_min, x2_max))
plt.xlabel('身高(cm)', fontsize=13)
plt.ylabel('体重(kg)', fontsize=13)
plt.title('EM算法估算GMM的参数', fontsize=15)
示例7: gaussian_overlap
# 需要导入模块: from sklearn.mixture import GaussianMixture [as 别名]
# 或者: from sklearn.mixture.GaussianMixture import predict_proba [as 别名]
def gaussian_overlap(w1, w2):
'''
estimate cluster overlap from a 2-mean Gaussian mixture model
Description
-------
Estimates the overlap between 2 spike clusters by fitting with two
multivariate Gaussians. Implementation makes use of scikit learn 'GMM'.
The percent of false positive and false negative errors are estimated for
both classes and stored as a confusion matrix. Error rates are calculated
by integrating the posterior probability of a misclassification. The
integral is then normalized by the number of events in the cluster of
interest. See description of confusion matrix below.
NOTE: The dimensionality of the data set is reduced to the top 99% of
principal components to increase the time efficiency of the fitting
algorithm.
Parameters
--------
w1 : array-like [Event x Sample ]
waveforms of 1st cluster
w2 : array-like [Event x Sample ]
waveforms of 2nd cluster
Returns
------
C
a confusion matrix
C[0,0] - False positive fraction in cluster 1 (waveforms of neuron 2 that were assigned to neuron 1)
C[0,1] - False negative fraction in cluster 1 (waveforms of neuron 1 that were assigned to neuron 2)
C[1,0] - False negative fraction in cluster 2
C[1,1] - False positive fraction in cluster 2
'''
# reduce dimensionality to 98% of top Principal Components
N1 = w1.shape[0]
N2 = w2.shape[0]
X = np.concatenate((w1, w2))
pca = PCA()
pca.fit(X)
Xn = pca.transform(X)
cutoff = 0.98
num_dims = (np.cumsum(pca.explained_variance_ratio_) < cutoff).sum()
w1 = Xn[:N1, :num_dims]
w2 = Xn[N1:, :num_dims]
# fit 2 multivariate gaussians
gmm = GMM(n_components=2)
gmm.fit(np.vstack((w1, w2)))
# get posteriors
pr1 = gmm.predict_proba(w1)
pr2 = gmm.predict_proba(w2)
# in the unlikely case that the cluster identities were flipped during the
# fitting procedure, flip them back
if pr1[:, 0].mean() + pr2[:, 1].mean() < 1:
pr1 = pr1[:, [1, 0]]
pr2 = pr2[:, [1, 0]]
# create confusion matrix
confusion = np.zeros((2, 2))
confusion[0, 0] = pr1[:, 1].mean() # probability that a member of 1 is false
# relative proportion of spikes that were placed in cluster 2 by mistake
confusion[0, 1] = pr2[:, 0].sum() / N1
confusion[1, 1] = pr2[:, 0].mean() # probability that a member of 2 was really from 1
# relative proportion of spikes that were placed in cluster 1 by mistake
confusion[1, 0] = pr1[:, 1].sum() / N2
return confusion