示例#1
0
def lle_noise():
    ''' LLE under noise for assignment 7'''
    datafile = np.load('flatroll_data.npz');
    data = datafile['Xflat'];
    
    noisyData1 = data + np.random.randn(data.shape[0],data.shape[1])*np.sqrt(0.2);
    noisyData2 = data + np.random.randn(data.shape[0],data.shape[1])*np.sqrt(1.8);  
    
    Y1 = imp.lle(noisyData1, 1, 'knn', 14, 1e0);
    Y2 = imp.lle(noisyData1, 1, 'knn',50,1e-2);
    
    Y3 = imp.lle(noisyData2, 1, 'knn',7,1e-1);
    Y4 = imp.lle(noisyData2, 1, 'knn', 50, 1e-2);
    
    pl.figure();
    pl.subplot(2,2,1);
    pl.scatter(noisyData1[0,:],noisyData1[1,:],c=Y1);
    pl.title('Low noise, k=14');
    
    pl.subplot(2,2,2);
    pl.scatter(noisyData1[0,:],noisyData1[1,:],c=Y2);
    pl.title('Low noise, k=50');
    
    pl.subplot(2,2,3);
    pl.scatter(noisyData2[0,:],noisyData2[1,:],c=Y3);
    pl.title('High noise, k=7');
    
    pl.subplot(2,2,4);
    pl.scatter(noisyData2[0,:],noisyData2[1,:],c=Y4);
    pl.title('High noise, k=50');
    
    pl.show();
示例#2
0
    def test_lle(self):
        n = 500
        Xt = 10. * np.random.rand(n, 2)
        X = np.append(Xt, 0.5 * np.random.randn(n, 8), 1)

        # Rotate data randomly.
        X = np.dot(X,
                   self.randrot(10).T)

        with self.subTest(n_rule='knn', k=30):
            Xp = imp.lle(X, 2, n_rule='knn', k=30, tol=1e-3)
            #self.plot(Xt, Xp, 'knn')
        with self.subTest(n_rule='eps-ball', epsilon=5.):
            Xp = imp.lle(X, 2, n_rule='eps-ball', epsilon=5., tol=1e-3)
            #self.plot(Xt, Xp, 'eps-ball')
        with self.subTest(n_rule='eps-ball', epsilon=0.5):
            with self.assertRaises(
                    ValueError,
                    msg='Graph should not be connected and raise ValueError.'):
                imp.lle(X, 2, n_rule='eps-ball', epsilon=0.5, tol=1e-3)
示例#3
0
def lle_test():
    n = 500
    print n
    Xt = 10 * np.random.rand(2, n)
    X = np.append(Xt, 0.5 * np.random.randn(8, n), 0)

    # Rotate data randomly.
    X = np.dot(randrot(10), X)

    for (n_rule, param) in [['knn', 30], ['eps-ball', 5], ['eps-ball', 0.5]]:
        try:
            Xp = imp.lle(X, 2, n_rule, param)
        except Exception as inst:
            print 'lle crashed for n_rule = ' + n_rule + ' = ' + str(
                param) + ':'
            print inst.args
            continue

        pl.figure(figsize=(14, 8))
        pl.subplot(1, 3, 1)
        pl.scatter(Xt[0, :], Xt[1, :], 30)
        pl.title('True 2D manifold')
        pl.ylabel('x_2')
        pl.xlabel('x_1')
        pl.xticks([], [])
        pl.yticks([], [])

        pl.subplot(1, 3, 2)
        pl.scatter(Xp[0, :], Xp[1, :], 30, Xt[0, :])
        pl.title(n_rule + ': embedding colored via x_1')
        pl.xlabel('x_1')
        pl.ylabel('x_2')
        pl.xticks([], [])
        pl.yticks([], [])

        pl.subplot(1, 3, 3)
        pl.scatter(Xp[0, :], Xp[1, :], 30, Xt[1, :])
        pl.title(n_rule + ': embedding colored via x_2')
        pl.xlabel('x_1')
        pl.ylabel('x_2')
        pl.xticks([], [])
        pl.yticks([], [])
        pl.show()

    print 'Tests for LLE passed? Check the figures!'
def lle_test():
    n = 500
    print n
    Xt = 10*np.random.rand(2,n);
    X = np.append( Xt, 0.5*np.random.randn(8,n), 0 );

    
    # Rotate data randomly.
    X = np.dot(randrot(10),X);
    

    for (n_rule, param) in [  ['knn',30],['eps-ball',5],['eps-ball',0.5] ]:
    #for (n_rule, param) in [  ['knn',30]]:
        try:
            Xp = imp.lle(X, 2, n_rule, param);
        except Exception as inst:
            print 'lle crashed for n_rule = ' + n_rule + ' = ' + str(param) + ':'
            print inst.args

        pl.figure(figsize=(14,8))
        pl.subplot(1,3,1)
        pl.scatter(Xt[0,:], Xt[1,:],30)
        pl.title('True 2D manifold')
        pl.ylabel('x_2')
        pl.xticks([],[]); pl.yticks([],[])
        
        pl.subplot(1,3,2);
        pl.scatter(Xp[0,:], Xp[1,:], 30, Xt[0,:]);
        pl.title(n_rule +': embedding colored via x_1');
        pl.xlabel('x_1')
        pl.xticks([],[]); pl.yticks([],[])

        pl.subplot(1,3,3);
        pl.scatter(Xp[0,:], Xp[1,:], 30, Xt[1,:]);
        pl.title(n_rule +': embedding colored via x_2');
        pl.xticks([],[]); pl.yticks([],[])
        
    print 'Tests for LLE passed? Check the figures!'

    print '\n(time the test takes on my notebook: approx. 2.5 seconds)'
示例#5
0
def my_lle_test():
    X = np.array([[0, 1, 0, 1], [0, 0, 1, 1]])
    imp.lle(X, 2, 'knn', 2)
示例#6
0
def lle_visualize(dataset='flatroll'):
    ''' visualization of LLE for assignment 6'''
    
    if dataset == 'flatroll':
        datafile = np.load('flatroll_data.npz');
        data = datafile['Xflat'];
        trueEmbedding = datafile['true_embedding'];
        #15, 1e-1
        Y = imp.lle(data, 1, 'knn', 9, 18e-3);
        
        pl.figure();
        pl.subplot(1,2,1);
        pl.scatter(data[0,:],data[1,:],c=trueEmbedding);
        pl.title('True embedding');
        pl.colorbar();
        pl.subplot(1,2,2);
        pl.scatter(data[0,:],data[1,:],c=Y);
        pl.title('LLE embedding');
        pl.colorbar();
        
        pl.show();
    
    if dataset == 'swissroll':
        datafile = np.load('swissroll_data.npz');
        
        data = datafile['x_noisefree'];
        trueEmbedding = datafile['z'];
        
        #6,1e-4
        Y = imp.lle(data,2,'knn',6,1e-4);
    
        fig = pl.figure();
        ax = fig.add_subplot(221, projection='3d');
        ax.scatter(data[0,:],data[1,:],data[2,:],c=trueEmbedding[0,:]);
        pl.title('True embedding');
        ax = fig.add_subplot(222, projection='3d');
        ax.scatter(data[0,:],data[1,:],data[2,:],c=Y[0,:]);
        pl.title('LLE embedding');
        
        pl.subplot(2,2,3);
        pl.scatter(trueEmbedding[0,:],trueEmbedding[1,:],c=trueEmbedding[0,:]);
        pl.title('True embedding');
        
        pl.subplot(2,2,4);
        pl.scatter(Y[0,:],Y[1,:],c=Y[0,:]);
        pl.title('LLE embedding');
        pl.show();
        
    if dataset == 'fishbowl':
        datafile = np.load('fishbowl_data.npz');
        
        data = datafile['x_noisefree'];
        trueEmbedding = datafile['z'];
        
        Y = imp.lle(data,2,'knn',7,1e-10);
    
        fig = pl.figure();
        ax = fig.add_subplot(221, projection='3d');
        ax.scatter(data[0,:],data[1,:],data[2,:],c=trueEmbedding[0,:]);
        pl.title('True embedding');
        ax = fig.add_subplot(222, projection='3d');
        ax.scatter(data[0,:],data[1,:],data[2,:],c=Y[0,:]);
        pl.title('LLE embedding');
        
        pl.subplot(2,2,3);
        pl.scatter(trueEmbedding[0,:],trueEmbedding[1,:],c=trueEmbedding[0,:]);
        pl.title('True embedding');
        
        pl.subplot(2,2,4);
        pl.scatter(Y[0,:],Y[1,:],c=Y[0,:]);
        pl.title('LLE embedding');
        pl.show();