本文整理汇总了Python中sklearn.mixture.GaussianMixture.predict方法的典型用法代码示例。如果您正苦于以下问题:Python GaussianMixture.predict方法的具体用法?Python GaussianMixture.predict怎么用?Python GaussianMixture.predict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.mixture.GaussianMixture
的用法示例。
在下文中一共展示了GaussianMixture.predict方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: learn_subset
# 需要导入模块: from sklearn.mixture import GaussianMixture [as 别名]
# 或者: from sklearn.mixture.GaussianMixture import predict [as 别名]
def learn_subset(self, search_space):
#Mask undesired features
current_array = self.vectors[:,search_space]
GM = GaussianMixture(n_components = 2,
covariance_type = "full",
tol = 0.001,
reg_covar = 1e-06,
max_iter = 1000,
n_init = 25,
init_params = "kmeans",
weights_init = None,
means_init = None,
precisions_init = None,
random_state = None,
warm_start = False,
verbose = 0,
verbose_interval = 10
)
GM.fit(current_array)
labels = GM.predict(current_array)
unique, counts = np.unique(labels, return_counts = True)
count_dict = dict(zip(unique, counts))
return count_dict, labels
示例2: gmm
# 需要导入模块: from sklearn.mixture import GaussianMixture [as 别名]
# 或者: from sklearn.mixture.GaussianMixture import predict [as 别名]
def gmm(nclusters, coords, n_init=50, n_iter=500):
if USE_GAUSSIAN_MIXTURE:
est = GaussianMixture(n_components=nclusters, n_init=n_init, max_iter=n_iter)
else:
est = GMM(n_components=nclusters, n_init=n_init, n_iter=n_iter)
est.fit(coords)
return Partition(est.predict(coords))
示例3: gmm
# 需要导入模块: from sklearn.mixture import GaussianMixture [as 别名]
# 或者: from sklearn.mixture.GaussianMixture import predict [as 别名]
def gmm(k, X, run_times=5):
gm = GMM(k, n_init=run_times, init_params='kmeans')
#gm = GMM(k)
gm.fit(X)
zh = gm.predict(X)
mu = gm.means_
cov = gm.covariances_
return zh, mu, cov
示例4: gmm
# 需要导入模块: from sklearn.mixture import GaussianMixture [as 别名]
# 或者: from sklearn.mixture.GaussianMixture import predict [as 别名]
def gmm(k, X, run_times=10, init='kmeans'):
"""GMM from sklearn library. init = {'kmeans', 'random'}, run_times
is the number of times the algorithm is gonna run with different
initializations.
"""
gm = GMM(k, n_init=run_times, init_params=init)
gm.fit(X)
zh = gm.predict(X)
return zh
示例5: int
# 需要导入模块: from sklearn.mixture import GaussianMixture [as 别名]
# 或者: from sklearn.mixture.GaussianMixture import predict [as 别名]
# Make data array to be put through the GMM - 5 components: 3 PCs, scaled energy, amplitude
this_cluster = np.where(predictions == int(clusters[0]))[0]
n_pc = 3
data = np.zeros((len(this_cluster), n_pc + 2))
data[:,2:] = pca_slices[this_cluster,:n_pc]
data[:,0] = energy[this_cluster]/np.max(energy[this_cluster])
data[:,1] = np.abs(amplitudes[this_cluster])/np.max(np.abs(amplitudes[this_cluster]))
# Cluster the data
g = GaussianMixture(n_components = n_clusters, covariance_type = 'full', tol = thresh, max_iter = n_iter, n_init = n_restarts)
g.fit(data)
# Show the cluster plots if the solution converged
if g.converged_:
split_predictions = g.predict(data)
x = np.arange(len(spike_waveforms[0])/10) + 1
for cluster in range(n_clusters):
split_points = np.where(split_predictions == cluster)[0]
# plt.figure(cluster)
slices_dejittered = spike_waveforms[this_cluster, :] # Waveforms and times from the chosen cluster
times_dejittered = spike_times[this_cluster]
times_dejittered = times_dejittered[split_points] # Waveforms and times from the chosen split of the chosen cluster
ISIs = np.ediff1d(np.sort(times_dejittered))/30.0
violations1 = 100.0*float(np.sum(ISIs < 1.0)/split_points.shape[0])
violations2 = 100.0*float(np.sum(ISIs < 2.0)/split_points.shape[0])
fig, ax = blech_waveforms_datashader.waveforms_datashader(slices_dejittered[split_points, :], x)
# plt.plot(x-15, slices_dejittered[split_points, :].T, linewidth = 0.01, color = 'red')
ax.set_xlabel('Sample (30 samples per ms)')
ax.set_ylabel('Voltage (microvolts)')
ax.set_title("Split Cluster{:d}, 2ms ISI violations={:.1f} percent".format(cluster, violations2) + "\n" + "1ms ISI violations={:.1f}%, Number of waveforms={:d}".format(violations1, split_points.shape[0]))
示例6: expand
# 需要导入模块: from sklearn.mixture import GaussianMixture [as 别名]
# 或者: from sklearn.mixture.GaussianMixture import predict [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)
plt.figure(figsize=(6, 6), facecolor='w')
plt.suptitle('GMM/DPGMM比较', fontsize=15)
ax = plt.subplot(211)
gmm = GaussianMixture(n_components=n_components, covariance_type='full', random_state=0)
gmm.fit(x)
centers = gmm.means_
covs = gmm.covariances_
print('GMM均值 = \n', centers)
print('GMM方差 = \n', covs)
y_hat = gmm.predict(x)
grid_hat = gmm.predict(grid_test)
grid_hat = grid_hat.reshape(x1.shape)
plt.pcolormesh(x1, x2, grid_hat, cmap=cm)
plt.scatter(x[:, 0], x[:, 1], s=20, c=y, cmap=cm, marker='o', edgecolors='#202020')
clrs = list('rgbmy')
for i, (center, cov) in enumerate(zip(centers, covs)):
value, vector = sp.linalg.eigh(cov)
width, height = value[0], value[1]
v = vector[0] / sp.linalg.norm(vector[0])
angle = 180* np.arctan(v[1] / v[0]) / np.pi
e = Ellipse(xy=center, width=width, height=height,
angle=angle, color=clrs[i], alpha=0.5, clip_box = ax.bbox)
ax.add_artist(e)
示例7: enumerate
# 需要导入模块: from sklearn.mixture import GaussianMixture [as 别名]
# 或者: from sklearn.mixture.GaussianMixture import predict [as 别名]
N2 = 300
N = N1 + N2
x1 = np.random.multivariate_normal(mean=(1, 2), cov=cov1, size=N1)
m = np.array(((1, 1), (1, 3)))
x1 = x1.dot(m)
x2 = np.random.multivariate_normal(mean=(-1, 10), cov=cov1, size=N2)
x = np.vstack((x1, x2))
y = np.array([0]*N1 + [1]*N2)
types = ('spherical', 'diag', 'tied', 'full')
err = np.empty(len(types))
bic = np.empty(len(types))
for i, type in enumerate(types):
gmm = GaussianMixture(n_components=2, covariance_type=type, random_state=0)
gmm.fit(x)
err[i] = 1 - accuracy_rate(gmm.predict(x), y)
bic[i] = gmm.bic(x)
print('错误率:', err.ravel())
print('BIC:', bic.ravel())
xpos = np.arange(4)
plt.figure(facecolor='w')
ax = plt.axes()
b1 = ax.bar(xpos-0.3, err, width=0.3, color='#77E0A0', edgecolor='k')
b2 = ax.twinx().bar(xpos, bic, width=0.3, color='#FF8080', edgecolor='k')
plt.grid(b=True, ls=':', color='#606060')
bic_min, bic_max = expand(bic.min(), bic.max())
plt.ylim((bic_min, bic_max))
plt.xticks(xpos, types)
plt.legend([b1[0], b2[0]], ('错误率', 'BIC'))
plt.title('不同方差类型的误差率和BIC', fontsize=15)
plt.show()
示例8: print
# 需要导入模块: from sklearn.mixture import GaussianMixture [as 别名]
# 或者: from sklearn.mixture.GaussianMixture import predict [as 别名]
d = (b - a) * 0.05
return a-d, b+d
if __name__ == '__main__':
data = np.loadtxt('..\\HeightWeight.csv', dtype=np.float, delimiter=',', skiprows=1)
print(data.shape)
y, x = np.split(data, [1, ], axis=1)
x, x_test, y, y_test = train_test_split(x, y, train_size=0.6, random_state=0)
gmm = GaussianMixture(n_components=2, covariance_type='full', random_state=0)
x_min = np.min(x, axis=0)
x_max = np.max(x, axis=0)
gmm.fit(x)
print('均值 = \n', gmm.means_)
print('方差 = \n', gmm.covariances_)
y_hat = gmm.predict(x)
y_test_hat = gmm.predict(x_test)
change = (gmm.means_[0][0] > gmm.means_[1][0])
if change:
z = y_hat == 0
y_hat[z] = 1
y_hat[~z] = 0
z = y_test_hat == 0
y_test_hat[z] = 1
y_test_hat[~z] = 0
acc = np.mean(y_hat.ravel() == y.ravel())
acc_test = np.mean(y_test_hat.ravel() == y_test.ravel())
acc_str = '训练集准确率:%.2f%%' % (acc * 100)
acc_test_str = '测试集准确率:%.2f%%' % (acc_test * 100)
print(acc_str)
print(acc_test_str)
示例9: main
# 需要导入模块: from sklearn.mixture import GaussianMixture [as 别名]
# 或者: from sklearn.mixture.GaussianMixture import predict [as 别名]
#.........这里部分代码省略.........
reduced_data = Rtsne(norm_counts, num_dimensions,
theta=tsne_theta, perplexity=tsne_perplexity)
elif "PCA" in dimensionality:
n_comps = num_dimensions
solver = "auto"
if pca_auto_components is not None:
n_comps = pca_auto_components
solver = "full"
decomp_model = PCA(n_components=n_comps, svd_solver=solver,
whiten=True, copy=True)
elif "ICA" in dimensionality:
decomp_model = FastICA(n_components=num_dimensions,
algorithm='parallel', whiten=True,
fun='logcosh', w_init=None, random_state=None)
elif "SPCA" in dimensionality:
import multiprocessing
decomp_model = SparsePCA(n_components=num_dimensions, alpha=1,
n_jobs=multiprocessing.cpu_count()-1)
elif "FactorAnalysis" in dimensionality:
decomp_model = FactorAnalysis(n_components=num_dimensions)
else:
sys.stderr.write("Error, incorrect dimensionality reduction method\n")
sys.exit(1)
if not "tSNE" in dimensionality:
# Perform dimensionality reduction, outputs a bunch of 2D/3D coordinates
reduced_data = decomp_model.fit_transform(norm_counts)
print("Performing clustering...")
# Do clustering of the dimensionality reduced coordinates
if "KMeans" in clustering:
labels = KMeans(init='k-means++',
n_clusters=num_clusters,
n_init=10).fit_predict(reduced_data)
elif "Hierarchical" in clustering:
labels = AgglomerativeClustering(n_clusters=num_clusters,
affinity='euclidean',
linkage='ward').fit_predict(reduced_data)
elif "DBSCAN" in clustering:
labels = DBSCAN(eps=dbscan_eps, min_samples=dbscan_min_size,
metric='euclidean', n_jobs=-1).fit_predict(reduced_data)
elif "Gaussian" in clustering:
gm = GaussianMixture(n_components=num_clusters,
covariance_type='full').fit(reduced_data)
labels = gm.predict(reduced_data)
else:
sys.stderr.write("Error, incorrect clustering method\n")
sys.exit(1)
# Check if there are -1 in the labels and that the number of labels is correct
if -1 in labels or len(labels) != len(norm_counts.index):
sys.stderr.write("Error, something went wrong in the clustering..\n")
sys.exit(1)
# We do not want zeroes in the labels
if 0 in labels: labels = labels + 1
# Write the spots and their classes to a file
file_writers = [open(os.path.join(outdir,
"{}_clusters.tsv".format(
os.path.splitext(os.path.basename(name))[0])),"w")
for name in counts_table_files]
# Write the coordinates and the label/class that they belong to
spot_plot_data = defaultdict(lambda: [[],[],[],[]])
for i, spot in enumerate(norm_counts.index):
tokens = spot.split("x")
示例10: PrettyTable
# 需要导入模块: from sklearn.mixture import GaussianMixture [as 别名]
# 或者: from sklearn.mixture.GaussianMixture import predict [as 别名]
t = PrettyTable(['Method', 'Accuracy'])
km = KMeans(k, n_init=5)
km.fit(Y)
zh_kmeans = km.labels_
x1_kmeans = X[np.where(zh_kmeans==0)][:, np.newaxis]
x2_kmeans = X[np.where(zh_kmeans==1)][:, np.newaxis]
x1_mu_kmeans, x2_mu_kmeans = km.cluster_centers_
x1_mu_kmeans, x2_mu_kmeans = x1_mu_kmeans[0], x2_mu_kmeans[0]
x1_var_kmeans, x2_var_kmeans = np.var(x1_kmeans), np.var(x2_kmeans)
acc_kmeans = metric.accuracy(z, zh_kmeans)
t.add_row(['k-means', acc_kmeans])
gm = GMM(k, n_init=5, init_params="kmeans")
gm.fit(Y)
zh_gmm = gm.predict(Y)
#x1_gmm = X[np.where(zh_gmm==0)][:, np.newaxis]
#x2_gmm = X[np.where(zh_gmm==1)][:, np.newaxis]
x1_mu_gmm, x2_mu_gmm = gm.means_
x1_mu_gmm, x2_mu_gmm = x1_mu_gmm[0], x2_mu_gmm[0]
x1_var_gmm, x2_var_gmm = gm.covariances_
x1_var_gmm, x2_var_gmm = x1_var_gmm[0][0], x2_var_gmm[0][0]
acc_gmm = metric.accuracy(z, zh_gmm)
t.add_row(['gmm', acc_gmm])
G = eclust.kernel_matrix(Y, lambda x, y: np.linalg.norm(x-y))
zh_kgroups = wrapper.kernel_kgroups(k, Y, G)
x1_kgroups = X[np.where(zh_kgroups==0)][:, np.newaxis]
x2_kgroups = X[np.where(zh_kgroups==1)][:, np.newaxis]
acc_kgroups = metric.accuracy(z, zh_kgroups)
t.add_row(['kernel k-groups', acc_kgroups])
示例11: timer
# 需要导入模块: from sklearn.mixture import GaussianMixture [as 别名]
# 或者: from sklearn.mixture.GaussianMixture import predict [as 别名]
start = timer()
zh = kernel_kmeans(k, G, Z0, W)
end = timer()
Zh = ztoZ(zh)
t.add_row(["kernel k-means (k-means++)", metric.accuracy(z, zh),
objective(Zh, G, W), end-start])
start = timer()
zh = kernel_kmeans(k, G, Z1, W)
end = timer()
Zh = ztoZ(zh)
t.add_row(["kernel k-means (spectral)", metric.accuracy(z, zh),
objective(Zh, G, W), end-start])
start = timer()
gmm = GMM(k)
gmm.fit(X)
zh = gmm.predict(X)
end = timer()
t.add_row(["GMM", metric.accuracy(z, zh), "-", end-start])
start = timer()
km = KMeans(k)
zh = km.fit_predict(X)
end = timer()
t.add_row(["k-means", metric.accuracy(z, zh), "-", end-start])
print t
示例12: GaussianMixture
# 需要导入模块: from sklearn.mixture import GaussianMixture [as 别名]
# 或者: from sklearn.mixture.GaussianMixture import predict [as 别名]
N2 = 300
N = N1 + N2
x1 = np.random.multivariate_normal(mean=(3, 2), cov=cov1, size=N1)
m = np.array(((1, 1), (1, 3)))
x1 = x1.dot(m)
x2 = np.random.multivariate_normal(mean=(-1, 10), cov=cov1, size=N2)
x = np.vstack((x1, x2))
y = np.array([0]*N1 + [1]*N2)
gmm = GaussianMixture(n_components=2, covariance_type='full', random_state=0)
gmm.fit(x)
centers = gmm.means_
covs = gmm.covariances_
print('GMM均值 = \n', centers)
print('GMM方差 = \n', covs)
y_hat = gmm.predict(x)
colors = '#A0FFA0', '#E080A0',
levels = 10
cm = mpl.colors.ListedColormap(colors)
x1_min, x1_max = x[:, 0].min(), x[:, 0].max()
x2_min, x2_max = x[:, 1].min(), x[:, 1].max()
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)
print(gmm.score_samples(grid_test))
grid_hat = -gmm.score_samples(grid_test)
grid_hat = grid_hat.reshape(x1.shape)
plt.figure(figsize=(7, 6), facecolor='w')
ax = plt.subplot(111)
示例13: gmm_analysis
# 需要导入模块: from sklearn.mixture import GaussianMixture [as 别名]
# 或者: from sklearn.mixture.GaussianMixture import predict [as 别名]
def gmm_analysis(self, X_train, X_test, y_train, y_test, data_set_name, max_clusters, analysis_name='GMM'):
scl = RobustScaler()
X_train_scl = scl.fit_transform(X_train)
X_test_scl = scl.transform(X_test)
em_bic = []
em_aic = []
em_completeness_score = []
em_homogeneity_score = []
em_measure_score = []
em_adjusted_rand_score = []
em_adjusted_mutual_info_score = []
cluster_range = np.arange(2, max_clusters+1, 1)
for k in cluster_range:
print('K Clusters: ', k)
##
## Expectation Maximization
##
em = GaussianMixture(n_components=k, covariance_type='full')
em.fit(X_train_scl)
em_pred = em.predict(X_train_scl)
em_bic.append(em.bic(X_train_scl))
em_aic.append(em.aic(X_train_scl))
# metrics
y_train_score = y_train.reshape(y_train.shape[0],)
em_homogeneity_score.append(homogeneity_score(y_train_score, em_pred))
em_completeness_score.append(completeness_score(y_train_score, em_pred))
em_measure_score.append(v_measure_score(y_train_score, em_pred))
em_adjusted_rand_score.append(adjusted_rand_score(y_train_score, em_pred))
em_adjusted_mutual_info_score.append(adjusted_mutual_info_score(y_train_score, em_pred))
##
## Plots
##
ph = plot_helper()
##
## BIC/AIC Plot
##
title = 'Information Criterion Plot (' + analysis_name + ') for ' + data_set_name
name = data_set_name.lower() + '_' + analysis_name.lower() + '_ic'
filename = './' + self.out_dir + '/' + name + '.png'
ph.plot_series(cluster_range,
[em_bic, em_aic],
[None, None],
['bic', 'aic'],
cm.viridis(np.linspace(0, 1, 2)),
['o', '*'],
title,
'Number of Clusters',
'Information Criterion',
filename)
##
## Score Plot
##
title = 'Score Summary Plot (' + analysis_name + ') for ' + data_set_name
name = data_set_name.lower() + '_' + analysis_name.lower() + '_score'
filename = './' + self.out_dir + '/' + name + '.png'
ph.plot_series(cluster_range,
[em_homogeneity_score, em_completeness_score, em_measure_score, em_adjusted_rand_score, em_adjusted_mutual_info_score],
[None, None, None, None, None, None],
['homogeneity', 'completeness', 'measure', 'adjusted_rand', 'adjusted_mutual_info'],
cm.viridis(np.linspace(0, 1, 5)),
['o', '^', 'v', '>', '<', '1'],
title,
'Number of Clusters',
'Score',
filename)