2

我有从 1992 年到 2016 年的一维月度数据。我想说的是,从 1992 年到 2016 年的所有 1 月值的平均值,以隔离季节性变化。我的 for 循环做对了吗?

test_mean = []

for months in range(lsds_detrend_mnths.size):
    mean = np.mean(lsds_detrend_mnths[months::months+1])
    test_mean.append(mean)

我是否正确使用了数组切片?

或者为了获取所有年份每个月的平均值,我是否要执行此循环?

4

1 回答 1

1

如果您的数据从一月份开始,您应该使用它来获取一月份的平均值:

mean = np.mean(lsds_detrend_mnths[::12])

这将是一月份的平均值。要获得其他月份的平均值,您可以使用此循环:

for i in range(12):
    test_mean.append(np.mean(lsds_detrend_mnths[i::12]))

只要记住切片符号的含义:

a[start:stop:step] # start through not past stop, by step

于 2020-11-27T09:26:16.430 回答