how to do downsampling in python

Solutions on MaxInterview for how to do downsampling in python by the best coders in the world

showing results for - "how to do downsampling in python"
Yoann
29 Nov 2018
1import xarray as xr
2import numpy as np
3import matplotlib.pyplot as plt
4
5fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(15,5))
6
7# Create a 10x10 array of random numbers
8a = xr.DataArray(np.random.rand(10,10)*100, dims=['x', 'y'])
9
10# "Downscale" the array, mean of blocks of size (2x2)
11b = a.coarsen(x=2, y=2).mean()
12
13# "Downscale" the array, mean of blocks of size (5x5)
14c = a.coarsen(x=5, y=5).mean()
15
16# Plot and cosmetics
17a.plot(ax=ax1)
18ax1.set_title("Full Data")
19
20b.plot(ax=ax2)
21ax2.set_title("mean of (2x2) boxes")
22
23c.plot(ax=ax3)
24ax3.set_title("mean of (5x5) boxes")
25