Esempio n. 1
0
def OMP_CV_ORACLE( pts, vals, pce ):
    # obtain best solver step using exact errors
    out = orthogonal_matching_pursuit( A, vals.squeeze(), 
                                       0., 
                                       numpy.iinfo(numpy.int32).max, 
                                       0 )

    #solutions = out[0]

    pce.set_coefficients( out[0] )
    pred_vals = pce.evaluate_set( test_pts )
    error_norms = numpy.sqrt( numpy.sum( errors**2, axis = 0 ) / 
                              test_vals.shape[0] )
    argmin = numpy.argmin( error_norms )
    pce.set_coefficients( out[0][:,argmin].reshape( out[0].shape[0], 1 ) )
Esempio n. 2
0
def least_squares_modified( pts, vals, pce, max_degree ):
        
    num_pts = pts.shape[1]
    num_reps = 2
    num_dims = pts.shape[0]

    indices = PolyIndexVector()
    from indexing_cpp import get_hyperbolic_indices
    all_indices = PolyIndexVector()
    get_hyperbolic_indices( num_dims, max_degree, 1, all_indices )
    #for index in all_indices:
    #    print index

    num_basis_terms =  min( 2*num_pts, len( all_indices ) / ( num_reps /2 ) )
    #num_basis_terms =  len( all_indices )
    print num_basis_terms

    key_sorter = lambda x: x.level_sum()

    indices_dict = dict()
    for n in xrange( num_reps ):
        I = numpy.random.randint( 0, len( all_indices ), (num_basis_terms) )
        #I = numpy.arange( len( all_indices ) )
        indices = PolyIndexVector()
        indices.resize( num_basis_terms )
        for i in xrange( I.shape[0] ):
            indices[i] = all_indices[int(I[i])]
            indices[i].set_array_index( i )

        indices_list = sorted( indices, 
                          key = key_sorter )[::-1]

        for i in xrange( len( indices ) ):
            indices[i] = indices_list[i]
            indices[i].set_array_index( i )

        #for index in indices:
        #    print index, max_degree

        pce.set_basis_indices( indices )
            
        A = pce.build_vandermonde( pts )
        
        #coeff = svd_solve_default( A, vals )[0]

        #pce.set_coefficients( coeff.reshape( ( coeff.shape[0], 1 ) ) )

        from compressed_sensing_cpp import orthogonal_matching_pursuit
        sols, metrics = orthogonal_matching_pursuit( A, vals.squeeze(), 
                                                     0., 
                                                     num_pts,
                                                     0 )

        coeff = sols[:,-1].reshape( sols.shape[0], 1 )
            
        #for index in indices:
        for i in metrics[1,:]:
            index = indices[int(i)]
            if not indices_dict.has_key( index ):
                indices_dict[index] = [coeff[index.get_array_index(),0]]
            else:
                indices_dict[index].append( coeff[index.get_array_index(),0] )

    best_indices = PolyIndexVector()
    best_indices.resize( len( indices_dict ) )
    i = 0
    for index in indices_dict:
        best_indices[i] = index
        i += 1
    
    key_sorter = lambda x: ( numpy.absolute( numpy.asarray( indices_dict[x] ) ).mean() )

    best_indices = sorted( best_indices, 
                           key = key_sorter )[::-1]

    
    i = 0
    for index in best_indices:
        print index, key_sorter( index )
        i += 1
        print i
        if i >= num_pts: 
            print i
            break
        
        assert False