3

The code below creates a list of arrays of coordinates from a DataFrame using numpy mgrid function - is there a way of doing this without the for-loop and utilizing numpy arrays?

import pandas as pd
import numpy as np

x = np.random.randint(0,100,10)
y = np.random.randint(0,100,10)
z = np.random.randint(0,100,10)

x2 = x+10
y2 = y+10
z2 = z+10

num_x = np.random.randint(1,11,10)
num_y = np.random.randint(1,11,10)
data = [x,y,z,x2,y2,z2,num_x,num_y]

df = pd.DataFrame(data).T
df.columns = ['x','y','z','x2','y2','z2','num_x','num_y']

out=[]

for i in range(len(df)):
    grid = np.mgrid[df['x'].values[i]:df['x2'].values[i]:df['num_x'].values[i]*1j,df['y'].values[i]:df['y2'].values[i]:df['num_y'].values[i]*1j,df['z'].values[i]:df['z2'].values[i]:1j]
    grid2 = np.array([grid[0].flatten(),grid[1].flatten(),grid[2].flatten()]).T
    out.append(grid2)

I appreciate that a for loop may be necessary but wanted to check before giving up. Reason for asking; a couple of articles 1, 2 suggest that by avoiding for-loops, list comprehension and map function in favour of numpy functions, especially when the data is array like, can significantly reduce runtime.

I have tried to pass the x-data as an array to mgrid but it gives a TypeError;

np.mgrid[df['x'].values:df['x2'].values:df['num_x'].values*1j]

TypeError: only length-1 arrays can be converted to Python scalars

A related question suggests that this error is caused by non-numpy like data going into the numpy function, but everything is type numpy.ndarray. Does this mean that mgrid can't take array input? Thanks.

1
  • It's complicated not to do this with a loop because each grid has different dimensions, so it is not clear how you could just compute some sort of super array with all the grids at the same time... Commented May 31, 2018 at 14:05

0

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.