473,785 Members | 2,602 Online
Bytes | Software Development & Data Engineering Community
+ 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 1916
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
6072
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 containing norwegian special characters æ, ø and å. >>> liste =
6
6545
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 #define CHANNEL_1 1 #define CHANNEL_2 2
8
4834
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
1956
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 GameBoard.h header file as follows: class GameBoard { //function prototypes public:
1
2740
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 refcursor. And I want to map the returning result to an entity class. I have created the entity class as follows: @Entity @SqlResultSetMapping(name = "flightBaggageSummaryMapping", entities = @EntityResult(entityClass =...
2
1452
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
5619
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 String.Format("'{0}'", value) End Function Basically, I just want to surround each value with single quotes. However, I am having trouble getting this to work using the System.Array.ForEach method. This may be partially because I am not very...
0
1155
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 main: char name, address, telno, input; int yearofbirth, choice, records; yearofbirth = 0; choice = -1; Then here's part of the code where it goes wrong:
5
13381
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 http://mghospedagem.com/images/controlpanel.jpg instead of http://mghospedagem.comhttp://mghospedagem.com/images/controlpanel.jpg As u see, there's the website URL before the image URL.
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10336
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10155
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8978
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7502
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3655
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.