How to reshape a scalar funcction to a scalar func

2019-08-26 06:01发布

My question is more of a general question about how np.meshgrid is organized to understand it better. So I have a set of 3d points and for each point I have a scalar value associated with it. So for this function is shaped as n x 1, however now I want to reshape this function with the same values, to a np.meshgrid this means to make it to a 3D numpy array. However I don't understand how can I begin to do that this, since I don't know how it supposed to look like. Do you know how I can do this and the reasoning behind it?

Thank you I'm really new to np.meshgrids and I still can't comprehend it fully.

标签: numpy grid
1条回答
smile是对你的礼貌
2楼-- · 2019-08-26 06:54

I will explain it in 2D (going to 3D if you understand it is quite easy).

Just imagine you have a section from the two dimensional plane. Lets say its a square with 5 in length and width. But the coordinates for x are in [10,15] and for y in [15,20], respectively.

Now you want to evaluate a function on this section (lets say with a resolution of 0.5). Numpys meshgrid now gives you two matrices in which the x and y coordinates for each "pixel" (0.5 x 0.5 area) are saved.

In some code this looks like:

import numpy as np

x = np.arange(10,15,.5)
y = np.arange(15,20,.5)

xx, yy = np.meshgrid(x,y, indexing = 'ij')

our area is dived into 10x10 pixels and therefore we expect the shapes to be:

xx.shape
>>> (10, 10)
yy.shape
>>> (10, 10)

looking at xx:

array([[10. , 10. , 10. , 10. , 10. , 10. , 10. , 10. , 10. , 10. ],
       [10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5],
       [11. , 11. , 11. , 11. , 11. , 11. , 11. , 11. , 11. , 11. ],
       [11.5, 11.5, 11.5, 11.5, 11.5, 11.5, 11.5, 11.5, 11.5, 11.5],
       [12. , 12. , 12. , 12. , 12. , 12. , 12. , 12. , 12. , 12. ],
       [12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5],
       [13. , 13. , 13. , 13. , 13. , 13. , 13. , 13. , 13. , 13. ],
       [13.5, 13.5, 13.5, 13.5, 13.5, 13.5, 13.5, 13.5, 13.5, 13.5],
       [14. , 14. , 14. , 14. , 14. , 14. , 14. , 14. , 14. , 14. ],
       [14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5]])

and yy:

array([[15. , 15.5, 16. , 16.5, 17. , 17.5, 18. , 18.5, 19. , 19.5],
       [15. , 15.5, 16. , 16.5, 17. , 17.5, 18. , 18.5, 19. , 19.5],
       [15. , 15.5, 16. , 16.5, 17. , 17.5, 18. , 18.5, 19. , 19.5],
       [15. , 15.5, 16. , 16.5, 17. , 17.5, 18. , 18.5, 19. , 19.5],
       [15. , 15.5, 16. , 16.5, 17. , 17.5, 18. , 18.5, 19. , 19.5],
       [15. , 15.5, 16. , 16.5, 17. , 17.5, 18. , 18.5, 19. , 19.5],
       [15. , 15.5, 16. , 16.5, 17. , 17.5, 18. , 18.5, 19. , 19.5],
       [15. , 15.5, 16. , 16.5, 17. , 17.5, 18. , 18.5, 19. , 19.5],
       [15. , 15.5, 16. , 16.5, 17. , 17.5, 18. , 18.5, 19. , 19.5],
       [15. , 15.5, 16. , 16.5, 17. , 17.5, 18. , 18.5, 19. , 19.5]])

so to the the coordinates for the 4 pixel into x and 5 pixel into y direction you can just get:

x_coord = xx[4,5]
y_coord = yy[4,5]
x_coord
>>> 12.0
y_coord
>>> 17.5

If you want in 3D you have three cubes instead of two matrices that's basically it.

Now if you want to evaluate a function on this lets say:

def fun(x,y):
    return np.sin(x)*np.cos(y)

you can just use xx and yy like:

zz = fun(xx,yy)
zz.shape
>>> (10, 10)

and it looks like:

import matplotlib.pyplot as plt
plt.contourf(xx,yy,zz)

enter image description here

查看更多
登录 后发表回答