e = Ellipse(xy=center, width=width, height=height, angle=angle, color='m', alpha=0.5, clip_box = ax.bbox) ax.add_artist(e) ax1_min, ax1_max, ax2_min, ax2_max = plt.axis() plt.xlim((x1_min, x1_max)) plt.ylim((x2_min, x2_max)) plt.title(u'GMM', fontsize=20) plt.grid(True) # DPGMM n_components = 3 dpgmm = DPGMM(n_components=n_components, alpha=1, covariance_type='full', random_state=0) dpgmm.fit(x) centers = dpgmm.means_ covs = dpgmm._get_covars() print 'DPGMM均值 = \n', centers print 'DPGMM方差 = \n', covs y_hat = dpgmm.predict(x) # print y_hat ax = plt.subplot(212) grid_hat = dpgmm.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=30, c=y, cmap=cm, marker='o') for i, cc in enumerate(zip(centers, covs)): if i not in y_hat: continue center, cov = cc
ax1_min, ax1_max, ax2_min, ax2_max = plt.axis() plt.xlim((x1_min, x1_max)) plt.ylim((x2_min, x2_max)) plt.title(u'GMM', fontsize=20) plt.grid(True) # DPGMM n_components = 3 dpgmm = DPGMM(n_components=n_components, alpha=1, covariance_type='full', random_state=0) dpgmm.fit(x) centers = dpgmm.means_ covs = dpgmm._get_covars() print 'DPGMM均值 = \n', centers print 'DPGMM方差 = \n', covs y_hat = dpgmm.predict(x) # print y_hat ax = plt.subplot(212) grid_hat = dpgmm.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=30, c=y, cmap=cm, marker='o') for i, cc in enumerate(zip(centers, covs)): if i not in y_hat: continue center, cov = cc