如何将标量函数重塑为网格上的标量函数 [英] How to reshape a scalar funcction to a scalar function on a grid

查看:77
本文介绍了如何将标量函数重塑为网格上的标量函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题更多是关于如何组织np.meshgrid以便更好地理解它的一般问题.因此,我有一组3d点,对于每个点,我都有一个与之关联的标量值.因此,对于此函数,其形状为n x 1,但是现在我想使用相同的值将其重塑为np.meshgrid,这意味着使其成为3D numpy数组.但是,我不知道如何开始执行此操作,因为我不知道它的外观.您知道我该怎么做以及其背后的原因吗?

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?

谢谢,我真的是np.meshgrids的新手,我仍然无法完全理解它.

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

推荐答案

我将以2D方式对其进行解释(如果您很容易理解,则可以使用3D).

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

假设您有一个二维平面的截面.可以说它是一个长度和宽度为5的正方形.但是x的坐标在[10,15]中 和[15,20]中的y.

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.

现在,您要在此部分评估函数(假设分辨率为0.5). Numpys meshgrid 现在为您提供了两个矩阵,其中保存了每个像素"(0.5 x 0.5区域)的x和y坐标.

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')

我们的区域被分成10x10像素,因此我们希望形状为:

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

xx.shape
>>> (10, 10)

yy.shape
>>> (10, 10)

看着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]])

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]])

所以对于x方向为4像素,y方向为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

如果要在3D模式下使用,则只有三个立方体,而不是两个矩阵.

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)

您可以像这样使用xxyy:

zz = fun(xx,yy)
zz.shape

>>> (10, 10)

它看起来像:

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

这篇关于如何将标量函数重塑为网格上的标量函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆