Пример #1
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))
Пример #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
#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)

feat_train=RealFeatures(generated)

#train GMM using EM
est_gmm=GMM(3)
est_gmm.train(feat_train)
est_gmm.train_em(min_cov, max_iter, min_change)

#get and print estimated means and covariances
est_mean1=est_gmm.get_nth_mean(0)
est_mean2=est_gmm.get_nth_mean(1)
est_mean3=est_gmm.get_nth_mean(2)
est_cov1=est_gmm.get_nth_cov(0)
Пример #4
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)

generated = generated.transpose()
feat_train = RealFeatures(generated)

#train GMM using SMEM and print log-likelihood
est_smem_gmm = GMM(3, cov_type)
est_smem_gmm.train(feat_train)

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

est_smem_gmm.set_nth_cov(array([[1.0, 0.0], [0.0, 1.0]]), 0)