def test_numerically_robust_responsibilities(): random_state = check_random_state(0) n_samples = 300 n_features = 2 X = np.ndarray((n_samples, n_features)) mean0 = np.array([0.0, 1.0]) X[:n_samples // 3, :] = random_state.multivariate_normal( mean0, [[0.5, -1.0], [-1.0, 5.0]], size=(n_samples // 3, )) mean1 = np.array([-2.0, -2.0]) X[n_samples // 3:-n_samples // 3, :] = random_state.multivariate_normal( mean1, [[3.0, 1.0], [1.0, 1.0]], size=(n_samples // 3, )) mean2 = np.array([3.0, 1.0]) X[-n_samples // 3:, :] = random_state.multivariate_normal( mean2, [[3.0, -1.0], [-1.0, 1.0]], size=(n_samples // 3, )) # artificial scaling, makes naive implementation fail X[:, 1] *= 10000.0 gmm = GMM(n_components=3, random_state=random_state) gmm.from_samples(X, init_params="random") mean_dists = pdist(gmm.means) assert_true(all(mean_dists > 1)) assert_true(all(1e7 < gmm.covariances[:, 1, 1])) assert_true(all(gmm.covariances[:, 1, 1] < 1e9))
def test_kmeanspp_initialization(): random_state = check_random_state(1) n_samples = 300 n_features = 2 X = np.ndarray((n_samples, n_features)) X[:n_samples // 3, :] = random_state.multivariate_normal( [0.0, 1.0], [[0.5, -1.0], [-1.0, 5.0]], size=(n_samples // 3, )) X[n_samples // 3:-n_samples // 3, :] = random_state.multivariate_normal( [-2.0, -2.0], [[3.0, 1.0], [1.0, 1.0]], size=(n_samples // 3, )) X[-n_samples // 3:, :] = random_state.multivariate_normal( [3.0, 1.0], [[3.0, -1.0], [-1.0, 1.0]], size=(n_samples // 3, )) # artificial scaling, makes standard implementation fail # either the initial covariances have to be adjusted or we have # to normalize the dataset X[:, 1] *= 10000.0 gmm = GMM(n_components=3, random_state=random_state) gmm.from_samples(X, init_params="random") ellipses = gmm.to_ellipses() widths = np.array([ellipsis_params[1] for _, ellipsis_params in ellipses])[:, np.newaxis] average_widths_random = np.mean(pdist(widths)) gmm = GMM(n_components=3, random_state=random_state) gmm.from_samples(X, init_params="kmeans++") ellipses = gmm.to_ellipses() widths = np.array([ellipsis_params[1] for _, ellipsis_params in ellipses])[:, np.newaxis] average_widths_kmeanspp = np.mean(pdist(widths)) # random initialization produces uneven covariance scaling assert_less(average_widths_kmeanspp, average_widths_random)
def __initial_parameters(self, S, Sd): S = numpy.concatenate(S.swapaxes(1, 2), axis=0).T Sd = numpy.concatenate(Sd.swapaxes(1, 2), axis=0).T X = numpy.concatenate((S, Sd)).T gmm = GMM(self.n_components) gmm.from_samples(X) if self.verbose: print("Estimated GMM") n_task_dims = self.n_task_dims priors = gmm.priors means = numpy.ndarray(gmm.means.shape) means[:, :n_task_dims] = gmm.means[:, :n_task_dims] # Transform covariances such that they satisfy the optimization # constraints: covars = numpy.ndarray(gmm.covariances.shape) eye = numpy.tile(numpy.eye(n_task_dims), (self.n_components, 1, 1)) covars[:, :n_task_dims, :n_task_dims] = \ eye * numpy.abs(gmm.covariances[:, :n_task_dims, :n_task_dims]) covars[:, n_task_dims:, n_task_dims:] = \ eye * numpy.abs(gmm.covariances[:, n_task_dims:, n_task_dims:]) covars[:, n_task_dims:, :n_task_dims] = \ -eye * numpy.abs(gmm.covariances[:, n_task_dims:, :n_task_dims]) covars[:, :n_task_dims, n_task_dims:] = \ -eye * numpy.abs(gmm.covariances[:, :n_task_dims, n_task_dims:]) # Compute the rest of the means by solving the optimization constraint for k in range(self.n_components): means[k, n_task_dims:] = covars[k, n_task_dims:, :n_task_dims].dot( numpy.linalg.inv(covars[k, :n_task_dims, :n_task_dims]).dot( means[k, :n_task_dims] - self.attractor)) return priors, means, covars
def test_float_precision_error(): try: from sklearn.datasets import load_boston except ImportError: raise SkipTest("sklearn is not available") boston = load_boston() X, y = boston.data, boston.target gmm = GMM(n_components=10, random_state=2016) gmm.from_samples(X)
def test_float_precision_error(): try: from sklearn.datasets import load_boston except ImportError: raise SkipTest("sklearn is not available") boston = load_boston() X, y = boston.data, boston.target gmm = GMM(n_components=10, random_state=2016) gmm.from_samples(X)
def test_gmm_to_mvn_vs_mvn(): random_state = check_random_state(0) gmm = GMM(n_components=2, random_state=random_state) gmm.from_samples(X) mvn_from_gmm = gmm.to_mvn() mvn = MVN(random_state=random_state) mvn.from_samples(X) assert_array_almost_equal(mvn_from_gmm.mean, mvn.mean) assert_array_almost_equal(mvn_from_gmm.covariance, mvn.covariance, decimal=3)
def test_probability_density(): """Test PDF of GMM.""" global X global random_state gmm = GMM(n_components=2, random_state=random_state) gmm.from_samples(X) x = np.linspace(-100, 100, 201) X_grid = np.vstack(list(map(np.ravel, np.meshgrid(x, x)))).T p = gmm.to_probability_density(X_grid) approx_int = np.sum(p) * ((x[-1] - x[0]) / 201)**2 assert_less(np.abs(1.0 - approx_int), 0.01)
def test_probability_density(): """Test PDF of GMM.""" global X global random_state gmm = GMM(n_components=2, random_state=random_state) gmm.from_samples(X) x = np.linspace(-100, 100, 201) X_grid = np.vstack(map(np.ravel, np.meshgrid(x, x))).T p = gmm.to_probability_density(X_grid) approx_int = np.sum(p) * ((x[-1] - x[0]) / 201) ** 2 assert_less(np.abs(1.0 - approx_int), 0.01)
def test_verbose_from_samples(): """Test verbose output.""" global X random_state = check_random_state(0) old_stdout = sys.stdout sys.stdout = StringIO() try: gmm = GMM(n_components=2, verbose=True, random_state=random_state) gmm.from_samples(X) finally: out = sys.stdout.getvalue() sys.stdout.close() sys.stdout = old_stdout assert("converged" in out)
def test_verbose_from_samples(): """Test verbose output.""" global X random_state = check_random_state(0) old_stdout = sys.stdout sys.stdout = StringIO() try: gmm = GMM(n_components=2, verbose=True, random_state=random_state) gmm.from_samples(X) finally: out = sys.stdout.getvalue() sys.stdout.close() sys.stdout = old_stdout assert ("converged" in out)
def test_estimation_from_previous_initialization(): global X global random_state global means global covariances gmm = GMM(n_components=2, priors=0.5 * np.ones(2), means=np.copy(means), covariances=np.copy(covariances), random_state=check_random_state(2)) gmm.from_samples(X, n_iter=2) assert_less(np.linalg.norm(gmm.means[0] - means[0]), 0.01) assert_less(np.linalg.norm(gmm.covariances[0] - covariances[0]), 0.03) assert_less(np.linalg.norm(gmm.means[1] - means[1]), 0.01) assert_less(np.linalg.norm(gmm.covariances[1] - covariances[1]), 0.04)
def test_regression_with_2d_input(): """Test regression with GMM and two-dimensional input.""" random_state = check_random_state(0) n_samples = 200 x = np.linspace(0, 2, n_samples)[:, np.newaxis] y1 = 3 * x[:n_samples // 2] + 1 y2 = -3 * x[n_samples // 2:] + 7 noise = random_state.randn(n_samples, 1) * 0.01 y = np.vstack((y1, y2)) + noise samples = np.hstack((x, x[::-1], y)) gmm = GMM(n_components=2, random_state=random_state) gmm.from_samples(samples) pred = gmm.predict(np.array([0, 1]), np.hstack((x, x[::-1]))) mse = np.sum((y - pred)**2) / n_samples
def test_regression_with_2d_input(): """Test regression with GMM and two-dimensional input.""" random_state = check_random_state(0) n_samples = 200 x = np.linspace(0, 2, n_samples)[:, np.newaxis] y1 = 3 * x[:n_samples / 2] + 1 y2 = -3 * x[n_samples / 2:] + 7 noise = random_state.randn(n_samples, 1) * 0.01 y = np.vstack((y1, y2)) + noise samples = np.hstack((x, x[::-1], y)) gmm = GMM(n_components=2, random_state=random_state) gmm.from_samples(samples) pred = gmm.predict(np.array([0, 1]), np.hstack((x, x[::-1]))) mse = np.sum((y - pred) ** 2) / n_samples
def test_from_samples_with_oas(): n_samples = 9 n_features = 2 X = np.ndarray((n_samples, n_features)) X[:n_samples // 3, :] = random_state.multivariate_normal( [0.0, 1.0], [[0.5, -1.0], [-1.0, 5.0]], size=(n_samples // 3, )) X[n_samples // 3:-n_samples // 3, :] = random_state.multivariate_normal( [-2.0, -2.0], [[3.0, 1.0], [1.0, 1.0]], size=(n_samples // 3, )) X[-n_samples // 3:, :] = random_state.multivariate_normal( [3.0, 3.0], [[3.0, -1.0], [-1.0, 1.0]], size=(n_samples // 3, )) gmm = GMM(n_components=3, random_state=random_state) gmm.from_samples(X, init_params="kmeans++", oracle_approximating_shrinkage=True) cond = gmm.condition(np.array([0]), np.array([1.0])) for i in range(cond.n_components): eigvals = np.linalg.eigvals(cond.covariances[i]) assert_true(all(eigvals >= 0))
def test_estimate_moments(): """Test moments estimated from samples and sampling from GMM.""" global X global random_state gmm = GMM(n_components=2, random_state=random_state) gmm.from_samples(X) assert_less(np.linalg.norm(gmm.means[0] - means[0]), 0.005) assert_less(np.linalg.norm(gmm.covariances[0] - covariances[0]), 0.01) assert_less(np.linalg.norm(gmm.means[1] - means[1]), 0.01) assert_less(np.linalg.norm(gmm.covariances[1] - covariances[1]), 0.03) X = gmm.sample(n_samples=100000) gmm = GMM(n_components=2, random_state=random_state) gmm.from_samples(X) assert_less(np.linalg.norm(gmm.means[0] - means[0]), 0.01) assert_less(np.linalg.norm(gmm.covariances[0] - covariances[0]), 0.03) assert_less(np.linalg.norm(gmm.means[1] - means[1]), 0.01) assert_less(np.linalg.norm(gmm.covariances[1] - covariances[1]), 0.04)
def test_estimate_moments(): """Test moments estimated from samples and sampling from GMM.""" global X global random_state gmm = GMM(n_components=2, random_state=random_state) gmm.from_samples(X) assert_less(np.linalg.norm(gmm.means[0] - means[0]), 0.005) assert_less(np.linalg.norm(gmm.covariances[0] - covariances[0]), 0.01) assert_less(np.linalg.norm(gmm.means[1] - means[1]), 0.01) assert_less(np.linalg.norm(gmm.covariances[1] - covariances[1]), 0.03) X = gmm.sample(n_samples=100000) gmm = GMM(n_components=2, random_state=random_state) gmm.from_samples(X) assert_less(np.linalg.norm(gmm.means[0] - means[0]), 0.01) assert_less(np.linalg.norm(gmm.covariances[0] - covariances[0]), 0.03) assert_less(np.linalg.norm(gmm.means[1] - means[1]), 0.01) assert_less(np.linalg.norm(gmm.covariances[1] - covariances[1]), 0.04)
def test_regression_without_noise(): """Test regression without noise.""" random_state = check_random_state(0) n_samples = 200 x = np.linspace(0, 2, n_samples)[:, np.newaxis] y1 = 3 * x[:n_samples // 2] + 1 y2 = -3 * x[n_samples // 2:] + 7 y = np.vstack((y1, y2)) samples = np.hstack((x, y)) gmm = GMM(n_components=2, random_state=random_state) gmm.from_samples(samples) assert_array_almost_equal(gmm.priors, 0.5 * np.ones(2), decimal=2) assert_array_almost_equal(gmm.means[0], np.array([1.5, 2.5]), decimal=2) assert_array_almost_equal(gmm.means[1], np.array([0.5, 2.5]), decimal=1) pred = gmm.predict(np.array([0]), x) mse = np.sum((y - pred)**2) / n_samples assert_less(mse, 0.01)
def test_regression_without_noise(): """Test regression without noise.""" random_state = check_random_state(0) n_samples = 200 x = np.linspace(0, 2, n_samples)[:, np.newaxis] y1 = 3 * x[:n_samples / 2] + 1 y2 = -3 * x[n_samples / 2:] + 7 y = np.vstack((y1, y2)) samples = np.hstack((x, y)) gmm = GMM(n_components=2, random_state=random_state) gmm.from_samples(samples) assert_array_almost_equal(gmm.priors, 0.5 * np.ones(2), decimal=2) assert_array_almost_equal(gmm.means[0], np.array([1.5, 2.5]), decimal=2) assert_array_almost_equal(gmm.means[1], np.array([0.5, 2.5]), decimal=1) pred = gmm.predict(np.array([0]), x) mse = np.sum((y - pred) ** 2) / n_samples assert_less(mse, 0.01)
def test_kmeanspp_initialization(): random_state = check_random_state(0) n_samples = 300 n_features = 2 X = np.ndarray((n_samples, n_features)) mean0 = np.array([0.0, 1.0]) X[:n_samples // 3, :] = random_state.multivariate_normal( mean0, [[0.5, -1.0], [-1.0, 5.0]], size=(n_samples // 3, )) mean1 = np.array([-2.0, -2.0]) X[n_samples // 3:-n_samples // 3, :] = random_state.multivariate_normal( mean1, [[3.0, 1.0], [1.0, 1.0]], size=(n_samples // 3, )) mean2 = np.array([3.0, 1.0]) X[-n_samples // 3:, :] = random_state.multivariate_normal( mean2, [[3.0, -1.0], [-1.0, 1.0]], size=(n_samples // 3, )) # artificial scaling, makes standard implementation fail # either the initial covariances have to be adjusted or we have # to normalize the dataset X[:, 1] *= 10000.0 gmm = GMM(n_components=3, random_state=random_state) gmm.from_samples(X, init_params="random") # random initialization fails assert_less(gmm.covariances[0, 0, 0], np.finfo(float).eps) assert_less(gmm.covariances[1, 0, 0], np.finfo(float).eps) assert_less(gmm.covariances[2, 0, 0], np.finfo(float).eps) assert_less(gmm.covariances[0, 1, 1], np.finfo(float).eps) assert_less(gmm.covariances[1, 1, 1], np.finfo(float).eps) assert_less(gmm.covariances[2, 1, 1], np.finfo(float).eps) gmm = GMM(n_components=3, random_state=random_state) gmm.from_samples(X, init_params="kmeans++") mean_dists = pdist(gmm.means) assert_true(all(mean_dists > 1)) assert_true(all(1e7 < gmm.covariances[:, 1, 1])) assert_true(all(gmm.covariances[:, 1, 1] < 1e9))
random_state = check_random_state(0) X_test = np.linspace(0, 2 * np.pi, 100) Y_test = np.linspace(-1.5, 1.5) plt.figure(figsize=(10, 5)) n_samples = 100 X = np.ndarray((n_samples, 2)) X[:, 0] = np.linspace(0, 2 * np.pi, n_samples) X[:, 1] = np.sin(X[:, 0]) + random_state.randn(n_samples) * 0.1 gmm = GMM(n_components=3, random_state=0) gmm.from_samples(X) Y_x = gmm.predict(np.array([0]), X_test[:, np.newaxis]) Y_y = gmm.predict(np.array([1]), Y_test[:, np.newaxis]) plt.subplot(1, 2, 1) plt.title("Mixture of Experts: $p(Y | X) = \Sigma_k \pi_{k, Y|X} " "\mathcal{N}_{k, Y|X}$") plt.scatter(X[:, 0], X[:, 1]) plot_error_ellipses(plt.gca(), gmm, colors=["r", "g", "b"]) plt.plot(X_test, Y_x.ravel(), c="k", lw=2) plt.plot(Y_y.ravel(), Y_test, c="k", lw=2) X2 = np.loadtxt("samples", unpack=True) X2_test = np.linspace(-7.0, 4.0)#X2[:, 0] Y2_test = np.linspace(-4.0, 6.0)
data = df.to_numpy() print('Number of transitions in the data: %d' % data.shape[0]) if sys.argv[1] == 'train': ####################### # Train the GMM model # ####################### n_components = 500 gmm = GMM(n_components=n_components) print('Training the model with %d gaussian units' % n_components) gmm.from_samples(data, init='', plot_title=gmm_id, n_iter=100, savefig=True) print('Model ready') plt.show() # Save the model: with open(gmm_file, 'wb') as f: pickle.dump(gmm, f) elif sys.argv[1] == 'score': # Load the model: with open(gmm_file, 'rb') as f: gmm = pickle.load(f)
import matplotlib.pyplot as plt from gmr import GMM, plot_error_ellipses def f(y, random_state): eps = random_state.rand(*y.shape) * 0.2 - 0.1 return y + 0.3 * np.sin(2.0 * np.pi * y) + eps y = np.linspace(0, 1, 1000) random_state = np.random.RandomState(3) x = f(y, random_state) XY_train = np.column_stack((x, y)) gmm = GMM(n_components=4, random_state=random_state) gmm.from_samples(XY_train) plt.figure(figsize=(10, 5)) ax = plt.subplot(121) ax.set_title("Dataset and GMM") ax.scatter(x, y, s=1) colors = ["r", "g", "b", "orange"] plot_error_ellipses(ax, gmm, colors=colors) ax.set_xlabel("x") ax.set_ylabel("y") ax = plt.subplot(122) ax.set_title("Conditional Distribution") Y = np.linspace(0, 1, 1000) Y_test = Y[:, np.newaxis]
X_test = np.linspace(0, 2 * np.pi, 100) mean, covariance = mvn.predict(np.array([0]), X_test[:, np.newaxis]) plt.figure(figsize=(10, 5)) plt.subplot(1, 2, 1) plt.title("Linear: $p(Y | X) = \mathcal{N}(\mu_{Y|X}, \Sigma_{Y|X})$") plt.scatter(X[:, 0], X[:, 1]) y = mean.ravel() s = covariance.ravel() plt.fill_between(X_test, y - s, y + s, alpha=0.2) plt.plot(X_test, y, lw=2) n_samples = 100 X = np.ndarray((n_samples, 2)) X[:, 0] = np.linspace(0, 2 * np.pi, n_samples) X[:, 1] = np.sin(X[:, 0]) + random_state.randn(n_samples) * 0.1 gmm = GMM(n_components=3, random_state=0) gmm.from_samples(X) Y = gmm.predict(np.array([0]), X_test[:, np.newaxis]) plt.subplot(1, 2, 2) plt.title("Mixture of Experts: $p(Y | X) = \Sigma_k \pi_{k, Y|X} " "\mathcal{N}_{k, Y|X}$") plt.scatter(X[:, 0], X[:, 1]) plot_error_ellipses(plt.gca(), gmm, colors=["r", "g", "b"]) plt.plot(X_test, Y.ravel(), c="k", lw=2) plt.show()
sample_resolution = 50 data = [] for x in np.linspace(*x_limit, sample_resolution): for y in np.linspace(*y_limit, sample_resolution): data.append([x, y, F(x, y)]) data = np.array(data) X_data = data[:, :2] y_data = data[:, 2] ####### # GMR # ####### gmm = GMM(n_components=4) gmm.from_samples(data) #x0 = 0 x0 = 1.5 #y0 = 1.5 y0 = -1.5 z0 = np.squeeze( gmm.predict(np.array([0, 1]), np.array([x0, y0])[np.newaxis, :])) dxdy = np.squeeze( gmm.condition_derivative(np.array([0, 1]), np.array([x0, y0]))) print('z( %g, %g ):' % (x0, y0), z0) print('dydx( %g, %g ):' % (x0, y0), dxdy) Zp = np.zeros_like(Zr) for i, x in enumerate(x_scale):
[-2.0, -2.0], [[3.0, 1.0], [1.0, 1.0]], size=(n_samples // 3, )) X[-n_samples // 3:, :] = random_state.multivariate_normal( [3.0, 1.0], [[3.0, -1.0], [-1.0, 1.0]], size=(n_samples // 3, )) # artificial scaling, makes standard implementation fail # either the initial covariances have to be adjusted or we have # to normalize the dataset X[:, 1] *= 10000.0 plt.figure(figsize=(10, 10)) n_components = 3 initial_covs = np.empty((n_components, n_features, n_features)) initial_covs[:] = np.eye(n_features) gmm = GMM(n_components=n_components, random_state=random_state) gmm.from_samples(X, init_params="random", n_iter=0) plt.subplot(2, 2, 1) plt.title("Default initialization") plt.xlim((-10, 10)) plt.ylim((-100000, 100000)) plot_error_ellipses(plt.gca(), gmm, colors=["r", "g", "b"], alpha=0.15) plt.scatter(X[:, 0], X[:, 1]) plt.scatter(gmm.means[:, 0], gmm.means[:, 1], color=["r", "g", "b"]) gmm.from_samples(X) plt.subplot(2, 2, 2) plt.title("Trained Gaussian Mixture Model") plt.xlim((-10, 10)) plt.ylim((-100000, 100000))
""" ======================= Clustering Iris Dataset ======================= The Iris dataset is a typical classification problem. We will cluster it with GMM and see whether the clusters roughly match the classes. For better visualization will will first reduce the dimensionality to two with PCA from sklearn. We will also use sklearn to load the dataset. We display samples by dots with colors indicating their true class. The clusters are represented by their error ellipses. """ from sklearn.datasets import load_iris from sklearn.decomposition import PCA import matplotlib.pyplot as plt from gmr import GMM, plot_error_ellipses X, y = load_iris(return_X_y=True) X_pca = PCA(n_components=2, whiten=True, random_state=0).fit_transform(X) gmm = GMM(n_components=3, random_state=1) gmm.from_samples(X_pca) plt.figure() ax = plt.subplot(111) ax.scatter(X_pca[:, 0], X_pca[:, 1], c=y) plot_error_ellipses(ax, gmm, alpha=0.1, colors=["r", "g", "b"]) plt.show()
X_test = np.linspace(0, 2 * np.pi, 100) mean, covariance = mvn.predict(np.array([0]), X_test[:, np.newaxis]) plt.figure(figsize=(10, 5)) plt.subplot(1, 2, 1) plt.title("Linear: $p(Y | X) = \mathcal{N}(\mu_{Y|X}, \Sigma_{Y|X})$") plt.scatter(X[:, 0], X[:, 1]) y = mean.ravel() s = covariance.ravel() plt.fill_between(X_test, y - s, y + s, alpha=0.2) plt.plot(X_test, y, lw=2) n_samples = 100 X = np.ndarray((n_samples, 2)) X[:, 0] = np.linspace(0, 2 * np.pi, n_samples) X[:, 1] = np.sin(X[:, 0]) + random_state.randn(n_samples) * 0.1 gmm = GMM(n_components=3, random_state=0) gmm.from_samples(X) Y = gmm.predict(np.array([0]), X_test[:, np.newaxis]) plt.subplot(1, 2, 2) plt.title("Mixture of Experts: $p(Y | X) = \Sigma_k \pi_{k, Y|X} " "\mathcal{N}_{k, Y|X}$") plt.scatter(X[:, 0], X[:, 1]) plot_error_ellipses(plt.gca(), gmm, colors=["r", "g", "b"]) plt.plot(X_test, Y.ravel(), c="k", lw=2) plt.show()
def fit_parameters( self, data: DataFrame) -> Dict[str, Dict[str, CondMixtureGaussParams]]: """ Train params for Conditional Mixture Gaussian Node. Return: {"hybcprob": {<combination of outputs from discrete parents> : CondMixtureGaussParams}} """ hycprob = dict() values = [] combinations = [] for d_p in self.disc_parents: values.append(np.unique(data[d_p].values)) for xs in itertools.product(*values): combinations.append(list(xs)) for comb in combinations: mask = np.full(len(data), True) for col, val in zip(self.disc_parents, comb): mask = (mask) & (data[col] == val) new_data = data[mask] new_data.reset_index(inplace=True, drop=True) key_comb = [str(x) for x in comb] nodes = [self.name] + self.cont_parents if new_data.shape[0] > 5: if self.cont_parents: n_comp = int( (component(new_data, nodes, 'aic') + component(new_data, nodes, 'bic')) / 2 ) # component(new_data, nodes, 'LRTS')#int((component(new_data, nodes, 'aic') + component(new_data, nodes, 'bic')) / 2) # n_comp = 3 gmm = GMM(n_components=n_comp).from_samples( new_data[nodes].values, n_iter=500, init_params='kmeans++') else: n_comp = int( (component(new_data, [self.name], 'aic') + component(new_data, [self.name], 'bic')) / 2 ) # component(new_data, [node], 'LRTS')#int((component(new_data, [node], 'aic') + component(new_data, [node], 'bic')) / 2) # n_comp = 3 gmm = GMM(n_components=n_comp).from_samples( np.transpose([new_data[self.name].values]), n_iter=500, init_params='kmeans++') means = gmm.means.tolist() cov = gmm.covariances.tolist() # weigts = np.transpose(gmm.to_responsibilities(np.transpose([new_data[node].values]))) w = gmm.priors.tolist() # [] # for row in weigts: # w.append(np.mean(row)) hycprob[str(key_comb)] = { 'covars': cov, 'mean': means, 'coef': w } elif new_data.shape[0] != 0: n_comp = 1 gmm = GMM(n_components=n_comp) if self.cont_parents: gmm.from_samples(new_data[nodes].values) else: gmm.from_samples(np.transpose([new_data[self.name].values ])) means = gmm.means.tolist() cov = gmm.covariances.tolist() # weigts = np.transpose(gmm.to_responsibilities(np.transpose([new_data[node].values]))) w = gmm.priors.tolist() # [] # for row in weigts: # w.append(np.mean(row)) hycprob[str(key_comb)] = { 'covars': cov, 'mean': means, 'coef': w } else: if self.cont_parents: hycprob[str(key_comb)] = { 'covars': np.nan, 'mean': np.nan, 'coef': [] } else: hycprob[str(key_comb)] = { 'covars': np.nan, 'mean': np.nan, 'coef': [] } return {"hybcprob": hycprob}