如何解决groupby apply函数中缺少值
我有一个大数据集[time,lat,lon]。我想针对网格中的每个点针对时间对变量进行回归,但是使用for循环需要永远的时间。我发现按纬度/经度对数据数组进行分组并应用函数可以大大减少计算时间。我正在将一个函数应用于经纬度分组的数据数组[time,lat,lon]。我的最终结果将是[lat,lon]。某些网格点没有全部数据(np.nan)。我希望函数为此类网格点返回np.nan,但是出现以下错误: SVD不能在线性最小二乘中收敛
这是代码:
hus = ifile.hus
#stack lat and lon into a single dimension called allpoints
stacked = hus.stack(allpoints=['lat','lon'])
# define a function to compute a linear trend of a timeseries
def linear_trend(x):
x = x.dropna(dim='time')
if len(x)==0:
return xr.DataArray(np.nan)
else:
time = np.arange(len(x))
pf = np.polyfit(time,x,1)
return xr.DataArray(pf[0])
trend = stacked.groupby('allpoints').apply(linear_trend)
trend_unstacked = trend.unstack('allpoints')
trend_unstacked
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-9-4e885325e9c4> in <module>
13
14 # apply the function over allpoints to calculate the trend at each point
---> 15 trend = t.groupby('allpoints').apply(linear_trend)
16 trend_unstacked = trend.unstack('allpoints')
17
~/anaconda3/lib/python3.7/site-packages/xarray/core/groupby.py in apply(self,func,shortcut,args,**kwargs)
824 stacklevel=2,825 )
--> 826 return self.map(func,shortcut=shortcut,args=args,**kwargs)
827
828 def _combine(self,applied,restore_coord_dims=False,shortcut=False):
~/anaconda3/lib/python3.7/site-packages/xarray/core/groupby.py in map(self,**kwargs)
809 grouped = self._iter_grouped()
810 applied = (maybe_wrap_array(arr,func(arr,*args,**kwargs)) for arr in grouped)
--> 811 return self._combine(applied,shortcut=shortcut)
812
813 def apply(self,shortcut=False,args=(),**kwargs):
~/anaconda3/lib/python3.7/site-packages/xarray/core/groupby.py in _combine(self,restore_coord_dims,shortcut)
833 combined = self._concat_shortcut(applied,dim,positions)
834 else:
--> 835 combined = concat(applied,dim)
836 combined = _maybe_reorder(combined,positions)
837
~/anaconda3/lib/python3.7/site-packages/xarray/core/concat.py in concat(objs,data_vars,coords,compat,positions,fill_value,join)
133 "objects,got %s" % type(first_obj)
134 )
--> 135 return f(objs,join)
136
137
~/anaconda3/lib/python3.7/site-packages/xarray/core/concat.py in _dataarray_concat(arrays,join)
427 join="outer",428 ):
--> 429 arrays = list(arrays)
430
431 if data_vars != "all":
~/anaconda3/lib/python3.7/site-packages/xarray/core/groupby.py in <genexpr>(.0)
808 else:
809 grouped = self._iter_grouped()
--> 810 applied = (maybe_wrap_array(arr,**kwargs)) for arr in grouped)
811 return self._combine(applied,shortcut=shortcut)
812
<ipython-input-9-4e885325e9c4> in linear_trend(x)
8 else:
9 time = np.arange(len(y))
---> 10 pf = np.polyfit(time,1)
11 # we need to return a dataarray or else xarray's groupby won't be happy
12 return xr.DataArray(pf[0])
<__array_function__ internals> in polyfit(*args,**kwargs)
~/anaconda3/lib/python3.7/site-packages/numpy/lib/polynomial.py in polyfit(x,y,deg,rcond,full,w,cov)
603 raise TypeError("expected 1D or 2D array for y")
604 if x.shape[0] != y.shape[0]:
--> 605 raise TypeError("expected x and y to have same length")
606
607 # set rcond
TypeError: expected x and y to have same length
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。