1

I am trying to build a meshgrid in order to compute an interpolation. I inspire myself from this example. My function is the following:

def oscillatory_(a,b,d,w=w,c=c):
    x = np.array([a,b,d])
    return np.cos(2*math.pi*w[0,0] + np.sum(c.T*x, axis = 1))

Which I call through:

data = oscillatory_(*np.meshgrid(a,b,d,indexing='ij', sparse=True))

Where

a = grid_dim[:,0]
b = grid_dim[:,1]
d = grid_dim[:,2]

are just some values taken from grid_dim which is a numpy n-array

When trying to run the code, I get the following error:

    Traceback (most recent call last):
  File "/usr/lib/python3.6/code.py", line 91, in runcode
    exec(code, self.locals)
  File "<input>", line 9, in <module>
  File "<input>", line 3, in f
AttributeError: 'numpy.ndarray' object has no attribute 'cos'

I really don't understand why is he assigning cos as an attribute and not a function, because if I run the function oscillatory outside the *np.meshgrid() everything is ok.

Also, I have played with the toy example from the link below adding a np.cos function and everything works fine. The problem is in my function and I am not able to figure where.

I am doing this in order to compute an interpolation afterwards through:

my_interpolating_function = RegularGridInterpolator((a,b,d), data)

Any help would be highly appreciated on that one. Thank you very much

3
  • 1
    If you give np.cos an array with object dtype, it iterates through the array, trying to delegate the task to an cos method for each item. I suspect then that you've passed an array of arrays to cos. Examine thecos argument for shape and dtype. Commented Apr 22, 2019 at 19:22
  • Are you using spyder? Commented Apr 22, 2019 at 19:34
  • Possible duplicate of AttributeError: 'numpy.ndarray' object has no attribute 'cos' Commented Apr 22, 2019 at 19:47

1 Answer 1

0

Making an array from sparse meshgrid produces an object dtype array

In [448]: I,J=np.meshgrid([0,1,2],[0,1], indexing='ij', sparse=True)                 
In [449]: I                                                                          
Out[449]: 
array([[0],
       [1],
       [2]])
In [450]: J                                                                          
Out[450]: array([[0, 1]])
In [451]: np.array([I, J])                                                           
Out[451]: 
array([array([[0],
       [1],
       [2]]), array([[0, 1]])], dtype=object)

With sparse False, you get a valid numeric array:

In [452]: I,J=np.meshgrid([0,1,2],[0,1], indexing='ij', sparse=False)                
In [453]: I                                                                          
Out[453]: 
array([[0, 0],
       [1, 1],
       [2, 2]])
In [454]: J                                                                          
Out[454]: 
array([[0, 1],
       [0, 1],
       [0, 1]])
In [455]: np.array([I, J])                                                           
Out[455]: 
array([[[0, 0],
        [1, 1],
        [2, 2]],

       [[0, 1],
        [0, 1],
        [0, 1]]])
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, but even with telling sparse = False, which is exactly what I want, I get a tuple problem. I did a post specifically on it: stackoverflow.com/questions/55807085/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.