def test_predictions(): np.random.seed(222) D = np.random.rand(100,10) D[:40] += np.random.rand(40,10)**2 labels = [0] * 40 + [1] * 60 cmat,_,predictions = nfoldcrossvalidation(D, labels, classifier=fast_classifier(), return_predictions=1) assert np.all((predictions == 0)|(predictions == 1)) assert cmat.trace() == np.sum(predictions == labels)
def test_nfoldcrossvalidation_simple(): from milksets import wine features, labels = wine.load() features = features[::2] labels = labels[::2] cmat,clabels = nfoldcrossvalidation(features, labels, classifier=fast_classifier()) assert cmat.shape == (3,3) assert len(clabels) == 3
def test_stringlabels(): np.random.seed(222) D = np.random.rand(100,10) D[:40] += np.random.rand(40,10)**2 labelnames = ['one'] * 40 + ['two'] * 60 cmat,Lo = nfoldcrossvalidation(D, labelnames, classifier=fast_classifier()) assert Lo[0] in labelnames assert Lo[1] in labelnames assert Lo[0] != Lo[1] in labelnames
def test_nfoldcrossvalidation_simple(): from milksets import wine features, labels = wine.load() features = features[::2] labels = labels[::2] cmat, clabels = nfoldcrossvalidation(features, labels, classifier=fast_classifier()) assert cmat.shape == (3, 3) assert len(clabels) == 3
def test_predictions(): np.random.seed(222) D = np.random.rand(100, 10) D[:40] += np.random.rand(40, 10)**2 labels = [0] * 40 + [1] * 60 cmat, _, predictions = nfoldcrossvalidation(D, labels, classifier=fast_classifier(), return_predictions=1) assert np.all((predictions == 0) | (predictions == 1)) assert cmat.trace() == np.sum(predictions == labels)
def test_stringlabels(): np.random.seed(222) D = np.random.rand(100, 10) D[:40] += np.random.rand(40, 10)**2 labelnames = ['one'] * 40 + ['two'] * 60 cmat, Lo = nfoldcrossvalidation(D, labelnames, classifier=fast_classifier()) assert Lo[0] in labelnames assert Lo[1] in labelnames assert Lo[0] != Lo[1] in labelnames
def test_tree(): mtree = milk.supervised.multi.multi_tree_learner(fast_classifier()) labels = [0,1,2,2,3,3,3,3] features = np.random.random_sample((len(labels), 8)) model = mtree.train(features, labels) counts = np.zeros(4) for ell in labels: counts[ell] += 1 g0,g1 = milk.supervised.multi.split(counts) assert np.all(g0 == [3]) or np.all(g1 == [3]) def r(m): if len(m) == 1: return int(m[0]) else: return sorted([r(m[1]), r(m[2])]) assert r(model.model) == [3,[2,[0,1]]]
def test_tree(): mtree = milk.supervised.multi.multi_tree_learner(fast_classifier()) labels = [0, 1, 2, 2, 3, 3, 3, 3] features = np.random.random_sample((len(labels), 8)) model = mtree.train(features, labels) counts = np.zeros(4) for ell in labels: counts[ell] += 1 g0, g1 = milk.supervised.multi.split(counts) assert np.all(g0 == [3]) or np.all(g1 == [3]) def r(m): if len(m) == 1: return int(m[0]) else: return sorted([r(m[1]), r(m[2])]) assert r(model.model) == [3, [2, [0, 1]]]
def test_classifier_no_set_options(): # Basically these should not raise an exception milk.supervised.multi.one_against_rest_multi(fast_classifier()) milk.supervised.multi.one_against_rest(fast_classifier()) milk.supervised.multi.one_against_one(fast_classifier())