Exemple #1
0
def test_mds():
    from milk.unsupervised import pdist
    np.random.seed(232)
    for _ in range(12):
        features = np.random.random_sample((12, 4))
        X = milk.unsupervised.mds(features, 4)
        D = pdist(features)
        D2 = pdist(X)
        assert np.mean((D - D2)**2) < 10e-4
Exemple #2
0
def test_mds():
    from milk.unsupervised import pdist
    np.random.seed(232)
    for _ in range(12):
        features = np.random.random_sample((12,4))
        X = milk.unsupervised.mds(features,4)
        D = pdist(features)
        D2 = pdist(X)
        assert np.mean( (D - D2) ** 2) < 10e-4
Exemple #3
0
def test_pdist():
    np.random.seed(222)
    X = np.random.randn(100,23)
    Y = np.random.randn(80,23)
    Dxx = pdist(X)
    for i in xrange(X.shape[0]):
        for j in xrange(X.shape[1]):
            assert np.allclose(Dxx[i,j], np.sum((X[i]-X[j])**2))

    Dxy = pdist(X,Y)
    for i in xrange(X.shape[0]):
        for j in xrange(Y.shape[1]):
            assert np.allclose(Dxy[i,j], np.sum((X[i]-Y[j])**2))
Exemple #4
0
 def call_many(self, qs):
     from milk.unsupervised import pdist
     dists = pdist(self.X, qs, 'euclidean2')
     dists /= -self.sigma
     np.exp(dists, dists)
     dists *= self.beta
     return dists.T
Exemple #5
0
 def call_many(self, qs):
     from milk.unsupervised import pdist
     dists = pdist(self.X, qs, 'euclidean2')
     dists /= -self.sigma
     np.exp(dists, dists)
     dists *= self.beta
     return dists.T
Exemple #6
0
def test_mds_dists():
    from milk.unsupervised import pdist
    np.random.seed(232)
    for _ in xrange(12):
        features = np.random.random_sample((12,4))
        D = pdist(features)
        X = milk.unsupervised.mds(features,4)
        X2 = milk.unsupervised.mds_dists(D, 4)
        assert np.mean( (X - X2) ** 2) < 10e-4
Exemple #7
0
def test_mds_dists():
    from milk.unsupervised import pdist
    np.random.seed(232)
    for _ in xrange(12):
        features = np.random.random_sample((12, 4))
        D = pdist(features)
        X = milk.unsupervised.mds(features, 4)
        X2 = milk.unsupervised.mds_dists(D, 4)
        assert np.mean((X - X2)**2) < 10e-4
Exemple #8
0
def prune_similar(features, threshold=None, frac=None):
    from milk.unsupervised import pdist
    dists = pdist(features.T.astype(np.float32))
    if frac is not None:
        Ds = dists.ravel().copy()
        Ds.sort()
        threshold = Ds[int(frac*len(Ds))]
    elif threshold is None:
        threshold = 0
    X,Y = np.where(dists <= threshold)
    select = np.ones(len(features.T), bool)
    for x,y in zip(X,Y):
        if x != y and select[x] and select[y]:
            select[x] = 0
    return features[:,select]
Exemple #9
0
    np.seterr(all="ignore")

    print __doc__ + "\n"
    if not len(sys.argv) == 2:
        print misc.USAGE % __file__
        sys.exit(-1)
    else:
        dataset = sys.argv[1]

    print "Loading data ..."
    data = misc.load_data(dataset)

    # set sigma to something useful
    from milk.unsupervised import pdist

    sigma = np.median(pdist(data[0]))

    print "Done, %s samples with %s features loaded into " "memory" % data[0].shape

    score, res_shogun = misc.bench(bench_shogun, data)
    print "Shogun: mean %.2f, std %.2f" % (np.mean(res_shogun), np.std(res_shogun))
    print "Score: %.2f\n" % score

    score, res_mdp = misc.bench(bench_mdp, data)
    print "MDP: mean %.2f, std %.2f" % (np.mean(res_mdp), np.std(res_mdp))
    print "Score: %.2f\n" % score

    score, res_skl = misc.bench(bench_skl, data)
    print "scikits.learn: mean %.2f, std %.2f" % (np.mean(res_skl), np.std(res_skl))
    print "Score: %.2f\n" % score
    warnings.simplefilter('ignore')
    np.seterr(all='ignore')

    print __doc__ + '\n'
    if not len(sys.argv) == 2:
        print misc.USAGE % __file__
        sys.exit(-1)
    else:
        dataset = sys.argv[1]

    print 'Loading data ...'
    data = misc.load_data(dataset)

    # set sigma to something useful
    from milk.unsupervised import pdist
    sigma = np.median(pdist(data[0]))

    print 'Done, %s samples with %s features loaded into ' \
      'memory' % data[0].shape

    score, res_shogun = misc.bench(bench_shogun, data)
    print 'Shogun: mean %.2f, std %.2f' % (
        np.mean(res_shogun), np.std(res_shogun))
    print 'Score: %.2f\n' % score

    score, res_mdp = misc.bench(bench_mdp, data)
    print 'MDP: mean %.2f, std %.2f' % (
        np.mean(res_mdp), np.std(res_mdp))
    print 'Score: %.2f\n' % score

    score, res_skl = misc.bench(bench_skl, data)