def test1(): print '============== test1 ============' dimension = 3 target0 = make_target(dimension, 2, (0.75, 0.25), ((1, 1, 1), (2, 3, 4)), ((1, 1, 1), (0.5, 0.5, 1))) target1 = make_target(dimension, 2, (0.5, 0.5), ((-1, -1, -1), (-2, -3, -4)), ((1, 1, 1), (0.5, 0.5, 1))) target2 = make_target(dimension, 2, (0.1, 0.9), ((1, 1, -2), (3, 3, 5)), ((1, 1, 1), (0.5, 0.5, 1))) print target0 print target1 print target2 GaussianModelBase.seed(0) labels = ('A', 'B', 'C') ncomps = (1, 2, 2) sources = dict((('A', target0), ('B', target1), ('C', target2))) GaussianMixtureModel.seed(0) gmm_mgr = GmmMgr(ncomps, dimension, GaussianModelBase.DIAGONAL_COVARIANCE) c0 = AdaptingGmmClassifier(gmm_mgr, izip(labels, count())) print print c0 result = list() proc0 = AdaptingGmmClassProcessor(c0, result.append) # Prime things a little bit to try to get a good start c0.set_relevance(0.001) c0.set_num_em_iterations(2) for i in xrange(1): for label in labels: target = sources[label] data = (target.sample() for i in xrange(100)) proc0.process((label, data)) # Now adapt on more data c0.set_relevance(10) c0.set_num_em_iterations(2) for i in xrange(10): for label in labels: target = sources[label] data = (target.sample() for i in xrange(100)) proc0.process((label, data)) print print c0 print print len(result) # XXX Win32 gets values off in the last 2-3 hex digits. I'm not sure how to account for this in a # logref test, so I'm disabling this printing for now. # for training_label, scores in result[-10:]: # print training_label, tuple(((label, float_to_readable_string(score)) for score, label in scores)) correct = tuple(label for label, scores in result) guessed = tuple(scores[0][1] for l, scores in result) print len(correct), len(guessed) ind = [c == g for (c, g) in izip(correct, guessed)] print ind.count(True) print ind.count(True) / len(correct)
def test0(): print '============== test0 ============' dimension = 3 target0 = make_target(dimension, 2, (0.75, 0.25), ((1, 1, 1), (2, 3, 4)), ((1, 1, 1), (0.5, 0.5, 1))) target1 = make_target(dimension, 2, (0.5, 0.5), ((-1, -1, -1), (-2, -3, -4)), ((1, 1, 1), (0.5, 0.5, 1))) target2 = make_target(dimension, 2, (0.1, 0.9), ((1, 1, -2), (3, 3, 5)), ((1, 1, 1), (0.5, 0.5, 1))) print target0 print target1 print target2 GaussianModelBase.seed(0) labels = ('A', 'B', 'C') ncomps = (1, 2, 2) sources = dict((('A', target0), ('B', target1), ('C', target2))) GaussianMixtureModel.seed(0) gmm_mgr = GmmMgr(ncomps, dimension, GaussianModelBase.DIAGONAL_COVARIANCE) c0 = AdaptingGmmClassifier(gmm_mgr, izip(labels, count())) print print c0 # Prime things a little bit to try to get a good start c0.set_relevance(0.001) for i in xrange(1): for label in labels: target = sources[label] data = (target.sample() for i in xrange(100)) c0.adapt_one_class(label, data) # Now adapt on more data c0.set_relevance(10) for i in xrange(10): for label in labels: target = sources[label] data = (target.sample() for i in xrange(100)) c0.adapt_one_class(label, data) print print c0 print