def test_score(): covar_type = 'full' rng = np.random.RandomState(0) rand_data = RandomData(rng, scale=7) n_components = rand_data.n_components X = rand_data.X[covar_type] # Check the error message if we don't call fit gmm1 = GaussianMixture(n_components=n_components, n_init=1, max_iter=1, reg_covar=0, random_state=rng, covariance_type=covar_type) assert_raise_message(NotFittedError, "This GaussianMixture instance is not fitted " "yet. Call 'fit' with appropriate arguments " "before using this method.", gmm1.score, X) # Check score value with warnings.catch_warnings(): warnings.simplefilter("ignore", ConvergenceWarning) gmm1.fit(X) gmm_score = gmm1.score(X) gmm_score_proba = gmm1.score_samples(X).mean() assert_almost_equal(gmm_score, gmm_score_proba) # Check if the score increase gmm2 = GaussianMixture(n_components=n_components, n_init=1, reg_covar=0, random_state=rng, covariance_type=covar_type).fit(X) assert_greater(gmm2.score(X), gmm1.score(X))
def test_score(): covar_type = 'full' rng = np.random.RandomState(0) rand_data = RandomData(rng, scale=7) n_components = rand_data.n_components X = rand_data.X[covar_type] # Check the error message if we don't call fit gmm1 = GaussianMixture(n_components=n_components, n_init=1, max_iter=1, reg_covar=0, random_state=rng, covariance_type=covar_type) assert_raise_message( NotFittedError, "This GaussianMixture instance is not fitted " "yet. Call 'fit' with appropriate arguments " "before using this method.", gmm1.score, X) # Check score value with warnings.catch_warnings(): warnings.simplefilter("ignore", ConvergenceWarning) gmm1.fit(X) gmm_score = gmm1.score(X) gmm_score_proba = gmm1.score_samples(X).mean() assert_almost_equal(gmm_score, gmm_score_proba) # Check if the score increase gmm2 = GaussianMixture(n_components=n_components, n_init=1, reg_covar=0, random_state=rng, covariance_type=covar_type).fit(X) assert_greater(gmm2.score(X), gmm1.score(X))
np.linspace(np.min(x[:, 1]), np.max(x[:, 1]), 50)) x_grid = np.c_[xx.ravel(), yy.ravel()] cov_type = 'full' # 'spherical', 'diag', 'tied', 'full' n_classes = 3 max_iters = 20 if sample_type == "" else 100 gmm = GMM(n_components=n_classes, covariance_type=cov_type, max_iter=max_iters, random_state=0) gmm.fit(x) logger.debug("Means:\n%s" % str(gmm.means_)) logger.debug("Covariances:\n%s" % str(gmm.covariances_)) scores = gmm.score_samples(x) top_anoms = np.argsort(scores)[np.arange(10)] if args.plot: # colors = ["red", "blue"] colors = ['navy', 'turquoise', 'darkorange'] pdfpath = "temp/gmm_%scontours.pdf" % sample_type dp = DataPlotter(pdfpath=pdfpath, rows=1, cols=2) pl = dp.get_next_plot() make_ellipses(gmm, pl, colors) dp.plot_points(x, pl, labels=y, lbl_color_map={