英文:
What is the behaviour of xarray when multiplying data arrays?
问题
我想要将两个具有相同维度的数据数组相乘:
```python
print(data1)
<xarray.DataArray 'var' (lat: 2160, lon: 4320)>
[9331200 values with dtype=int8]
Coordinates:
* lon (lon) float64 -180.0 -179.9 -179.8 -179.7 ... 179.8 179.9 180.0
* lat (lat) float64 89.96 89.88 89.79 89.71 ... -89.79 -89.88 -89.96
print(data2)
<xarray.DataArray 'var' (lat: 2160, lon: 4320)>
[9331200 values with dtype=float32]
Coordinates:
* lon (lon) float64 -180.0 -179.9 -179.8 -179.7 ... 179.8 179.9 180.0
* lat (lat) float64 89.96 89.88 89.79 89.71 ... -89.79 -89.87 -89.96
data1 * data2
返回以下错误:
ValueError: Cannot apply_along_axis when any iteration dimensions are 0
请注意,根据此线程的建议,我确保维度一致,并重新索引了两个数据数组。
由于这两个数组具有不同的dtype
,我尝试了 data1.astype(np.float64) * data2
,但返回相同的错误。
另一方面,这返回了一个空数组:
data3 = data1.astype(np.float64) * data2.astype(np.float64)
print(data3)
<xarray.DataArray 'var' (lat: 0, lon: 0)>
array([], shape=(0, 0), dtype=float64)
Coordinates:
* lon (lon) float64
* lat (lat) float64
我找到的唯一一种实现这种乘法的方法是获取底层的 np 数据:
data3 = data1.data * data2.data
尽管这对我的需求有效,但我仍然很好奇为什么纯粹的 xarray 方法会失败。有人可以告诉我或指向我可能错过的文档部分吗?
<details>
<summary>英文:</summary>
I would like to multiply two data arrays of same dimensions:
```python
print(data1)
<xarray.DataArray 'var' (lat: 2160, lon: 4320)>
[9331200 values with dtype=int8]
Coordinates:
* lon (lon) float64 -180.0 -179.9 -179.8 -179.7 ... 179.8 179.9 180.0
* lat (lat) float64 89.96 89.88 89.79 89.71 ... -89.79 -89.88 -89.96
print(data2)
<xarray.DataArray 'var' (lat: 2160, lon: 4320)>
[9331200 values with dtype=float32]
Coordinates:
* lon (lon) float64 -180.0 -179.9 -179.8 -179.7 ... 179.8 179.9 180.0
* lat (lat) float64 89.96 89.88 89.79 89.71 ... -89.79 -89.87 -89.96
data1 * data2
returns this error:
ValueError: Cannot apply_along_axis when any iteration dimensions are 0
Note that following this thread, I made sure to have consistent dimensions and re-indexed both data arrays.
Since both arrays have different dtype
, I have tried data1.astype(np.float64) * data2
, but that returned the same error.
On the other hand, this returned an empty array:
data3 = data1.astype(np.float64) * data2.astype(np.float64)
print(data3)
<xarray.DataArray 'var' (lat: 0, lon: 0)>
array([], shape=(0, 0), dtype=float64)
Coordinates:
* lon (lon) float64
* lat (lat) float64
The only way I found to achieve this multiplication was to get the underlying np data:
data3 = data1.data * data2.data
Although this works for my need, I am still curious to understand why the pure xarray method fails. Can anyone inform me or point me towards a part of the documentation I might have missed?
答案1
得分: 0
以下是翻译好的部分:
对于那些感兴趣的人,我的两个数据数组的坐标存在轻微差异,显然是由于浮点精度问题(感谢这些人)。您可以使用以下代码来验证两个数据数组的坐标是否正确:
import xarray as xr
xr.testing.assert_equal(data1.lon, data2.lon)
xr.testing.assert_equal(data1.lat, data2.lat)
如果确信坐标应该对齐,一个选项是手动修正坐标:
data1['lon'] = data2['lon']
data1['lat'] = data2['lat']
然后乘法运算就可以正常进行。
英文:
For those interested, there was a slight difference in the coordinates of my two data arrays, apparently due to floating point precision (thanks to these guys). You can whether coordinates of both data arrays are correct with:
import xarray as xr
xr.testing.assert_equal(data1.lon, data2.lon)
xr.testing.assert_equal(data1.lat, data2.lat)
In case one is sure the coordinates should align, one option is to manually correct the coordinates:
data1['lon'] = data2['lon']
data1['lat'] = data2['lat']
Then the multiplication works without problem.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论