473,466 Members | 1,389 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

4 New Member
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.

Expand|Select|Wrap|Line Numbers
  1. def b_maker(data,target,function):   
  2.   b = ones((len(data)+1,1),float)                       
  3.   for m in range(len(data)):                              
  4.     b[m] = sqrt((data[m][0] - target[0])**2 + (data[m][1] - target[1])**2)  
  5.   for k in range(len(b)-1):
  6.     b[k] = map(function,b[k])   ##### <--Problem is here 
  7.   b = array(b)
  8.   b = matrix(b)
  9.   b = b.T
  10.   return b
  11.  
I get the same error form this function:

Expand|Select|Wrap|Line Numbers
  1. def A_inverse(data,function):   
  2.   A = zeros((len(data),len(data)+1),float)
  3.   for m in range(len(data)):                              
  4.     for n in range(len(data)):                          
  5.       A[m,n] = distance_in_data(data,m,n)                 
  6.   for k in range(len(A)):
  7.     A[k] = map(function,A[k])     ## <----Problem occurs here
  8.     A[k][k] =0.0
  9.   for k in range(len(A)):
  10.     A[len(A)-1][k] = 1.0
  11.   A[len(A)-1][len(A)] = 0.0
  12.   A = array(A)
  13.   A = matrix(A)
  14.   A = A.I
  15.   return A
  16.  
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
Apr 23 '07 #1
7 1899
bartonc
6,596 Recognized Expert Expert
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]
>>>
Apr 23 '07 #2
kulpojke
4 New Member
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:
Expand|Select|Wrap|Line Numbers
  1. def b_maker(data,target,function):   
  2.   b = ones((len(data)+1,1),float)                        
  3.   for m in range(len(data)):                              
  4.     b[m] = sqrt((data[m][0] - target[0])**2 + (data[m][1] - target[1])**2)                                               
  5.   b = map(function,b)                             
  6.   b = array(b)                                           
  7.   b = matrix(b)                                          
  8.   b = b.T                                                
  9.   return b
  10.  
but again, the error persists
Apr 23 '07 #3
ghostdog74
511 Recognized Expert Contributor
how did you call these 2 functions. show the whole script.
Apr 23 '07 #4
kulpojke
4 New Member
here it is:

Expand|Select|Wrap|Line Numbers
  1. def main(file_name):
  2.   global i, max_lag, max_var, min_var, semivariogramz, w 
  3.   w = [0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1]
  4.   f = open(file_name,'r')
  5.   data = tabfile_to_listnest(f)                                 # returns nested list
  6.   f.close
  7.   semivariogramz = semivar(data)                         # returns nested list
  8.   i = len(semivariogramz) - 1
  9.   max_lag = semivariogramz[i][0]
  10.   max_var = semivariogramz[i][1]
  11.   min_var = semivariogramz[0][1]
  12.   F = [f0,f1,f2,f3,f4,f5,f6,f7,f8,f9]                            # f0...f9 defined elsewhere
  13.   A_list = b_list = F
  14.   for m in range(len(data)):
  15.     target = data[m]
  16.  
  17.     for n in range(10):
  18.       A_list[n] = A_maker(data,F[n])
  19.       b_list[n] = b_maker(data,target,F[n])
  20.  
  21.  
  22.  
  23. def A_maker(data,function):  
  24.   A = zeros((len(data),len(data)+1),float)               
  25.   for m in range(len(data)):                             
  26.     for n in range(len(data)):                            
  27.       A[m,n] = distance_in_data(data,m,n)                 
  28.   A = map(function,A)
  29.   for k in range(len(A)):
  30.     A[k][k] = 0.0
  31.   for k in range(len(A)):
  32.     A[len(A)-1][k] = 1.0
  33.   A[len(A)-1][len(A)] = 0.0
  34.   A = array(A)
  35.   A = matrix(A)
  36.   return A
  37.  
  38.  
  39. def b_maker(data,target,function):  
  40.   b = ones((len(data)+1,1),float)                         
  41.   for m in range(len(data)):                              
  42.     b[m] = sqrt((data[m][0] - target[0])**2 + (data[m][1] - target[1])**2)                
  43.  
  44.   b = map(function,b)                                     
  45.   b = array(b)                                           
  46.   b = matrix(b)                                                                                        
  47.   return b
  48.  
Apr 23 '07 #5
ghostdog74
511 Recognized Expert Contributor
here it is:

Expand|Select|Wrap|Line Numbers
  1. def main(file_name):
  2.   global i, max_lag, max_var, min_var, semivariogramz, w 
  3.   w = [0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1]
  4.   f = open(file_name,'r')
  5.   data = tabfile_to_listnest(f)                                 # returns nested list
  6.   f.close
  7.   semivariogramz = semivar(data)                         # returns nested list
  8.   i = len(semivariogramz) - 1
  9.   max_lag = semivariogramz[i][0]
  10.   max_var = semivariogramz[i][1]
  11.   min_var = semivariogramz[0][1]
  12.   F = [f0,f1,f2,f3,f4,f5,f6,f7,f8,f9]                            # f0...f9 defined elsewhere
  13.   A_list = b_list = F
  14.   for m in range(len(data)):
  15.     target = data[m]
  16.  
  17.     for n in range(10):
  18.       A_list[n] = A_maker(data,F[n])
  19.       b_list[n] = b_maker(data,target,F[n])
  20.  
  21.  
  22.  
  23. def A_maker(data,function):  
  24.   A = zeros((len(data),len(data)+1),float)               
  25.   for m in range(len(data)):                             
  26.     for n in range(len(data)):                            
  27.       A[m,n] = distance_in_data(data,m,n)                 
  28.   A = map(function,A)
  29.   for k in range(len(A)):
  30.     A[k][k] = 0.0
  31.   for k in range(len(A)):
  32.     A[len(A)-1][k] = 1.0
  33.   A[len(A)-1][len(A)] = 0.0
  34.   A = array(A)
  35.   A = matrix(A)
  36.   return A
  37.  
  38.  
  39. def b_maker(data,target,function):  
  40.   b = ones((len(data)+1,1),float)                         
  41.   for m in range(len(data)):                              
  42.     b[m] = sqrt((data[m][0] - target[0])**2 + (data[m][1] - target[1])**2)                
  43.  
  44.   b = map(function,b)                                     
  45.   b = array(b)                                           
  46.   b = matrix(b)                                                                                        
  47.   return b
  48.  

this

Expand|Select|Wrap|Line Numbers
  1.  A_list[n] = A_maker(data,F[n])
  2.  
is the problem. you are passing in a list F[n] to map() as the first argument which should be a function.
[/code]
Apr 23 '07 #6
kulpojke
4 New Member
this

Expand|Select|Wrap|Line Numbers
  1.  A_list[n] = A_maker(data,F[n])
  2.  
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:
Expand|Select|Wrap|Line Numbers
  1. F = [f0,f1,f2,f3,f4,f5,f6,f7,f8,f9]
  2.  
Apr 24 '07 #7
ghostdog74
511 Recognized Expert Contributor
But F[] is a list of functions. f0...f9 are defined elsewhere and F looks like:
Expand|Select|Wrap|Line Numbers
  1. F = [f0,f1,f2,f3,f4,f5,f6,f7,f8,f9]
  2.  
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
Apr 24 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

39
by: Erlend Fuglum | last post by:
Hi everyone, I'm having some trouble sorting lists. I suspect this might have something to do with locale settings and/or character encoding/unicode. Consider the following example, text...
6
by: naruto | last post by:
Hi all, I have the following being defined in a A.cxx file. // define in source file. Not exported to the outside world (this cannot be // moved to the header file ) #define CHANNEL_0 0...
8
by: Falc2199 | last post by:
Hi, Does anyone know how to make this work? var sectionId = 5; repeat_section_sectionId(); function repeat_section_5(){ alert("firing"); }
2
by: free2klim | last post by:
Hi, I am relatively new to programming C++ and I am writing a small program in which I am using a 3D array of type int. The 3D array is a member of class GameBoard, which I have defined in the...
1
by: rengaraj | last post by:
JBoss version:4.0.4 Hibernate version:3.2 Database:Oracle10g Hi. I am facing an entity mapping problem. I have an Oracle stored function which takes as a parameter an Oracle array and returns a...
2
by: 4Ankit | last post by:
hey guys i am having trouble changing my array code to include a 'for/ in' structure the code i am trying to change is below: <script type="text/javascript"> var contents = new Array(3)
9
by: Nathan Sokalski | last post by:
I am trying to use the System.Array.ForEach method in VB.NET. The action that I want to perform on each of the Array values is: Private Function AddQuotes(ByVal value As String) As String Return...
0
by: jthep | last post by:
Hi, I'm trying to get user input for a record book but I'm having trouble as I think I'm not making the getline function read the buffer correctly. I have the following variables declared in...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.