示例#1
0
    def test003_cluster(self):
        N = 250
        mu = np.array([[-2, 0], [2, 2], [1.1, -0.95]])
        Sigma = np.array([[[1., -0.1], [-0.1, 1.3]], [[0.6, 0.2], [0.2, 1.8]],
                          [[2.2, -0.3], [-0.3, 0.7]]])

        X1 = np.random.multivariate_normal(mu[0], Sigma[0], size=N // 3)
        X2 = np.random.multivariate_normal(mu[1], Sigma[1], size=N // 3)
        X3 = np.random.multivariate_normal(mu[2], Sigma[2], size=N // 3)
        X = np.vstack((X1, X2, X3))

        gmm = GaussianMixtureModel(3, do_logging=True)
        gmm.fit(X, callback=plot_progress_2D, num_restarts=5)

        x_min, x_max = np.min(X[:, 0]), np.max(X[:, 0])
        y_min, y_max = np.min(X[:, 1]), np.max(X[:, 1])
        x = np.linspace(1.1 * x_min, 1.1 * x_max, 200)
        y = np.linspace(1.1 * y_min, 1.1 * y_max, 200)

        xx, yy = np.meshgrid(x, y)
        xxyy = np.array([xx.ravel(), yy.ravel()]).T
        p = gmm.density(xxyy).reshape(xx.shape)
        plt.scatter(X1[:, 0], X1[:, 1], color='r', marker='x')
        plt.scatter(X2[:, 0], X2[:, 1], color='m', marker='o')
        plt.scatter(X3[:, 0], X3[:, 1], color='g', marker='v')
        plt.contourf(x, y, p, alpha=0.5)
        plt.colorbar()
        plt.xlabel('$x$')
        plt.ylabel('$y$')
        plt.title('GMM Density Estimates')
        plt.show()
        return
示例#2
0
    def test005_cluster_complex(self):
        N = 600
        K = 5
        m = 2

        np.random.seed(2718)
        mu = 3 * np.random.normal(size=(K, 2))
        Sigma = np.stack([np.eye(2)] * K)
        for _ in range(m):
            L = np.random.multivariate_normal(np.zeros(2), np.eye(2), size=K)
            Sigma += np.einsum('kp,kq->kpq', L, L)
        Sigma *= 0.5

        X = np.random.multivariate_normal(mu[0], Sigma[0], size=N // 2)
        for k in range(1, K):
            Xk = np.random.multivariate_normal(mu[k], Sigma[k], size=N // 2)
            X = np.vstack((X, Xk))

        gmm = GaussianMixtureModel(K + 3)
        gmm.fit(X, num_restarts=25)
        fig = gmm.plot_scatter_density(X)
        fig.show()
        fig.savefig('example2.png')
        plt.show()
        return
示例#3
0
 def test002_basic(self):
     N, p = 150, 5
     X = np.random.normal(size=(N, p))
     K = 2
     gmm = GaussianMixtureModel(K, do_logging=True)
     gmm.fit(X)
     return
示例#4
0
    def test001_basic(self):
        N, p = 150, 2
        X = 2 * np.random.normal(size=(N, p))
        K = 1  # Number of clusters

        gmm = GaussianMixtureModel(num_clusters=K, do_logging=True)
        gmm.fit(X)
        return
示例#5
0
    def test007_pruning(self):
        N = 200
        mu = np.array([[-4, 0], [4, 4]])
        Sigma = np.array([[[1., -0.1], [-0.1, 1.3]], [[0.6, 0.2], [0.2, 1.8]]])

        X1 = np.random.multivariate_normal(mu[0], Sigma[0], size=N // 2)
        X2 = np.random.multivariate_normal(mu[1], Sigma[1], size=N // 2)
        X = np.vstack((X1, X2))

        gmm = GaussianMixtureModel(200, do_logging=True, prune_clusters=True)
        gmm.fit(X, callback=plot_progress_2D, num_restarts=5)
        return
示例#6
0
    def test004_cluster_simple(self):
        N = 200
        mu = np.array([[-2, 0], [2, 2]])
        Sigma = np.array([[[1., -0.1], [-0.1, 1.3]], [[0.6, 0.2], [0.2, 1.8]]])

        X1 = np.random.multivariate_normal(mu[0], Sigma[0], size=N // 2)
        X2 = np.random.multivariate_normal(mu[1], Sigma[1], size=N // 2)
        X = np.vstack((X1, X2))

        gmm = GaussianMixtureModel(2)
        gmm.fit(X, num_restarts=10)
        fig = gmm.plot_scatter_density(X)
        fig.savefig('example1.png')
        plt.show()
        return
示例#7
0
    def test006_high_dim(self):
        N = 600
        K = 5
        m = 2
        p = 10

        mu = 3 * np.random.normal(size=(K, p))
        Sigma = np.stack([np.eye(p)] * K)
        for _ in range(m):
            L = np.random.multivariate_normal(np.zeros(p), np.eye(p), size=K)
            Sigma += np.einsum('kp,kq->kpq', L, L)
        Sigma *= 0.5

        X = np.random.multivariate_normal(mu[0], Sigma[0], size=N // 2)
        for k in range(1, K):
            Xk = np.random.multivariate_normal(mu[k], Sigma[k], size=N // 2)
            X = np.vstack((X, Xk))

        gmm = GaussianMixtureModel(K + 3)
        gmm.fit(X, num_restarts=25)
        return