在多维数组上应用Mann-Whitney U测试并替换Python中xarray数据数组变量的单个值?

2024-09-28 22:32:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python新手,需要xarray的帮助。 我有两个用于未来和过去气候的三维数据阵列(rlon、rlat、time)。我想计算每个网格点的Mann-Whitney-U检验,以分析未来与过去相比温度变化的重要性。我已经得到了Mann-Whitney-U-test的工作,从历史和未来数据的一个网格点中选择了一个时间序列。例如:

import numpy as np
import xarray as xr
import scipy.stats as sts

#selecting time period and grid point of past and future data

tp = fileHis['tas']
tf = fileFut['tas']
gridpoint_past=tp.sel(rlon=-6.375, rlat=1.375, time=slice('1999-01-01', '1999-01-31'))
gridpoint_future=tf.sel(rlon=-6.375, rlat=1.375, time=slice('2099-01-01', '2099-01-31'))

#mannwhintey-u-test

result=sts.mannwhitneyu(gridpoint_past, gridpoint_future, alternative='two-sided')
print('pvalue =',result[1])

输出: pvalue = 0.05922372345359562

我现在的问题是,我需要对每个网格点和每个月都这样做,最后我希望有一个数据数组,其中包含一年中每个网格点和每个月的PV值。 我在考虑循环使用所有rlat、rlon和月份,并对每个月份进行Mann-Whitney-U测试,除非有更好的方法。? 我如何将pvalues逐个写入具有相同rlat,rlon维度的新数据数组? 我试过这个,但不起作用: 我创建了一个数据数组pvalue_mon,它具有与tptf相同的rlat、rlon,并且具有12个月的时间步长

pvalue_mon.sel(rlon=-6.375, rlat=1.375, time=th.time.dt.month.isin([1])) = result[1] 
SyntaxError: can't assign to function call

或者这个:

pvalue_mon.sel(rlon=-6.375, rlat=1.375, time=pvalue_mon.time.dt.month.isin([1])).update(result[1])
TypeError: 'numpy.float64' object is not iterable

如何替换现有变量的单个值


Tags: 数据import网格timeasresultmannwhitney