计算xarray数据集的平方根误差 [英] Calculate Root Squared Error of xarray dataset

查看:127
本文介绍了计算xarray数据集的平方根误差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有1月份的xarray数据集 monthly_data ,其中包含以下信息:

I have xarray dataset monthly_data of just January's with following info:

lat: float64 (192)
lon: float64 (288)
time: object (1200)(monthly data)

Data Variables:
tas: (time, lat, lon)[[[45,78,...],...]...]

我具有地面真实值 grnd_trth ,其中包含一月份的真实数据

I have ground truth value grnd_trth which has true data of January

Coordinates:
lat: float64 (192)
lon: float64 (288)

Data Variables:
tas(lat and lon)

现在,我想根据 grnd_trth monthly_data 计算每个月的平方根误差,我尝试使用循环,但我猜它工作正常,这是我的尝试:

Now I want to calculate root squared error for each month from monthly_data with respect to grnd_trth, I tried using loops and I guess it's working fine, here's my try:

rms = []

for i in range(1200):
  err = 0
  for j in (grnd_trth.tas[0] - monthly_data.tas[i]).values:
    for k in j:
      err += k**2
  rms.append(err**1/2)

我只想知道是否有更有效的方法或任何直接功能来做到这一点?

I just want to know is there more efficient way or any direct function to do so?

monthly_data.tas 的输出:

xarray.Datarray 'tas': (time:1200 lat: 192 lon: 288)
array([[[45,46,45,4....],....]...]

Coordinates:
lat:
array([-90. , -89.75,...])

lon:
array([0., 1.25.,.... ])

time:
array([cftime.DatetimeNoLeap(0001-01-15 12:00:00),
       cftime.DatetimeNoLeap(0002-01-15 12:00:00),
       cftime.DatetimeNoLeap(0003-01-15 12:00:00), ...,
       cftime.DatetimeNoLeap(1198-01-15 12:00:00),
       cftime.DatetimeNoLeap(1199-01-15 12:00:00),
       cftime.DatetimeNoLeap(1200-01-15 12:00:00)]

grnd_trth.tas 的输出:

xarray.Datarray 'tas': (lat: 192 lon: 288)
array([[45,46,45,4....],....]

Coordinates:
lat:
array([-90. , -89.75,...])

lon:
array([0., 1.25.,.... ])

time:
array([cftime.DatetimeNoLeap(0001-01-15 12:00:00)]

但是当我只使用.values()函数时,它只会返回tas值数组!

But when I just use .values( ) function it'll only return me tas value array!

推荐答案

以更有效"的方式进行操作时,有两点需要指出.

In terms of doing this in a more 'efficient' way, there are two things to point out.

1)允许您直接在xarray对象上进行算术运算.

1) You're allowed to do arithmetic operations directly on xarray objects eg.

for time_idx in range(1200):
    # For each time idx, find the root squared error at 
    # each pixel between grnd_truth and monthly_data

    err2 = (grnd_truth.tas - monthly_data.tas[time_idx,...])**2
    err  = err2**(1/2)

2)有一个方法调用.sum(),它对数组中的所有元素求和,所以这意味着您不必做for k in j:行就可以对像素求和.例如

2) There's a method call .sum() which sums all the elements in an array, so this means you won't have to do the for k in j: line in order to sum over the pixels. Eg.

rms=[]

for time_idx in range(2000):
    # same two lines as before...

    # sum over every pixel and extract the value from the DataArray
    err_tot = err.sum().values

    # Add to running total
    rms.append(err_tot)

现在,要指出的一件事是,仅从DataArray中提取值,就会丢失有关该数组的所有元数据!因此,这实际上不是最佳做法,但现在我认为这可以回答您的问题?

Now, one thing to point out here is that, by simply extracting the values from the DataArray, you lose all of the metadata about the array! So this isn't really best practice, but for now I think this answers your question?

这篇关于计算xarray数据集的平方根误差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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