Trouble mapping a function to an array; what is wrong?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kulpojke
    New Member
    • Apr 2007
    • 4

    #1

    Trouble mapping a function to an array; what is wrong?

    I am trying to map a function to the contents of an array using the map() function. However, whenever the following function is called an error message is returned complaining that:

    ... line 121, in b_maker
    b[k] = map(function,b[k])
    TypeError: 'matrix' object is not callable

    This is on a windows32 system, using python 2.5 with the numpy library.
    In the following code data is a nested list of type float, target is a list of type float and function is a pre-defined function. In the first call to b_maker, where the problem occurs, function simply returns zero. I am confused because there are no matrix objects involved until after the line where the error is.

    Code:
    def b_maker(data,target,function):   
      b = ones((len(data)+1,1),float)                       
      for m in range(len(data)):                              
        b[m] = sqrt((data[m][0] - target[0])**2 + (data[m][1] - target[1])**2)  
      for k in range(len(b)-1):
        b[k] = map(function,b[k])   ##### <--Problem is here 
      b = array(b)
      b = matrix(b)
      b = b.T
      return b
    I get the same error form this function:

    Code:
    def A_inverse(data,function):   
      A = zeros((len(data),len(data)+1),float)
      for m in range(len(data)):                              
        for n in range(len(data)):                          
          A[m,n] = distance_in_data(data,m,n)                 
      for k in range(len(A)):
        A[k] = map(function,A[k])     ## <----Problem occurs here
        A[k][k] =0.0
      for k in range(len(A)):
        A[len(A)-1][k] = 1.0
      A[len(A)-1][len(A)] = 0.0
      A = array(A)
      A = matrix(A)
      A = A.I
      return A
    However the latter function is called and executes successfully many times before getting hung up and returning the error. I checked the data carefully to make sure there is not an invalid entry causing the problems, and all the functions being passed as the parameter function are fine. I am baffled.

    This is semi-urgent because it is part of a project that must be finished in three days. Thank you
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    I don't see why you are calling map() in a loop. map() goes through the iterable and returns an iterable with each element passed to and return from func:

    >>> def func1(data):
    ... return data + 1
    ...
    >>> aList = [1,2,3,4]
    >>> map(func1, aList)
    [2, 3, 4, 5]
    >>>

    Comment

    • kulpojke
      New Member
      • Apr 2007
      • 4

      #3
      That was a good point, and i have changed the code so that the loop is no longer included, but i still get the same error.

      now then code looks like:
      Code:
      def b_maker(data,target,function):   
        b = ones((len(data)+1,1),float)                        
        for m in range(len(data)):                              
          b[m] = sqrt((data[m][0] - target[0])**2 + (data[m][1] - target[1])**2)                                               
        b = map(function,b)                             
        b = array(b)                                           
        b = matrix(b)                                          
        b = b.T                                                
        return b
      but again, the error persists

      Comment

      • ghostdog74
        Recognized Expert Contributor
        • Apr 2006
        • 511

        #4
        how did you call these 2 functions. show the whole script.

        Comment

        • kulpojke
          New Member
          • Apr 2007
          • 4

          #5
          here it is:

          Code:
          def main(file_name):
            global i, max_lag, max_var, min_var, semivariogramz, w 
            w = [0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1]
            f = open(file_name,'r')
            data = tabfile_to_listnest(f)                                 # returns nested list
            f.close
            semivariogramz = semivar(data)                         # returns nested list
            i = len(semivariogramz) - 1
            max_lag = semivariogramz[i][0]
            max_var = semivariogramz[i][1]
            min_var = semivariogramz[0][1]
            F = [f0,f1,f2,f3,f4,f5,f6,f7,f8,f9]                            # f0...f9 defined elsewhere
            A_list = b_list = F
            for m in range(len(data)):
              target = data[m]
              
              for n in range(10):
                A_list[n] = A_maker(data,F[n])
                b_list[n] = b_maker(data,target,F[n])
          
          
          
          def A_maker(data,function):  
            A = zeros((len(data),len(data)+1),float)               
            for m in range(len(data)):                             
              for n in range(len(data)):                            
                A[m,n] = distance_in_data(data,m,n)                 
            A = map(function,A)
            for k in range(len(A)):
              A[k][k] = 0.0
            for k in range(len(A)):
              A[len(A)-1][k] = 1.0
            A[len(A)-1][len(A)] = 0.0
            A = array(A)
            A = matrix(A)
            return A
          
          
          def b_maker(data,target,function):  
            b = ones((len(data)+1,1),float)                         
            for m in range(len(data)):                              
              b[m] = sqrt((data[m][0] - target[0])**2 + (data[m][1] - target[1])**2)                
                                                                    
            b = map(function,b)                                     
            b = array(b)                                           
            b = matrix(b)                                                                                        
            return b

          Comment

          • ghostdog74
            Recognized Expert Contributor
            • Apr 2006
            • 511

            #6
            Originally posted by kulpojke
            here it is:

            Code:
            def main(file_name):
              global i, max_lag, max_var, min_var, semivariogramz, w 
              w = [0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1]
              f = open(file_name,'r')
              data = tabfile_to_listnest(f)                                 # returns nested list
              f.close
              semivariogramz = semivar(data)                         # returns nested list
              i = len(semivariogramz) - 1
              max_lag = semivariogramz[i][0]
              max_var = semivariogramz[i][1]
              min_var = semivariogramz[0][1]
              F = [f0,f1,f2,f3,f4,f5,f6,f7,f8,f9]                            # f0...f9 defined elsewhere
              A_list = b_list = F
              for m in range(len(data)):
                target = data[m]
                
                for n in range(10):
                  A_list[n] = A_maker(data,F[n])
                  b_list[n] = b_maker(data,target,F[n])
            
            
            
            def A_maker(data,function):  
              A = zeros((len(data),len(data)+1),float)               
              for m in range(len(data)):                             
                for n in range(len(data)):                            
                  A[m,n] = distance_in_data(data,m,n)                 
              A = map(function,A)
              for k in range(len(A)):
                A[k][k] = 0.0
              for k in range(len(A)):
                A[len(A)-1][k] = 1.0
              A[len(A)-1][len(A)] = 0.0
              A = array(A)
              A = matrix(A)
              return A
            
            
            def b_maker(data,target,function):  
              b = ones((len(data)+1,1),float)                         
              for m in range(len(data)):                              
                b[m] = sqrt((data[m][0] - target[0])**2 + (data[m][1] - target[1])**2)                
                                                                      
              b = map(function,b)                                     
              b = array(b)                                           
              b = matrix(b)                                                                                        
              return b

            this

            Code:
             A_list[n] = A_maker(data,F[n])
            is the problem. you are passing in a list F[n] to map() as the first argument which should be a function.
            [/code]

            Comment

            • kulpojke
              New Member
              • Apr 2007
              • 4

              #7
              Originally posted by ghostdog74
              this

              Code:
               A_list[n] = A_maker(data,F[n])
              is the problem. you are passing in a list F[n] to map() as the first argument which should be a function.
              [/code]

              But F[] is a list of functions. f0...f9 are defined elsewhere and F looks like:
              Code:
              F = [f0,f1,f2,f3,f4,f5,f6,f7,f8,f9]

              Comment

              • ghostdog74
                Recognized Expert Contributor
                • Apr 2006
                • 511

                #8
                Originally posted by kulpojke
                But F[] is a list of functions. f0...f9 are defined elsewhere and F looks like:
                Code:
                F = [f0,f1,f2,f3,f4,f5,f6,f7,f8,f9]
                if its a list of functions, then you have to call map on each of them individually. use a for loop to go through every item in this list and pass to A_maker

                Comment

                Working...