import shutil import random import numpy import cv from utils import cvarray from utils.prog_bar import ProgBar from gcp.gaussian import Gaussian from dpgmm import DPGMM # Tests the dpgmm model by seeing how well it fits a mixture of 2 1D Gaussians, which is visualised using open CV. # Define the Gaussian, draw the samples... dims = 1 gt_weight = [0.4, 0.6] gt = [Gaussian(dims), Gaussian(dims)] gt[0].setMean([5.0]) gt[0].setCovariance([[4.0]]) gt[1].setMean([-1.0]) gt[1].setCovariance([[4.0]]) sample_count = 2048 samples = [] for _ in xrange(sample_count): r = random.random() for i in xrange(len(gt)): r -= gt_weight[i] if r < 0.0: break x = gt[i].sample() samples.append(x)
import cv from utils import cvarray from utils.prog_bar import ProgBar from gcp.gaussian import Gaussian from dpgmm import DPGMM # Tests the dpgmm model by seeing how well it fits a single 1D Gaussian, which is visualised using open CV. # Define the Gaussian, draw the samples... dims = 1 gt = Gaussian(dims) gt.setMean([5.0]) gt.setCovariance([[4.0]]) sample_count = 2048 samples = [] for _ in xrange(sample_count): samples.append(gt.sample()) # Create the output directory... out_dir = 'test_1d_1mode' try: shutil.rmtree(out_dir) except: pass os.mkdir(out_dir)
import os import shutil import numpy import cv from utils import cvarray from utils.prog_bar import ProgBar from gcp.gaussian import Gaussian from dpgmm import DPGMM # Tests the dpgmm model by seeing how well it fits a single 1D Gaussian, which is visualised using open CV. # Define the Gaussian, draw the samples... dims = 1 gt = Gaussian(dims) gt.setMean([5.0]) gt.setCovariance([[4.0]]) sample_count = 2048 samples = [] for _ in xrange(sample_count): samples.append(gt.sample()) # Create the output directory... out_dir = 'test_1d_1mode' try: shutil.rmtree(out_dir) except: pass os.mkdir(out_dir)
import matplotlib.mlab as mlab import matplotlib.pyplot as plt from gcp.gaussian import Gaussian from scipy.integrate import quad import numpy import sys def f2(y,x,ctx): p=max(ctx.prob([x,y]),1e-15) return p*numpy.log(p) def f(x,ctx): r, abserr = quad(f2, -100, 100,args=(x,ctx)) return r ctx = Gaussian(2) ctx.setMean([1.0,1.0]) #一つでも対角成分が共分散より大きく低い数値だった場合エラーとなる ctx.setCovariance([[1.0,0.5],[0.5,1.0]]) sample = [] y, abserr = quad(f, -100, 100,args=(ctx)) print -y sys.exit(1) for _ in range(10000): sample.append(ctx.sample()[0]) print ctx.prob(sample[0]) #fig = plt.figure() #ax = fig.add_subplot(111)
#! /usr/bin/env python import os import sys from gcp.gaussian import Gaussian from dpgmm import DPGMM import numpy.random import time from scipy.integrate import quad dims=2 numpy.random.seed(1); gt = Gaussian(dims) gt.setMean([1.0,0.0]) gt.setCovariance([[1.0,0.8],[0.8,1.0]]) sample_count = 30000 sample=[] for _ in xrange(sample_count): sample.append(gt.sample()) f=open('data.txt','w') for x in sample: f.write('%lf,%lf\n'%(x[0],x[1])) f.close() model = DPGMM(dims, 1) for i,data in enumerate(sample): model.add(data) start = time.time() model.setPrior() elapsed_time = time.time() - start num=model.solve()