data2 = xr.open_dataset(file_path)[target_var].loc['1962-01-01':'2014-12-12', :, :] data = data2.loc[data2.time.dt.month.isin([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])].resample(time='Y').mean(dim='time').values#.mean(dim=['time']).values data1 = data.flatten() # print(data1.shape) model[i] = data1 # print(model[i].shape) data_spring = data2.loc[data2.time.dt.month.isin([3, 4, 5])].resample(time='Y').mean(dim='time').values#.mean(dim=['time']).values data1_spring = data_spring.flatten() print(model_spring[i].shape) print(data1_spring.shape) model_spring[i] = data1_spring data_summer = data2.loc[data2.time.dt.month.isin([6, 7, 8])].resample(time='Y').mean(dim='time').values#.mean(dim=['time']).values data1_summer = data_summer.flatten() model_summer[i] = data1_summer data_autumn = data2.loc[data2.time.dt.month.isin([9, 10, 11])].resample(time='Y').mean(dim='time').values#.mean(dim=['time']).values data1_autumn = data_autumn.flatten() model_autumn[i] = data1_autumn data_winter = data2.loc[data2.time.dt.month.isin([12, 1, 2])].resample(time='Y').mean(dim='time').values#.mean(dim=['time']).values data1_winter = data_winter.flatten() model_winter[i] = data1_winter i = i + 1这样读取速度太慢了,可不可以帮我优化一下
时间: 2023-06-24 21:06:45 浏览: 176
可以考虑以下优化:
1. 读取数据时可以使用延迟加载的方式,即设置 `chunks` 参数,将数据分块读取,可以减少一次性加载数据占用的内存和读取时间。
2. 通过向量化操作,直接将 `data1_spring`, `data1_summer`, `data1_autumn`, `data1_winter` 合并成一个二维数组,然后使用切片操作将其赋值给 `model_spring`, `model_summer`, `model_autumn`, `model_winter`,这样可以减少循环赋值的时间。
下面是优化后的代码:
```
data2 = xr.open_dataset(file_path)[target_var].loc['1962-01-01':'2014-12-12', :, :]
data = data2.loc[data2.time.dt.month.isin([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])].resample(time='Y').mean(dim='time').chunk({'latitude': 50, 'longitude': 50, 'time': -1})
data_spring = data2.loc[data2.time.dt.month.isin([3, 4, 5])].resample(time='Y').mean(dim='time').chunk({'latitude': 50, 'longitude': 50, 'time': -1})
data_summer = data2.loc[data2.time.dt.month.isin([6, 7, 8])].resample(time='Y').mean(dim='time').chunk({'latitude': 50, 'longitude': 50, 'time': -1})
data_autumn = data2.loc[data2.time.dt.month.isin([9, 10, 11])].resample(time='Y').mean(dim='time').chunk({'latitude': 50, 'longitude': 50, 'time': -1})
data_winter = data2.loc[data2.time.dt.month.isin([12, 1, 2])].resample(time='Y').mean(dim='time').chunk({'latitude': 50, 'longitude': 50, 'time': -1})
model = np.zeros((len(files), data.size), dtype='float32')
model_spring = np.zeros((len(files), data_spring.size), dtype='float32')
model_summer = np.zeros((len(files), data_summer.size), dtype='float32')
model_autumn = np.zeros((len(files), data_autumn.size), dtype='float32')
model_winter = np.zeros((len(files), data_winter.size), dtype='float32')
i = 0
for d, s, su, a, w in zip(data, data_spring, data_summer, data_autumn, data_winter):
data1 = d.values.flatten()
data1_spring = s.values.flatten()
data1_summer = su.values.flatten()
data1_autumn = a.values.flatten()
data1_winter = w.values.flatten()
model[i] = data1
model_spring[i:i+len(data1_spring)] = data1_spring.reshape(-1, data_spring.shape[1])
model_summer[i:i+len(data1_summer)] = data1_summer.reshape(-1, data_summer.shape[1])
model_autumn[i:i+len(data1_autumn)] = data1_autumn.reshape(-1, data_autumn.shape[1])
model_winter[i:i+len(data1_winter)] = data1_winter.reshape(-1, data_winter.shape[1])
i += 1
```
阅读全文