Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python xarray广播如何使用numpy功能?_Python_Numpy_Python Xarray - Fatal编程技术网

Python xarray广播如何使用numpy功能?

Python xarray广播如何使用numpy功能?,python,numpy,python-xarray,Python,Numpy,Python Xarray,我想用我的xarray坐标做坐标变换,所以 我有一个数据数组,如: d = xr.DataArray(np.zeros((10, 10, 1)), dims=['x', 'y', 'z'] 我做的是这样的操作: r = np.sqrt(d.x**2 + d.y**2 + d.z**2) theta = np.arctan2(np.sqrt(d.x**2 + d.y**2), d.z) phi = np.arctan2(d.y, d.x) 我得到以下形状: In [212]: r.shape

我想用我的xarray坐标做坐标变换,所以

我有一个数据数组,如:

d = xr.DataArray(np.zeros((10, 10, 1)), dims=['x', 'y', 'z']
我做的是这样的操作:

r = np.sqrt(d.x**2 + d.y**2 + d.z**2)
theta = np.arctan2(np.sqrt(d.x**2 + d.y**2), d.z)
phi = np.arctan2(d.y, d.x)
我得到以下形状:

In [212]: r.shape
Out[212]: (10, 10, 1)

In [214]: theta.shape
Out[214]: (10, 10)

In [216]: phi.shape
Out[216]: (10,)
我想看看我得到的所有r的
(10,10,1)
形状

看起来xarray的奇特的广播只适用于基本的算术运算,而numpy函数的广播很幼稚

这是正确的吗


有没有一个被认可的方法来解决这个问题?我可以编写自己的支持xarray的arctan2函数来正确地进行广播,但我希望我不必这样做。

不幸的是,NumPy函数通常不会在xarray对象上进行适当的广播。但是xarray确实附带了许多这些函数的包装版本,包括中的
arctan2
。所以
xarray.ufuncs.arctan2
应该完全满足您的需求