예제 #1
0
    def fit_gmm(self, samples):
        """
        Runs a couple of em instances on random starting points and returns
        internal GMM representation of best instance
        """
        features = RealFeatures(samples.T)

        gmms = []
        log_likelihoods = zeros(self.num_runs_em)
        for i in range(self.num_runs_em):
            # set up Shogun's GMM class and run em (corresponds to random
            # initialisation)
            gmm = GMM(self.num_components)
            gmm.set_features(features)
            log_likelihoods[i] = gmm.train_em()
            gmms.append(gmm)

        max_idx = log_likelihoods.argmax()

        # construct Gaussian mixture components in internal representation
        components = []
        for i in range(self.num_components):
            mu = gmms[max_idx].get_nth_mean(i)
            Sigma = gmms[max_idx].get_nth_cov(i)
            components.append(Gaussian(mu, Sigma))

        # construct a Gaussian mixture model based on the best EM run
        pie = gmms[max_idx].get_coef()
        proposal = MixtureDistribution(components[0].dimension,
                                       self.num_components, components,
                                       Discrete(pie))

        return proposal
예제 #2
0
def generate_gmm_classification_data(request):
    from modshogun import GMM, Math

    num_classes = int(request.POST['num_classes'])
    gmm = GMM(num_classes)
    total = 40
    rng = 4.0
    num = total / num_classes
    for i in xrange(num_classes):
        gmm.set_nth_mean(np.array([Math.random(-rng, rng) for j in xrange(2)]),
                         i)
        cov_tmp = Math.normal_random(0.2, 0.1)
        cov = np.array([[1.0, cov_tmp], [cov_tmp, 1.0]], dtype=float)
        gmm.set_nth_cov(cov, i)

    data = []
    labels = []
    for i in xrange(num_classes):
        coef = np.zeros(num_classes)
        coef[i] = 1.0
        gmm.set_coef(coef)
        data.append(np.array([gmm.sample() for j in xrange(num)]).T)
        labels.append(np.array([i for j in xrange(num)]))

    data = np.hstack(data)
    data = data / (2.0 * rng)
    xmin = np.min(data[0, :])
    ymin = np.min(data[1, :])
    labels = np.hstack(labels)
    toy_data = []
    for i in xrange(num_classes * num):
        toy_data.append({
            'x': data[0, i] - xmin,
            'y': data[1, i] - ymin,
            'label': float(labels[i])
        })
    return HttpResponse(json.dumps(toy_data))
예제 #3
0
from modshogun import Gaussian, GMM
from modshogun import RealFeatures
import util

util.set_title('SMEM for 2d GMM example')

#set the parameters
max_iter = 100
max_cand = 5
min_cov = 1e-9
max_em_iter = 1000
min_change = 1e-9
cov_type = 0

#setup the real GMM
real_gmm = GMM(3)

real_gmm.set_nth_mean(array([2.0, 2.0]), 0)
real_gmm.set_nth_mean(array([-2.0, -2.0]), 1)
real_gmm.set_nth_mean(array([2.0, -2.0]), 2)

real_gmm.set_nth_cov(array([[1.0, 0.2], [0.2, 0.5]]), 0)
real_gmm.set_nth_cov(array([[0.2, 0.1], [0.1, 0.5]]), 1)
real_gmm.set_nth_cov(array([[0.3, -0.2], [-0.2, 0.8]]), 2)

real_gmm.set_coef(array([0.3, 0.4, 0.3]))

#generate training set from real GMM
generated = array([real_gmm.sample()])
for i in range(199):
    generated = append(generated, array([real_gmm.sample()]), axis=0)
예제 #4
0
from numpy import array, append, arange, empty, exp
from modshogun import Gaussian, GMM
from modshogun import RealFeatures
import util

util.set_title('SMEM for 1d GMM example')

#set the parameters
max_iter = 100
max_cand = 5
min_cov = 1e-9
max_em_iter = 1000
min_change = 1e-9

#setup the real GMM
real_gmm = GMM(3)

real_gmm.set_nth_mean(array([-2.0]), 0)
real_gmm.set_nth_mean(array([0.0]), 1)
real_gmm.set_nth_mean(array([2.0]), 2)

real_gmm.set_nth_cov(array([[0.3]]), 0)
real_gmm.set_nth_cov(array([[0.1]]), 1)
real_gmm.set_nth_cov(array([[0.2]]), 2)

real_gmm.set_coef(array([0.3, 0.5, 0.2]))

#generate training set from real GMM
generated = array([real_gmm.sample()])
for i in range(199):
    generated = append(generated, array([real_gmm.sample()]), axis=1)
예제 #5
0
from pylab import figure, show, connect, hist, plot, legend
from numpy import array, append, arange, empty, exp
from modshogun import Gaussian, GMM
from modshogun import RealFeatures
import util

util.set_title('EM for 1d GMM example')

#set the parameters
min_cov = 1e-9
max_iter = 1000
min_change = 1e-9

#setup the real GMM
real_gmm = GMM(3)

real_gmm.set_nth_mean(array([-2.0]), 0)
real_gmm.set_nth_mean(array([0.0]), 1)
real_gmm.set_nth_mean(array([2.0]), 2)

real_gmm.set_nth_cov(array([[0.3]]), 0)
real_gmm.set_nth_cov(array([[0.1]]), 1)
real_gmm.set_nth_cov(array([[0.2]]), 2)

real_gmm.set_coef(array([0.3, 0.5, 0.2]))

#generate training set from real GMM
generated = array([real_gmm.sample()])
for i in range(199):
    generated = append(generated, array([real_gmm.sample()]), axis=1)
예제 #6
0
from pylab import figure, scatter, contour, show, legend, connect
from numpy import array, append, arange, reshape, empty, exp
from modshogun import Gaussian, GMM
from modshogun import RealFeatures
import util

util.set_title('EM for 2d GMM example')

#set the parameters
min_cov = 1e-9
max_iter = 1000
min_change = 1e-9
cov_type = 0

#setup the real GMM
real_gmm = GMM(2)

real_gmm.set_nth_mean(array([1.0, 1.0]), 0)
real_gmm.set_nth_mean(array([-1.0, -1.0]), 1)

real_gmm.set_nth_cov(array([[1.0, 0.2], [0.2, 0.1]]), 0)
real_gmm.set_nth_cov(array([[0.3, 0.1], [0.1, 1.0]]), 1)

real_gmm.set_coef(array([0.3, 0.7]))

#generate training set from real GMM
generated = array([real_gmm.sample()])
for i in range(199):
    generated = append(generated, array([real_gmm.sample()]), axis=0)

generated = generated.transpose()