#print 'stick weights:' #print model.intMixture()[0] #print 'z sums:' #print model.z.sum(axis=0) # Now plot the estimated distribution against the actual distribution... img = numpy.ones((height, width, 3)) draw = model.sampleMixture() for px in xrange(width): x = float(px) / float(width) * (high - low) + low y_gt = 0.0 for ii in xrange(len(gt)): y_gt += gt_weight[ii] * gt[ii].prob([x]) y_gu = model.prob([x]) y_gd = 0.0 for ind, gauss in enumerate(draw[1]): y_gd += draw[0][ind] * gauss.prob([x]) py_gt = int((1.0 - y_gt / scale) * height) py_gu = int((1.0 - y_gu / scale) * height) py_gd = numpy.clip(int((1.0 - y_gd / scale) * height), 0, height - 1) img[py_gt, px, :] = [0.0, 1.0, 0.0] img[py_gu, px, :] = [1.0, 0.0, 0.0] img[py_gd, px, :] = [0.0, 0.0, 1.0] # Save plot out... img = cvarray.array2cv(img * 255.0)
p = ProgBar() it = model.solve() del p print 'Updated fitting in %i iterations'%it # Now plot the estimated distribution against the actual distribution... img = numpy.ones((height,width,3)) draw = model.sampleMixture() for px in xrange(width): x = float(px)/float(width) * (high-low) + low y_gt = 0.0 for ii in xrange(len(gt)): y_gt += gt_weight[ii] * gt[ii].prob([x]) y_gu = model.prob([x]) y_gd = 0.0 for ind,gauss in enumerate(draw[1]): y_gd += draw[0][ind] * gauss.prob([x]) py_gt = int((1.0 - y_gt/scale) * height) py_gu = int((1.0 - y_gu/scale) * height) py_gd = numpy.clip(int((1.0 - y_gd/scale) * height),0,height-1) img[py_gt,px,:] = [0.0,1.0,0.0] img[py_gu,px,:] = [1.0,0.0,0.0] img[py_gd,px,:] = [0.0,0.0,1.0] # Save plot out... img = cvarray.array2cv(img*255.0) cv.SaveImage('%s/plot_%i.png'%(out_dir,model.getStickCap()),img)
# Iterate, slowlly building up the number of samples used and outputting the fit for each... out = [8, 16, 32, 64, 128, 256, 512, 1024, 2048] model = DPGMM(dims, 8) model.setConcGamma(1 / 8., 1 / 8.) for i, point in enumerate(samples): model.add(point) if (i + 1) in out: print '%i datapoints:' % (i + 1) # First fit the model... model.setPrior() p = ProgBar() it = model.solve() del p print 'Updated fitting in %i iterations' % it # Calculate it's posterior distribution... y_post = model.prob(x[:, None]) # Generate and save a nice figure... plt.figure(figsize=(16, 8)) plt.xlim(x_low, x_high) plt.ylim(0.0, y_high) plt.plot(x, y_true, c='g') plt.plot(x, y_post, c='b') plt.savefig(os.path.join(out_dir, '{:04d}.png'.format(i + 1)), bbox_inches='tight')
# Iterate, slowlly building up the number of samples used and outputting the fit for each... out = [8,16,32,64,128,256,512,1024,2048] model = DPGMM(dims, 8) model.setConcGamma(1/8., 1/8.) for i,point in enumerate(samples): model.add(point) if (i+1) in out: print '%i datapoints:'%(i+1) # First fit the model... model.setPrior() p = ProgBar() it = model.solve() del p print 'Updated fitting in %i iterations'%it # Calculate it's posterior distribution... y_post = model.prob(x[:,None]) # Generate and save a nice figure... plt.figure(figsize=(16,8)) plt.xlim(x_low, x_high) plt.ylim(0.0, y_high) plt.plot(x, y_true, c='g') plt.plot(x, y_post, c='b') plt.savefig(os.path.join(out_dir, '{:04d}.png'.format(i+1)), bbox_inches='tight')
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() print elapsed_time print num print "%f"%(model.prob([2.0,1.0])) #for i in range(10): # x=i*0.4-2.0 #print "%f,%f"%(x,model.prob([x]))
out = [8,16,32,64,128,256,512,1024,2048] model = DPGMM(dims, 6) for i,point in enumerate(samples): model.add(point) model.setPrior() #p = ProgBar() it = model.solve() #del p img = ones((height,width,3)) draw = model.sampleMixture() for px in xrange(width): x = float(px)/float(width) * (high-low) + low for py in xrange(width): y = float(py)/float(width) * (high-low) + low # print "%f,%f"%(x,y) print "%f\t%f\t%f"%(x,y,model.prob([x,y])) sys.exit() # print [x,x] #norm_pdf_multivariate([x,x],mu1,cov) # if (i+1) in out: # print '%i datapoints:'%(i+1) # model.setPrior() # p = ProgBar() # it = model.solve() # del p # print 'Updated fitting in %i iterations'%it # Now plot the estimated distribution against the actual distribution... # img = ones((height,width,3)) # draw = model.sampleMixture() # for px in xrange(width):