Baz
Baz

Reputation: 13125

IndexError: too many indices for np.array

I wish to do something like this where I have an ndarray containing coordinates, and one containing values, as required by scipy.interpolate.griddata

import numpy
p = {(1,2):10, (0,2):12, (2,0):11}
coords, values = np.array([(np.array(k),v) for k,v in p.items()]).T

However, I also want to be able to do the following:

x = coords[:,0]
y = coords[0,:]
np.mgrid[x.min():x.max():5j, y.min():y.max():5j]

but, I am getting the error:

IndexError: too many indices for array

for x = coords[:,0].

What am I doing wrong?

Upvotes: 1

Views: 2144

Answers (1)

llllllllll
llllllllll

Reputation: 16404

Because you put np.array into np.array, the inner np.array is a whole and not recognizable to the outer np.array. In fact, your coords has shape (3,).

Correct way:

coordsx, coordsy, values = np.array([(kx, ky, v) for (kx, ky), v in p.items()]).T

Upvotes: 2

Related Questions