广义可加模型在xarray中的应用

2024-06-09 03:07:22 发布

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

我有一个netCDF文件,我和xarray一起读过。数组包含时间、纬度、经度和一个数据变量(即索引值)

# read the netCDF files
    with xr.open_mfdataset('wet_tropics.nc') as wet:
    print(wet)

    Out[]: 
    <xarray.Dataset>
    Dimensions:       (time: 1437, x: 24, y: 20)
    Coordinates:
      * y             (y) float64 -1.878e+06 -1.878e+06 -1.878e+06 -1.878e+06 ...
      * x             (x) float64 1.468e+06 1.468e+06 1.468e+06 1.468e+06 ...
      * time          (time) object '2013-03-29T00:22:28.500000000' ...
    Data variables:
        index_values  (time, y, x) float64 dask.array<shape=(1437, 20, 24), chunksize=(1437, 20, 24)>

到目前为止,还不错。 现在我需要对数组中的每个网格单元应用一个广义加法模型。我想使用的模型来自Facebook的Prophet(https://facebook.github.io/prophet/),我之前已经成功地将它应用到了pandas的数据数组中。例如:

cns_ap['y'] = cns_ap['av_index']  # Prophet requires specific names 'y' and 'ds' for column names
cns_ap['ds'] = cns_ap['Date']
cns_ap['cap'] = 1
m1 = Prophet(weekly_seasonality=False,  # disables weekly_seasonality
             daily_seasonality=False,  # disables daily_seasonality
             growth='logistic',  # logistic because indices have a maximum 
             yearly_seasonality=4,  # fourier transform. int between 1-10
             changepoint_prior_scale=0.5).fit(cns_ap)  
future1 = m1.make_future_dataframe(periods=60,  # 5 year prediction
                                   freq='M',  # monthly predictions
                                   include_history=True)  # fits model to all historical data
future1['cap'] = 1  # sets cap at maximum index value
forecast1 = m1.predict(future1)
# m1.plot_components(forecast1, plot_cap=False);
# m1.plot(forecast1, plot_cap=False, ylabel='CNS index', xlabel='Year');

问题是现在我必须 1) 遍历netCDF文件的每个单元格, 2) 通过时间获取该单元格的所有值, 3) 应用GAM(使用fbprophet),然后导出并打印结果。你知道吗

问题是:你对如何在光栅中循环,获取每个像素的索引值,以便我运行GAM有什么想法吗? 我认为嵌套for循环是可行的,尽管我不知道如何创建一个遍历每个单元格的循环。你知道吗

感谢您的帮助


Tags: falseindextimeplotnetcdf数组apcap