コード例 #1
0
def test_gaussian_mixture_fit_predict_n_init():
    # Check that fit_predict is equivalent to fit.predict, when n_init > 1
    X = np.random.RandomState(0).randn(1000, 5)
    gm = GaussianMixture(n_components=5, n_init=5, random_state=0)
    y_pred1 = gm.fit_predict(X)
    y_pred2 = gm.predict(X)
    assert_array_equal(y_pred1, y_pred2)
コード例 #2
0
def test_gaussian_mixture_fit_predict_n_init():
    # Check that fit_predict is equivalent to fit.predict, when n_init > 1
    X = np.random.RandomState(0).randn(1000, 5)
    gm = GaussianMixture(n_components=5, n_init=5, random_state=0)
    y_pred1 = gm.fit_predict(X)
    y_pred2 = gm.predict(X)
    assert_array_equal(y_pred1, y_pred2)
コード例 #3
0
    def fuse(self, agent1, agent2):
        gmm = GaussianMixture(n_components=self.num_pattern).fit(np.concatenate((agent1.patterns[0], agent2.patterns[0])))
        s1 = agent1.S_label_pattern
        s2 = agent2.S_label_pattern
        idx1 = gmm.predict(agent1.patterns[0])
        idx2 = gmm.predict(agent2.patterns[0])

        s = np.ones((self.num_pattern, 10))
        for j in range(self.num_pattern):
            i1 = np.argwhere(idx1 == j)[:, 0]
            for i in i1:
                s[j, :] *= s1[i, :]
            i2 = np.argwhere(idx2 == j)[:, 0]
            for i in i2:
                s[j, :] *= s2[i, :]
        normalization_const = np.sum(s, axis=1, keepdims=True)
        # normalization_const = np.reshape(normalization_const, (self.num_pattern, 10))
        normalization_const = np.tile(normalization_const, (1, 10))
        s /= normalization_const

        return Agent(agent1.sess, (gmm.means_, gmm.covariances_), s)
コード例 #4
0
def test_gaussian_mixture_predict_predict_proba():
    rng = np.random.RandomState(0)
    rand_data = RandomData(rng)
    for covar_type in COVARIANCE_TYPE:
        X = rand_data.X[covar_type]
        Y = rand_data.Y
        g = GaussianMixture(n_components=rand_data.n_components,
                            random_state=rng, weights_init=rand_data.weights,
                            means_init=rand_data.means,
                            precisions_init=rand_data.precisions[covar_type],
                            covariance_type=covar_type)

        # Check a warning message arrive if we don't do fit
        assert_raise_message(NotFittedError,
                             "This GaussianMixture instance is not fitted "
                             "yet. Call 'fit' with appropriate arguments "
                             "before using this method.", g.predict, X)

        g.fit(X)
        Y_pred = g.predict(X)
        Y_pred_proba = g.predict_proba(X).argmax(axis=1)
        assert_array_equal(Y_pred, Y_pred_proba)
        assert_greater(adjusted_rand_score(Y, Y_pred), .95)
コード例 #5
0
def test_gaussian_mixture_predict_predict_proba():
    rng = np.random.RandomState(0)
    rand_data = RandomData(rng)
    for covar_type in COVARIANCE_TYPE:
        X = rand_data.X[covar_type]
        Y = rand_data.Y
        g = GaussianMixture(n_components=rand_data.n_components,
                            random_state=rng, weights_init=rand_data.weights,
                            means_init=rand_data.means,
                            precisions_init=rand_data.precisions[covar_type],
                            covariance_type=covar_type)

        # Check a warning message arrive if we don't do fit
        assert_raise_message(NotFittedError,
                             "This GaussianMixture instance is not fitted "
                             "yet. Call 'fit' with appropriate arguments "
                             "before using this method.", g.predict, X)

        g.fit(X)
        Y_pred = g.predict(X)
        Y_pred_proba = g.predict_proba(X).argmax(axis=1)
        assert_array_equal(Y_pred, Y_pred_proba)
        assert_greater(adjusted_rand_score(Y, Y_pred), .95)
コード例 #6
0
    g = GaussianMixture(n_components=N, n_init=N_INIT, init_params=INIT_METHOD)
    for j, side in enumerate(SIDES.keys()):

        data_ = data[side_index == j]
        labels_ = labels[side_index == j]

        final_labels = labels_.copy()
        # reencode the labels to take into account the added clusters
        final_labels[labels_ == 2] = 4
        # select central cluster
        data_ = data_[labels_ == 1]

        g.fit(data_)
        w = g.weights_
        m = g.means_
        c = g.covariances_
        pred_labels = g.predict(data_)

        # directly sort the labels according to the precentral coordinates of the cluster
        t = np.argsort(m, axis=0)[:, 0]
        sorted_means = m[t]
        sorted_cov = c[t]
        sorted_weigth = w[t]
        sorted_pred_labels = pred_labels.copy()

        for z, s in enumerate(t):
            # s+1 to account for the existence of ventral cluster
            sorted_pred_labels[pred_labels == s] = z + 1
        final_labels[labels_ == 1] = sorted_pred_labels
        np.save(CLUSTERING_LABELS[side], final_labels)
コード例 #7
0
estimator = GaussianMixture(n_components=2, max_iter=1000, random_state=0, init_params='random')


plt.figure()
plt.plot(X_train[:, 0], X_train[:, 1], 'k.', markersize=25)
plt.savefig('Figure_6-datapoints.png')
plt.close()

colors = ['r', 'b']


# initial means
estimator.means_init = np.array([[0, 0.5], [0.5, 0]])
# Train the other parameters using the EM algorithm.
estimator.fit(X_train)
classes = estimator.predict(X_train)

plt.figure()
for i in range(2):
    plt.plot(X_train[classes == i, 0], X_train[classes == i, 1], colors[i]+'.', markersize=25, label='class'+str(i+1))
    plt.plot(estimator.means_init[i, 0], estimator.means_init[i, 1], colors[i]+'*', markersize=20, label='mean_init')
    plt.plot(np.mean(X_train[classes == i, 0]), np.mean(X_train[classes == i, 1]), colors[i] + 'P', markersize=15, label='mean_init')
plt.title('mean_init='+str(estimator.means_init))
plt.legend()
plt.savefig('Figure_6-EM1.png')
plt.close()

# initial means
estimator.means_init = np.array([[0, 1], [0, -1]])
# Train the other parameters using the EM algorithm.
estimator.fit(X_train)